目錄
超參數
SFT培訓
保存並推動模型
>適配器與基本模型合併,並將結果模型推向擁抱的臉。
首頁 科技週邊 人工智慧 Mistral 7B教程:使用和微調Mistral 7b的分步指南

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

Mar 09, 2025 am 10:37 AM

>本教程提供了使用和微調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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1657
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1229
24
開始使用Meta Llama 3.2 -Analytics Vidhya 開始使用Meta Llama 3.2 -Analytics Vidhya Apr 11, 2025 pm 12:04 PM

Meta的Llama 3.2:多模式和移動AI的飛躍 Meta最近公佈了Llama 3.2,這是AI的重大進步,具有強大的視覺功能和針對移動設備優化的輕量級文本模型。 以成功為基礎

10個生成AI編碼擴展,在VS代碼中,您必須探索 10個生成AI編碼擴展,在VS代碼中,您必須探索 Apr 13, 2025 am 01:14 AM

嘿,編碼忍者!您當天計劃哪些與編碼有關的任務?在您進一步研究此博客之前,我希望您考慮所有與編碼相關的困境,這是將其列出的。 完畢? - 讓&#8217

AV字節:Meta&#039; llama 3.2,Google的雙子座1.5等 AV字節:Meta&#039; llama 3.2,Google的雙子座1.5等 Apr 11, 2025 pm 12:01 PM

本週的AI景觀:進步,道德考慮和監管辯論的旋風。 OpenAI,Google,Meta和Microsoft等主要參與者已經釋放了一系列更新,從開創性的新車型到LE的關鍵轉變

向員工出售AI策略:Shopify首席執行官的宣言 向員工出售AI策略:Shopify首席執行官的宣言 Apr 10, 2025 am 11:19 AM

Shopify首席執行官TobiLütke最近的備忘錄大膽地宣布AI對每位員工的基本期望是公司內部的重大文化轉變。 這不是短暫的趨勢。這是整合到P中的新操作範式

視覺語言模型(VLMS)的綜合指南 視覺語言模型(VLMS)的綜合指南 Apr 12, 2025 am 11:58 AM

介紹 想像一下,穿過​​美術館,周圍是生動的繪畫和雕塑。現在,如果您可以向每一部分提出一個問題並獲得有意義的答案,該怎麼辦?您可能會問:“您在講什麼故事?

GPT-4O vs OpenAI O1:新的Openai模型值得炒作嗎? GPT-4O vs OpenAI O1:新的Openai模型值得炒作嗎? Apr 13, 2025 am 10:18 AM

介紹 Openai已根據備受期待的“草莓”建築發布了其新模型。這種稱為O1的創新模型增強了推理能力,使其可以通過問題進行思考

如何在SQL中添加列? - 分析Vidhya 如何在SQL中添加列? - 分析Vidhya Apr 17, 2025 am 11:43 AM

SQL的Alter表語句:動態地將列添加到數據庫 在數據管理中,SQL的適應性至關重要。 需要即時調整數據庫結構嗎? Alter表語句是您的解決方案。本指南的詳細信息添加了Colu

最新的最佳及時工程技術的年度彙編 最新的最佳及時工程技術的年度彙編 Apr 10, 2025 am 11:22 AM

對於那些可能是我專欄新手的人,我廣泛探討了AI的最新進展,包括體現AI,AI推理,AI中的高科技突破,及時的工程,AI培訓,AI,AI RE RE等主題

See all articles