首頁 >科技週邊 >人工智慧 >Mistral 7B教程:使用和微調Mistral 7b的分步指南

Mistral 7B教程:使用和微調Mistral 7b的分步指南

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌原創
2025-03-09 10:37:13893瀏覽

>本教程提供了使用和微調Mistral 7b語言模型的自然語言處理任務的綜合指南。 您將學會利用Kaggle進行模型訪問,執行推理,應用量化技術,微調模型,合併適配器並部署到擁抱面樞紐。

>訪問Mistral 7b

Mistral 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”,然後添加。
    >
  1. 記錄目錄路徑。
型號和令牌加載使用

庫:> Mistral 7B Tutorial: A Step-by-Step Guide to Using and Fine-Tuning Mistral 7B 使用

>函數來簡化推理:

> 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>

pipelineMISTRAL 7B微調

<code>pipe = pipeline(
    "text-generation", 
    model=model, 
    tokenizer = tokenizer, 
    torch_dtype=torch.bfloat16, 
    device_map="auto"
)</code>
本節通過使用PEFT,4位量化和Qlora之類的技術來指導您通過數據集上的Mistral 7b進行微調。 該教程還引用了有關更多環境的微調Llama 2指南。

>設置

<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>
該模型裝有4位精度:

<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>
添加適配器

Mistral 7B Tutorial: A Step-by-Step Guide to Using and Fine-Tuning Mistral 7B 添加了一個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>

Mistral 7B Tutorial: A Step-by-Step Guide to Using and Fine-Tuning Mistral 7B

保存並推動模型

微調模型被保存並推到擁抱的臉上樞紐:>

<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教程:使用和微調Mistral 7b的分步指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn