Mistral 7B教程:使用和微调Mistral 7b的分步指南
>本教程提供了使用和微调Mistral 7b语言模型的自然语言处理任务的综合指南。 您将学会利用Kaggle进行模型访问,执行推理,应用量化技术,微调模型,合并适配器并部署到拥抱面枢纽。
>访问Mistral 7bMistral 7b可以通过各种平台访问,包括拥抱脸,顶点AI,Replicate,Sagemaker Jumpstart和Baseten。 本教程的重点是利用Kaggle的“模型”功能进行简化的访问,消除了对手动下载的需求。
>本节演示了从kaggle和执行推理加载模型。 基本图书馆更新对于防止错误至关重要:
使用BITSANDBYTES使用NF4配置进行4位量化量化量增强加载速度并降低内存使用情况:
<code>!pip install -q -U transformers !pip install -q -U accelerate !pip install -q -U bitsandbytes</code>
>将Mistral 7b模型添加到您的Kaggle笔记本中涉及以下步骤:
<code>from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline import torch bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, )</code>
>搜索“ Mistral 7b”,选择“ 7b-v0.1-hf”,然后添加。
- >
- 记录目录路径。
库:
使用
>
transformers
<code>model_name = "/kaggle/input/mistral/pytorch/7b-v0.1-hf/1" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, load_in_4bit=True, quantization_config=bnb_config, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, )</code>
pipeline
MISTRAL 7B微调
<code>pipe = pipeline( "text-generation", model=model, tokenizer = tokenizer, torch_dtype=torch.bfloat16, device_map="auto" )</code>
>设置
<code>prompt = "As a data scientist, can you explain the concept of regularization in machine learning?" sequences = pipe( prompt, do_sample=True, max_new_tokens=100, temperature=0.7, top_k=50, top_p=0.95, num_return_sequences=1, ) print(sequences[0]['generated_text'])</code>
>
导入相关模块:
使用Kaggle Secrets安全管理API密钥:guanaco-llama2-1k
基本模型,数据集和新型号名称已定义:
数据加载
<code>%%capture %pip install -U bitsandbytes %pip install -U transformers %pip install -U peft %pip install -U accelerate %pip install -U trl</code>
加载数据集并显示样本:
><code>from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig,HfArgumentParser,TrainingArguments,pipeline, logging from peft import LoraConfig, PeftModel, prepare_model_for_kbit_training, get_peft_model import os,torch, wandb from datasets import load_dataset from trl import SFTTrainer</code>
<code>from kaggle_secrets import UserSecretsClient user_secrets = UserSecretsClient() secret_hf = user_secrets.get_secret("HUGGINGFACE_TOKEN") secret_wandb = user_secrets.get_secret("wandb")</code>
>加载Mistral 7b
<code>!huggingface-cli login --token $secret_hf wandb.login(key = secret_wandb) run = wandb.init( project='Fine tuning mistral 7B', job_type="training", anonymous="allow" )</code>
<code>base_model = "/kaggle/input/mistral/pytorch/7b-v0.1-hf/1" dataset_name = "mlabonne/guanaco-llama2-1k" new_model = "mistral_7b_guanaco"</code>
加载和配置了令牌:
>
<code>dataset = load_dataset(dataset_name, split="train") dataset["text"][100]</code>
添加了一个lora适配器以进行有效的微调:
超参数
>培训论点是定义的:
<code>bnb_config = BitsAndBytesConfig( load_in_4bit= True, bnb_4bit_quant_type= "nf4", bnb_4bit_compute_dtype= torch.bfloat16, bnb_4bit_use_double_quant= False, ) model = AutoModelForCausalLM.from_pretrained( base_model, load_in_4bit=True, quantization_config=bnb_config, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ) model.config.use_cache = False model.config.pretraining_tp = 1 model.gradient_checkpointing_enable()</code>
SFT培训
SFTTRAINER已配置并启动培训: 微调模型被保存并推到拥抱的脸上枢纽: 合并适配器 访问微型模型
<code>!pip install -q -U transformers
!pip install -q -U accelerate
!pip install -q -U bitsandbytes</code>
保存并推动模型
<code>from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
import torch
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
)</code>
使用权重和偏见评估模型性能。提供了推理示例。
>适配器与基本模型合并,并将结果模型推向拥抱的脸。
合并的模型是通过拥抱的脸和推理加载的。
结论
以上是Mistral 7B教程:使用和微调Mistral 7b的分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文回顾了AI最高的艺术生成器,讨论了他们的功能,对创意项目的适用性和价值。它重点介绍了Midjourney是专业人士的最佳价值,并建议使用Dall-E 2进行高质量的可定制艺术。

Meta的Llama 3.2:多模式和移动AI的飞跃 Meta最近公布了Llama 3.2,这是AI的重大进步,具有强大的视觉功能和针对移动设备优化的轻量级文本模型。 以成功为基础

本文比较了诸如Chatgpt,Gemini和Claude之类的顶级AI聊天机器人,重点介绍了其独特功能,自定义选项以及自然语言处理和可靠性的性能。

文章讨论了Grammarly,Jasper,Copy.ai,Writesonic和Rytr等AI最高的写作助手,重点介绍了其独特的内容创建功能。它认为Jasper在SEO优化方面表现出色,而AI工具有助于保持音调的组成

嘿,编码忍者!您当天计划哪些与编码有关的任务?在您进一步研究此博客之前,我希望您考虑所有与编码相关的困境,这是将其列出的。 完毕? - 让&#8217

Shopify首席执行官TobiLütke最近的备忘录大胆地宣布AI对每位员工的基本期望是公司内部的重大文化转变。 这不是短暂的趋势。这是整合到P中的新操作范式

本周的AI景观:进步,道德考虑和监管辩论的旋风。 OpenAI,Google,Meta和Microsoft等主要参与者已经释放了一系列更新,从开创性的新车型到LE的关键转变

本文评论了Google Cloud,Amazon Polly,Microsoft Azure,IBM Watson和Discript等高级AI语音生成器,重点介绍其功能,语音质量和满足不同需求的适用性。
