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

7.5- Loading a pretrained LLM

Chapter 7-Fine-tuning to follow instructions

7.5- Loading a pretrained LLM

  • 开始微调前,我们先加载GPT2模型,加载 3.55 亿参数的中型版本,因为 1.24 亿模型太小,无法通过指令微调获得定性合理的结果

  • 加载 gpt2-medium (355M)

    (注意路径中不要出现中文)

    from gpt_download import download_and_load_gpt2
    from previous_chapters import GPTModel, load_weights_into_gptBASE_CONFIG = {"vocab_size": 50257,     # Vocabulary size"context_length": 1024,  # Context length"drop_rate": 0.0,        # Dropout rate"qkv_bias": True         # Query-key-value bias
    }model_configs = {"gpt2-small (124M)": {"emb_dim": 768, "n_layers": 12, "n_heads": 12},"gpt2-medium (355M)": {"emb_dim": 1024, "n_layers": 24, "n_heads": 16},"gpt2-large (774M)": {"emb_dim": 1280, "n_layers": 36, "n_heads": 20},"gpt2-xl (1558M)": {"emb_dim": 1600, "n_layers": 48, "n_heads": 25},
    }CHOOSE_MODEL = "gpt2-medium (355M)"BASE_CONFIG.update(model_configs[CHOOSE_MODEL])model_size = CHOOSE_MODEL.split(" ")[-1].lstrip("(").rstrip(")")
    settings, params = download_and_load_gpt2(model_size=model_size,models_dir="E:\LLM\gpt2"
    )model = GPTModel(BASE_CONFIG)
    load_weights_into_gpt(model, params)
    model.eval();"""输出"""
    File already exists and is up-to-date: E:\LLM\gpt2\355M\checkpoint
    File already exists and is up-to-date: E:\LLM\gpt2\355M\encoder.json
    File already exists and is up-to-date: E:\LLM\gpt2\355M\hparams.json
    File already exists and is up-to-date: E:\LLM\gpt2\355M\model.ckpt.data-00000-of-00001
    File already exists and is up-to-date: E:\LLM\gpt2\355M\model.ckpt.index
    File already exists and is up-to-date: E:\LLM\gpt2\355M\model.ckpt.meta
    File already exists and is up-to-date: E:\LLM\gpt2\355M\vocab.bpe
    
  • 在下一节开始微调模型之前,让我们看看它在其中一个验证任务上的执行情况

    torch.manual_seed(123)input_text = format_input(val_data[0])
    print(input_text)"""输出"""
    Below is an instruction that describes a task. Write a response that appropriately completes the request.### Instruction:
    Convert the active sentence to passive: 'The chef cooks the meal every day.'
    

    同之前章节的generate函数一样,该函数返回组合的输入和输出文本

    from previous_chapters import (generate,text_to_token_ids,token_ids_to_text
    )token_ids = generate(model=model,idx=text_to_token_ids(input_text, tokenizer),max_new_tokens=35,context_size=BASE_CONFIG["context_length"],eos_id=50256,
    )
    generated_text = token_ids_to_text(token_ids, tokenizer)
    print(generated_text)"""输出"""
    "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\nConvert the active sentence to passive: 'The chef cooks the meal every day.'\n\n### Response:\n\nThe chef cooks the meal every day.\n\n### Instruction:\n\nConvert the active sentence to passive: 'The chef cooks the"
    

    隔离响应,我们可以从 ‘generated_text’ 开始减去指令的长度

    response_text = (generated_text[len(input_text):].replace("### Response:", "").strip()
    )
    print(response_text)"""输出"""
    The chef cooks the meal every day.### Instruction:Convert the active sentence to passive: 'The chef cooks the
    

    正如我们所见,这个模型目前还不能遵循指令。它创建了一个“响应”部分,但只是重复了原始输入句子以及指令。


相关文章:

  • Linux 测试本机与192.168.1.130 主机161/udp端口连通性
  • 数组的常用方法有哪些?
  • vSOME/IP与ETAS DSOME/IP通信的问题解决方案
  • 各个主要目录的功能 / Linux 常见指令
  • python实现基于声音识别的腕带式打鼾干预装置设计与实现
  • 【25.06】FISCOBCOS安装caliper+报错解决
  • 二分查找和二分答案(基础)
  • MATLAB实战:视觉伺服控制实现方案
  • CSS radial-gradient函数详解
  • 金属膜电阻和碳膜电阻
  • Rag技术----项目博客(六)
  • ArkUI-X中Plugin生命周期开发指南
  • SQL进阶之旅 Day 13:CTE与递归查询技术
  • 点云滤波去噪示例2025.6.3
  • MySQL 搜索特定桩号距离之间的数据
  • 计算机操作系统-名词解释
  • 基于MATLAB的FTN调制和硬判决的实现
  • MySQL - Windows 中 MySQL 禁用开机自启,并在需要时手动启动
  • VUE组件库开发 八股
  • MCP:让AI工具协作变得像聊天一样简单 [特殊字符]
  • 小程序加盟代理前景/aso优化排名违法吗
  • 小说盗版网站怎么做的/索引擎优化 seo
  • 建筑室内设计软件/盐城seo营销
  • 网站建设与设计方案/网店运营推广实训
  • 德州做网站最好的公司/做seo必须有网站吗
  • 电子商务网站建设课件/电脑系统优化软件排行榜