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

第七章 指令微调学习(四)基于指令数据对大语言模型进行微调

第七章 指令微调学习(四)

7.6基于指令数据对大语言模型进行微调

  1. 现在使用指令数据集在已加载的预训练模型上进行微调,需要使用到之前所用的损失计算和训练函数:
    from pre_training import calc_loss_loader from Training_an_LLM_3_16 import train_model_simple
  2. 计算初始训练和验证集的损失
importtorchfrompre_trainingimportcalc_loss_loaderfromDownload_instruction_dataset5_9importtrain_loader,val_loaderfromTraining_an_LLM_3_16importtrain_model_simplefromload_pretrained_model5_20importval_datafromload_pretrained_model5_20importmodelimporttiktoken device=torch.device("cuda"iftorch.cuda.is_available()else"cpu")model.to(device)torch.manual_seed(123)withtorch.no_grad():train_loss=calc_loss_loader(train_loader,model,device,num_batches=5)val_loss=calc_loss_loader(val_loader,model,device,num_batches=5)print("Training loss:",train_loss)print("Validation loss:",val_loss)


3. 训练模型:初始化优化器、设置训练轮数,根据第7.5节中讨论的第一个验证集指令(val_data[0])来定义评估频率及初始上下文,以便在训练过程中评估生成的LLM响应。

importtorchfrompre_trainingimportcalc_loss_loaderfromDownload_instruction_dataset5_9importtrain_loader,val_loaderfromTraining_an_LLM_3_16importtrain_model_simplefromload_pretrained_model5_20importval_datafromload_pretrained_model5_20importmodelimporttiktoken device=torch.device("cuda"iftorch.cuda.is_available()else"cpu")model.to(device)torch.manual_seed(123)withtorch.no_grad():train_loss=calc_loss_loader(train_loader,model,device,num_batches=5)val_loss=calc_loss_loader(val_loader,model,device,num_batches=5)print("Training loss:",train_loss)print("Validation loss:",val_loss)defformat_input(entry):instruction_text=(f"Below is an instruction that describes a task. "f"Write a response that appropriately completes the request."f"\n\n### Instruction:\n{entry['instruction']}")input_text=(f"\n\n### Input:\n{entry['input']}"ifentry["input"]else"")returninstruction_text+input_textimporttime start_time=time.time()torch.manual_seed(123)optimizer=torch.optim.AdamW(model.parameters(),lr=0.00005,weight_decay=0.1)num_epochs=2tokenizer=tiktoken.get_encoding("gpt2")train_losses,val_losses,tokens_seen=train_model_simple(model,train_loader,val_loader,optimizer,device,num_epochs=num_epochs,eval_freq=5,eval_iter=5,start_context=format_input(val_data[0]),tokenizer=tokenizer)end_time=time.time()execution_time_minutes=(end_time-start_time)/60print(f"Training completed in{execution_time_minutes:.2f}minutes.")

结果


损失值的持续下降表明模型遵循指令并生成适当响应的能力正在提升;

最终结果:

Ep 2(Step 000230): Train loss 0.294,Val loss 0.656 Below is an instruction that describes a task.Writea response that appropriately completes the request.### Instruction: Convert the active sentence to passive: 'The chef cooks the meal every day.' ### Response: The meal is cooked every day by the chef.<|endoftext|>The following is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: What is the capital of the United KingdomBelow is an instruction that describes a task.Writea response that appropriately completes the request.### Instruction: Convert the active sentence to passive: 'The chef cooks the meal every day.' ### Response: The meal is cooked every day by the chef.<|endoftext|>The following is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: What is the capital of the United KingdomTraining completed in 13.64 minutes.

训练结果表明:模型学习效果显著——从两个训练周期中持续下降的训练损失和验证损失值即可看出这一点。说明模型逐渐提升了理解并遵循给定指令的能力。(不需要延长至第三个或更多训练周期,可能导致过拟合加剧。)我们将在后续章节更详细地重新评估模型的响应质量。
4. 通过分析训练与验证损失曲线来进一步了解模型的学习过程。
采用与预训练阶段相同的plot_losses函数

importmatplotlib.pyplotaspltfrommatplotlib.tickerimportMaxNLocatordefplot_losses(epochs_seen,tokens_seen,train_losses,val_losses):fig,ax1=plt.subplots(figsize=(5,3))ax1.plot(epochs_seen,train_losses,label="Training loss")ax1.plot(epochs_seen,val_losses,linestyle="-.",label="Validation loss")ax1.set_xlabel("Epochs")ax1.set_ylabel("Loss")ax1.legend(loc="upper right")ax1.xaxis.set_major_locator(MaxNLocator(integer=True))ax2=ax1.twiny()ax2.plot(tokens_seen,train_losses,alpha=0)ax2.set_xlabel("Tokens seen")fig.tight_layout()plt.show()epochs_tensor=torch.linspace(0,num_epochs,len(train_losses))plot_losses(epochs_tensor,tokens_seen,train_losses,val_losses)

从损失曲线图可以看出,模型在训练集和验证集上的性能在整个训练过程中均显著提升。
初始阶段损失值的快速下降表明模型能迅速从数据中学习到有意义的模式与特征表示;随后进入第二个训练周期后,损失值虽持续下降但增速放缓,这说明模型正在优化已学得的特征表示,并逐渐收敛至稳定解。
虽然图中的损失曲线表明模型正在有效训练,但最关键的因素在于其响应质量与正确性的表现。因此,接下来我们需要提取这些响应,并将其存储在一种能够用于评估和量化响应质量的格式中。
总结:已完成指令的微调,并进行了训练和验证损失的的可视化。
整体代码:

importtorchfrompre_trainingimportcalc_loss_loaderfromDownload_instruction_dataset5_9importtrain_loader,val_loaderfromTraining_an_LLM_3_16importtrain_model_simplefromload_pretrained_model5_20importval_datafromload_pretrained_model5_20importmodelimporttiktoken device=torch.device("cuda"iftorch.cuda.is_available()else"cpu")model.to(device)torch.manual_seed(123)withtorch.no_grad():train_loss=calc_loss_loader(train_loader,model,device,num_batches=5)val_loss=calc_loss_loader(val_loader,model,device,num_batches=5)print("Training loss:",train_loss)print("Validation loss:",val_loss)defformat_input(entry):instruction_text=(f"Below is an instruction that describes a task. "f"Write a response that appropriately completes the request."f"\n\n### Instruction:\n{entry['instruction']}")input_text=(f"\n\n### Input:\n{entry['input']}"ifentry["input"]else"")returninstruction_text+input_textimporttime start_time=time.time()torch.manual_seed(123)optimizer=torch.optim.AdamW(model.parameters(),lr=0.00005,weight_decay=0.1)num_epochs=2tokenizer=tiktoken.get_encoding("gpt2")train_losses,val_losses,tokens_seen=train_model_simple(model,train_loader,val_loader,optimizer,device,num_epochs=num_epochs,eval_freq=5,eval_iter=5,start_context=format_input(val_data[0]),tokenizer=tokenizer)end_time=time.time()execution_time_minutes=(end_time-start_time)/60print(f"Training completed in{execution_time_minutes:.2f}minutes.")importmatplotlib.pyplotaspltfrommatplotlib.tickerimportMaxNLocatordefplot_losses(epochs_seen,tokens_seen,train_losses,val_losses):fig,ax1=plt.subplots(figsize=(5,3))ax1.plot(epochs_seen,train_losses,label="Training loss")ax1.plot(epochs_seen,val_losses,linestyle="-.",label="Validation loss")ax1.set_xlabel("Epochs")ax1.set_ylabel("Loss")ax1.legend(loc="upper right")ax1.xaxis.set_major_locator(MaxNLocator(integer=True))ax2=ax1.twiny()ax2.plot(tokens_seen,train_losses,alpha=0)ax2.set_xlabel("Tokens seen")fig.tight_layout()plt.show()epochs_tensor=torch.linspace(0,num_epochs,len(train_losses))plot_losses(epochs_tensor,tokens_seen,train_losses,val_losses)

写给自己:加油!再接再厉!相信自己!

http://www.jsqmd.com/news/868589/

相关文章:

  • AI 矩阵带货怎么做起来?现成系统一站式搭建落地
  • 深入了解指针(3)
  • 泰国双清包税哪家好?泰国清关哪家强?2026泰国海运清关强的公司+泰国陆运清关强的公司合集 - 栗子测评
  • Golang技术周刊 2026年第16周
  • 别再死磕修改了!paperxie 一站式搞定论文查重与降 AIGC 率,毕业党速码
  • 【编号110】64个地级市土地利用图
  • 开源fNIRS脑机接口帽技术解析与应用
  • 2026避雷塔厂家推荐:新疆角钢塔厂家+变电站架构+新疆钢管塔厂家+钢管杆厂家推荐精选 - 栗子测评
  • 2026 小众暴利 AI 项目,AI短剧带货,简单复制就能盈利
  • 开发靠 AI 提效,测试成最大瓶颈,现状过于真实
  • tensorflow:昇腾CANN的TensorFlow适配层
  • Python之anonymate包语法、参数和实际应用案例
  • c#基础知识合集08 随机数 DateTime
  • 衔接器CC Switch 小白图文安装,接入Claude Opus4.7+deekseep V4 +千问等等都不在话下,再也不用担心无法配置几个第三方大模型。
  • 如何重新定义华硕笔记本性能管理:探索G-Helper的轻量化解决方案
  • Cortex-M3/M4处理器模式判断与调试技巧
  • 2026电力金具厂家推荐:铁附件加工厂家+绝缘子厂家推荐名录 - 栗子测评
  • Ollama API 详解(学习笔记)
  • 到底什么是 AI 测试?AI 测试与传统测试的区别?
  • 量子计算与人工智能融合:技术原理与应用前景
  • 魔兽争霸3终极兼容方案:5分钟解决Win10/Win11运行问题
  • Python __slots__ 入门指南
  • 北光恒电:安捷伦DSOS系列示波器(DSOS104/254/404/804)不开机、输出不正常故障排查
  • 2026新疆电力铁塔厂家全梳理:电力铁塔生产厂家+高压铁塔定制厂家+高压输变电塔厂家推荐 - 栗子测评
  • BarrageGrab:构建企业级直播弹幕实时采集系统的技术架构与实践指南
  • 从对话框到具身:AI 交互方式的深层变化
  • A51汇编器Error 21解析与8051开发实践
  • Hermes agent 部署安装windows+D盘超详细步骤
  • 第1章:AI Agent 架构与核心组件
  • CANN 加速库实战:FlashAttention 让大模型推理吞吐翻 3 倍