首頁 > 科技週邊 > 人工智慧 > 開始使用PHI-2

開始使用PHI-2

William Shakespeare
發布: 2025-03-08 10:50:11
原創
838 人瀏覽過

>本博客文章深入研究了Microsoft的Phi-2語言模型,將其性能與其他模型進行了比較,並詳細介紹了其培訓過程。 我們還將使用Transformers庫和擁抱的臉部角色扮演數據集介紹如何訪問和微調PHI-2。 Microsoft的“ PHI”系列中的27億參數模型

PHI-2,儘管大小相對較小,但其目標是最先進的性能。 它採用了一個變壓器體系結構,該體系結構對關注NLP和編碼的合成數據集和Web數據集進行了1.4萬億個代幣的培訓。 與許多較大的型號不同,PHI-2是一個沒有指令微調或RLHF的基本模型。

>

>兩個關鍵方面推動了Phi-2的發展:

  • >高質量培訓數據:優先考慮“教科書 - 質量”數據,包括合成數據集和高價值Web內容,以灌輸常識推理,一般知識和科學理解。
  • 縮放的知識傳遞:
  • >從13億個參數PHI-1.5模型中利用知識來加速培訓並提高基準分數。 有關建立類似LLM的見解,請考慮主LLM概念課程。
  • > phi-2基準
PHI-2超過7B-13B參數模型,例如各種基準(常識推理,語言理解,數學,編碼),例如Llama-2和Mistral。 值得注意的是,在多步推理任務上,它的表現要優於明顯更大的Llama-2-70B。

圖像源

Getting Started with Phi-2

>這種關注較小,易於微調的模型允許在移動設備上部署,從而實現了與大型型號相當的性能。 phi-2甚至超過了Google gemini nano 2在Big Banch,Boolq和MBPP基准上。

圖像源

Getting Started with Phi-2

>訪問Phi-2

>通過擁抱的面孔空間演示探索Phi-2的功能:phi 2在GPU上流式傳輸。 該演示提供了基本的提示響應功能。

> AI新的? AI基礎知識技能軌道是一個很好的起點。

>讓我們使用推理Getting Started with Phi-2 >管道(確保已安裝了最新的

>>)。

使用提示符,調整參數(例如 and 來生成文本。 降價輸出轉換為html。 transformers> transformers PHI-2的輸出令人印象深刻,生成了用解釋的代碼。 > accelerate

!pip install -q -U transformers
!pip install -q -U accelerate

from transformers import pipeline

model_name = "microsoft/phi-2"

pipe = pipeline(
    "text-generation",
    model=model_name,
    device_map="auto",
    trust_remote_code=True,
)
登入後複製
登入後複製

> PHI-2應用

PHI-2的緊湊型尺寸允許在Q&A,代碼生成和基本對話的筆記本電腦和移動設備上使用。

>

>微調PHI-2

本節使用peft進行了

數據集上的微調phi-2。 hieunguyenminh/roleplay

設置和安裝

!pip install -q -U transformers
!pip install -q -U accelerate

from transformers import pipeline

model_name = "microsoft/phi-2"

pipe = pipeline(
    "text-generation",
    model=model_name,
    device_map="auto",
    trust_remote_code=True,
)
登入後複製
登入後複製
導入必要的庫:

from IPython.display import Markdown

prompt = "Please create a Python application that can change wallpapers automatically."

outputs = pipe(
    prompt,
    max_new_tokens=300,
    do_sample=True,
    temperature=0.7,
    top_k=50,
    top_p=0.95,
)
Markdown(outputs[0]["generated_text"])
登入後複製
>定義基本型號,數據集和微調模型名稱的變量:

%%capture
%pip install -U bitsandbytes
%pip install -U transformers
%pip install -U peft
%pip install -U accelerate
%pip install -U datasets
%pip install -U trl
登入後複製
>擁抱臉登錄

>使用您的擁抱臉API令牌登錄。 (用實際的令牌檢索方法替換)。

>

from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    TrainingArguments,
    pipeline,
    logging,
)
from peft import (
    LoraConfig,
    PeftModel,
    prepare_model_for_kbit_training,
    get_peft_model,
)
import os, torch
from datasets import load_dataset
from trl import SFTTrainer
登入後複製

Getting Started with Phi-2

加載數據集

>加載數據集的子集以進行更快的培訓:

base_model = "microsoft/phi-2"
dataset_name = "hieunguyenminh/roleplay"
new_model = "phi-2-role-play"
登入後複製
加載模型和令牌

>加載4位量化模型的內存效率模型:

# ... (Method to securely retrieve Hugging Face API token) ...
!huggingface-cli login --token $secret_hf
登入後複製
添加適配器層

添加洛拉層以進行有效的微調:

dataset = load_dataset(dataset_name, split="train[0:1000]")
登入後複製
培訓

設置培訓參數和sfttrainer:

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,
    quantization_config=bnb_config,
    device_map="auto",
    trust_remote_code=True,
)

model.config.use_cache = False
model.config.pretraining_tp = 1

tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
登入後複製

Getting Started with Phi-2

保存並推動模型

>保存並上傳微調模型:

model = prepare_model_for_kbit_training(model)
peft_config = LoraConfig(
    r=16,
    lora_alpha=16,
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
    target_modules=[
        'q_proj',
        'k_proj',
        'v_proj',
        'dense',
        'fc1',
        'fc2',
    ]
)
model = get_peft_model(model, peft_config)
登入後複製

Getting Started with Phi-2

圖像源

模型評估

評估微調模型:

training_arguments = TrainingArguments(
    output_dir="./results", # Replace with your desired output directory
    num_train_epochs=1,
    per_device_train_batch_size=2,
    gradient_accumulation_steps=1,
    optim="paged_adamw_32bit",
    save_strategy="epoch",
    logging_steps=100,
    logging_strategy="steps",
    learning_rate=2e-4,
    fp16=False,
    bf16=False,
    group_by_length=True,
    disable_tqdm=False,
    report_to="none",
)

trainer = SFTTrainer(
    model=model,
    train_dataset=dataset,
    peft_config=peft_config,
    max_seq_length= 2048,
    dataset_text_field="text",
    tokenizer=tokenizer,
    args=training_arguments,
    packing= False,
)

trainer.train()
登入後複製

Getting Started with Phi-2

結論

本教程提供了Microsoft的PHI-2,其性能,培訓和微調的全面概述。 對這種較小模型進行微調的能力有效地為定制的應用程序和部署打開了可能性。 建議進一步探索使用Langchain之類的框架來構建LLM應用程序。

>

以上是開始使用PHI-2的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板