>本博客文章深入研究了Microsoft的Phi-2语言模型,将其性能与其他模型进行了比较,并详细介绍了其培训过程。 我们还将使用Transformers库和拥抱的脸部角色扮演数据集介绍如何访问和微调PHI-2。 Microsoft的“ PHI”系列中的27亿参数模型
PHI-2,尽管大小相对较小,但其目标是最先进的性能。 它采用了一个变压器体系结构,该体系结构对关注NLP和编码的合成数据集和Web数据集进行了1.4万亿个代币的培训。 与许多较大的型号不同,PHI-2是一个没有指令微调或RLHF的基本模型。>
>两个关键方面推动了Phi-2的发展:
图像源
图像源
>通过拥抱的面孔空间演示探索Phi-2的功能:phi 2在GPU上流式传输。 该演示提供了基本的提示响应功能。
> AI新的? AI基础知识技能轨道是一个很好的起点。
>让我们使用推理>管道(确保已安装了最新的
使用提示符,调整参数(例如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。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>拥抱脸登录
>
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
base_model = "microsoft/phi-2" dataset_name = "hieunguyenminh/roleplay" new_model = "phi-2-role-play"加载模型和令牌
# ... (Method to securely retrieve Hugging Face API token) ... !huggingface-cli login --token $secret_hf添加适配器层
dataset = load_dataset(dataset_name, split="train[0:1000]")培训
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
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)
图像源
模型评估
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()
>
以上是开始使用PHI-2的详细内容。更多信息请关注PHP中文网其他相关文章!