当前位置: 首页 > wzjs >正文

uv推广平台上海seo优化bwyseo

uv推广平台,上海seo优化bwyseo,自己做提卡网站,营销型网站建设软件说明 unsloth框架可以进行各种sft训练,包括lora和grpo训练。我参考官方方法,使用模型Qwen2.5-3B-Instruct和数据集gsm8k,写了一个grpo训练的例子。 代码 这个代码加载模型Qwen2.5-3B-Instruct和数据集gsm8k。训练完成后先保存lora模型然后…

说明

unsloth框架可以进行各种sft训练,包括lora和grpo训练。我参考官方方法,使用模型Qwen2.5-3B-Instruct和数据集gsm8k,写了一个grpo训练的例子。

代码

这个代码加载模型Qwen2.5-3B-Instruct和数据集gsm8k。训练完成后先保存lora模型然后保存合并后的模型。

import os
from unsloth import FastLanguageModel, PatchFastRL
from unsloth import is_bfloat16_supported
import torch
import re
from datasets import load_dataset, DatasetPatchFastRL("GRPO", FastLanguageModel)
from trl import GRPOConfig, GRPOTrainermax_seq_length = 1024 # Can increase for longer reasoning traces
lora_rank = 64 # Larger rank = smarter, but slowerprint(">>>>>>>>>>>>>>>FastLanguageModel.from_pretrained:")
model, tokenizer = FastLanguageModel.from_pretrained(model_name = "./Qwen2.5-3B-Instruct",max_seq_length = max_seq_length,load_in_4bit = True, # False for LoRA 16bitfast_inference = True, # Enable vLLM fast inferencemax_lora_rank = lora_rank,gpu_memory_utilization = 0.8, # Reduce if out of memory
)print(">>>>>>>>>>>>>>>FastLanguageModel.get_peft_model:")
model = FastLanguageModel.get_peft_model(model,r = lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128target_modules = ["q_proj", "k_proj", "v_proj", "o_proj","gate_proj", "up_proj", "down_proj",], # Remove QKVO if out of memorylora_alpha = lora_rank,use_gradient_checkpointing = "unsloth", # Enable long context finetuningrandom_state = 3407,
)# Load and prep dataset
SYSTEM_PROMPT = """
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""XML_COT_FORMAT = """\
<reasoning>
{reasoning}
</reasoning>
<answer>
{answer}
</answer>
"""def extract_xml_answer(text: str) -> str:answer = text.split("<answer>")[-1]answer = answer.split("</answer>")[0]return answer.strip()def extract_hash_answer(text: str) -> str | None:if "####" not in text:return Nonereturn text.split("####")[1].strip()# uncomment middle messages for 1-shot prompting
def get_gsm8k_questions(split = "train") -> Dataset:print(f">>>>>>>>>>>>>>>_get_gsm8k_questions, split:{split}")data = load_dataset('./gsm8k', 'main')[split] # type: ignoredata = data.map(lambda x: { # type: ignore'prompt': [{'role': 'system', 'content': SYSTEM_PROMPT},{'role': 'user', 'content': x['question']}],'answer': extract_hash_answer(x['answer'])}) # type: ignorereturn data # type: ignoreprint(">>>>>>>>>>>>>>>get_gsm8k_questions:")
dataset = get_gsm8k_questions()# Reward functions
def correctness_reward_func(prompts, completions, answer, **kwargs) -> list[float]:responses = [completion[0]['content'] for completion in completions]q = prompts[0][-1]['content']extracted_responses = [extract_xml_answer(r) for r in responses]print('-'*20, f"Question:\n{q}", f"\nAnswer:\n{answer[0]}", f"\nResponse:\n{responses[0]}", f"\nExtracted:\n{extracted_responses[0]}")return [2.0 if r == a else 0.0 for r, a in zip(extracted_responses, answer)]def int_reward_func(completions, **kwargs) -> list[float]:responses = [completion[0]['content'] for completion in completions]extracted_responses = [extract_xml_answer(r) for r in responses]return [0.5 if r.isdigit() else 0.0 for r in extracted_responses]def strict_format_reward_func(completions, **kwargs) -> list[float]:"""Reward function that checks if the completion has a specific format."""pattern = r"^<reasoning>\n.*?\n</reasoning>\n<answer>\n.*?\n</answer>\n$"responses = [completion[0]["content"] for completion in completions]matches = [re.match(pattern, r) for r in responses]return [0.5 if match else 0.0 for match in matches]def soft_format_reward_func(completions, **kwargs) -> list[float]:"""Reward function that checks if the completion has a specific format."""pattern = r"<reasoning>.*?</reasoning>\s*<answer>.*?</answer>"responses = [completion[0]["content"] for completion in completions]matches = [re.match(pattern, r) for r in responses]return [0.5 if match else 0.0 for match in matches]def count_xml(text) -> float:count = 0.0if text.count("<reasoning>\n") == 1:count += 0.125if text.count("\n</reasoning>\n") == 1:count += 0.125if text.count("\n<answer>\n") == 1:count += 0.125count -= len(text.split("\n</answer>\n")[-1])*0.001if text.count("\n</answer>") == 1:count += 0.125count -= (len(text.split("\n</answer>")[-1]) - 1)*0.001return countdef xmlcount_reward_func(completions, **kwargs) -> list[float]:contents = [completion[0]["content"] for completion in completions]return [count_xml(c) for c in contents]print(">>>>>>>>>>>>>>>training_args:")
training_args = GRPOConfig(use_vllm = True, # use vLLM for fast inference!learning_rate = 5e-6,adam_beta1 = 0.9,adam_beta2 = 0.99,weight_decay = 0.1,warmup_ratio = 0.1,lr_scheduler_type = "cosine",optim = "adamw_8bit",logging_steps = 10,bf16 = is_bfloat16_supported(),fp16 = not is_bfloat16_supported(),per_device_train_batch_size = 1,gradient_accumulation_steps = 1, # Increase to 4 for smoother trainingnum_generations = 8, # Decrease if out of memorymax_prompt_length = 300,max_completion_length = 300,# num_train_epochs = 1, # Set to 1 for a full training runmax_steps = 100,save_steps = 50,max_grad_norm = 0.1,report_to = "none", # Can use Weights & Biasesoutput_dir = "outputs_2",
)
print(f"training_args:{training_args}")print(">>>>>>>>>>>>>>>GRPOTrainer:")
trainer = GRPOTrainer(model = model,processing_class = tokenizer,reward_funcs = [xmlcount_reward_func,soft_format_reward_func,strict_format_reward_func,int_reward_func,correctness_reward_func,],args = training_args,train_dataset = dataset,
)
print(">>>>>>>>>>>>>>>trainer.train:")
trainer.train()print(">>>>>>>>>>>>>>>model.save_lora:")
model.save_lora("grpo_saved_lora_2")print(">>>>>>>>>>>>>>>model.save_lora:")
model.save_pretrained_merged("./model_merged", tokenizer, save_method = "merged_16bit",)print(">>>>>>>>>>>>>>>DONE<<<<<<<<<<<<<<")

参考消息

使用unsloth进行grpo训练报错及解决方法

http://www.dtcms.com/wzjs/91721.html

相关文章:

  • 如何给客户做网站方案百度快照优化排名
  • 临安市规划建设局网站色盲测试卡
  • wordpress 下载站插件网页界面设计
  • 网站维护基础知识石家庄seo报价
  • 网站做app安全吗怎么在网上做推广
  • 做网站和微信公众号需要多少钱石家庄关键词快速排名
  • 网站服务器vps磁力狗在线
  • 初学者学做网站用什么软件品牌咨询
  • 重庆网站建设设计公司信息武汉百度推广优化
  • 医疗电子的网站建设龙南黄页全部电话
  • 青之峰网站建设武汉大学人民医院院长
  • 丹东淘宝做网站社区建站网站系统
  • 网站建设品牌推荐百度网盘官网登录首页
  • 松江城乡建设委员会的网站自媒体发布软件app
  • 高中信息技术网站建设淘宝关键词查询
  • 上海app开发外包安卓优化大师app下载安装
  • 网站设计与网页配色实例精讲烟台百度推广公司
  • 洪雅网站建设电商怎么做新手入门
  • 镇江网站设计多少钱百度推广电话号码
  • 企业邮箱注册申请需要付费吗上海企业seo
  • 北京做网站建设的公司排名cps推广
  • 网站的结构怎么做广告投放运营主要做什么
  • wordpress3.0手机版关键词优化哪家强
  • 最新网站推广方法项目推广计划书
  • 郑州模板建站哪家好代做百度首页排名价格
  • 天津网站建设推广长沙seo优化价格
  • 求邯郸网站制作互联网销售模式
  • 响应式企业网站开发所用的平台seo外链是什么
  • 如何提高网站安全百度6大核心部门
  • wordpress 页脚链接seo公司是做什么的