【LLM】吴恩达『微调大模型』代码笔记(03_Instruction_tuning_lab_student)【如何进行推断函数定义】

news/2024/9/21 22:00:39/

关注B站可以观看更多实战教学视频:hallo128的个人空间

【LLM】吴恩达『微调大模型』代码笔记(03_Instruction_tuning_lab_student)

指令微调(代码详解-代码及输出结果)

1. 推断函数定义

# 通过 AutoTokenizer 从预训练模型 EleutherAI/pythia-70m 加载分词器(tokenizer)。
# 通过 AutoModelForCausalLM 从同样的预训练模型 EleutherAI/pythia-70m 加载因果语言模型(Causal LM),该模型用于生成文本
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pythia-70m")
model = AutoModelForCausalLM.from_pretrained("EleutherAI/pythia-70m")# 定义一个函数 inference,用于进行推理。
# 参数包括
# 输入文本 text、模型 model、分词器 tokenizer,以及两个可选参数:
#  max_input_tokens 限制输入最大 token 数,max_output_tokens 限制输出最大 token 数
def inference(text, model, tokenizer, max_input_tokens=1000, max_output_tokens=100):# 分词:通过分词器 tokenizer 将输入文本 text 转换为 token 序列。# 使用 encode 函数将其转换为 PyTorch 的张量格式(return_tensors="pt"),并对输入进行截断(truncation=True),保证输入的 token 数量不超过 max_input_tokens。# Tokenizeinput_ids = tokenizer.encode(text,return_tensors="pt",truncation=True,max_length=max_input_tokens)# 生成:获取模型所在设备(model.device),并使用模型生成文本。# generate 函数基于输入 token 序列 input_ids 生成文本,生成的 token 总数不超过 max_output_tokens# Generatedevice = model.devicegenerated_tokens_with_prompt = model.generate(input_ids=input_ids.to(device),max_length=max_output_tokens)# 解码:通过分词器 tokenizer 将生成的 token 序列转回可读的文本。batch_decode 函数将生成的 token 解码为字符串,并跳过特殊 token(如 <pad> 等)# Decodegenerated_text_with_prompt = tokenizer.batch_decode(generated_tokens_with_prompt, skip_special_tokens=True)# 剥离输入提示:由于生成的文本包含了输入文本(prompt),所以通过截取去除输入文本部分,仅保留生成的输出文本# Strip the promptgenerated_text_answer = generated_text_with_prompt[0][len(text):]# 返回生成的文本:函数返回生成的文本结果(不包含输入提示的部分)return generated_text_answer

2. 加载数据集

# 加载微调数据集:通过 load_dataset 函数加载指定路径 "lamini/lamini_docs" 下的微调数据集 finetuning_dataset,并打印出来
finetuning_dataset_path = "lamini/lamini_docs"
finetuning_dataset = load_dataset(finetuning_dataset_path)
print(finetuning_dataset)

DatasetDict({
train: Dataset({
features: [‘question’, ‘answer’, ‘input_ids’, ‘attention_mask’, ‘labels’],
num_rows: 1260
})
test: Dataset({
features: [‘question’, ‘answer’, ‘input_ids’, ‘attention_mask’, ‘labels’],
num_rows: 140
})
})

# 获取数据集中的一个测试样本:从测试数据集中获取第一个样本 test_sample,并打印该样本内容。
test_sample = finetuning_dataset["test"][0]
print(test_sample)

{‘question’: ‘Can Lamini generate technical documentation or user manuals for software projects?’, ‘answer’: ‘Yes, Lamini can generate technical documentation and user manuals for software projects. It uses natural language generation techniques to create clear and concise documentation that is easy to understand for both technical and non-technical users. This can save developers a significant amount of time and effort in creating documentation, allowing them to focus on other aspects of their projects.’, ‘input_ids’: [5804, 418, 4988, 74, 6635, 7681, 10097, 390, 2608, 11595, 84, 323, 3694, 6493, 32, 4374, 13, 418, 4988, 74, 476, 6635, 7681, 10097, 285, 2608, 11595, 84, 323, 3694, 6493, 15, 733, 4648, 3626, 3448, 5978, 5609, 281, 2794, 2590, 285, 44003, 10097, 326, 310, 3477, 281, 2096, 323, 1097, 7681, 285, 1327, 14, 48746, 4212, 15, 831, 476, 5321, 12259, 247, 1534, 2408, 273, 673, 285, 3434, 275, 6153, 10097, 13, 6941, 731, 281, 2770, 327, 643, 7794, 273, 616, 6493, 15], ‘attention_mask’: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ‘labels’: [5804, 418, 4988, 74, 6635, 7681, 10097, 390, 2608, 11595, 84, 323, 3694, 6493, 32, 4374, 13, 418, 4988, 74, 476, 6635, 7681, 10097, 285, 2608, 11595, 84, 323, 3694, 6493, 15, 733, 4648, 3626, 3448, 5978, 5609, 281, 2794, 2590, 285, 44003, 10097, 326, 310, 3477, 281, 2096, 323, 1097, 7681, 285, 1327, 14, 48746, 4212, 15, 831, 476, 5321, 12259, 247, 1534, 2408, 273, 673, 285, 3434, 275, 6153, 10097, 13, 6941, 731, 281, 2770, 327, 643, 7794, 273, 616, 6493, 15]}

3.进行推断

  • 问题: ‘Can Lamini generate technical documentation or user manuals for software projects?’,
  • 原问题的答案: ‘Yes, Lamini can generate technical documentation and user manuals for software projects. It uses natural language generation techniques to create clear and concise documentation that is easy to understand for both technical and non-technical users. This can save developers a significant amount of time and effort in creating documentation, allowing them to focus on other aspects of their projects.’
# 通过 inference 函数对测试样本中的问题 test_sample["question"] 进行推理,打印生成的答案
print(inference(test_sample["question"], model, tokenizer))

I have a question about the following:

How do I get the correct documentation to work?

A:

I think you need to use the following code:

A:

You can use the following code to get the correct documentation.

A:

You can use the following code to get the correct documentation.

A:

You can use the following


http://www.ppmy.cn/news/1528572.html

相关文章

TCP四大拥塞控制算法总结

四大算法&#xff1a;1.慢启动&#xff0c;2.拥塞避免&#xff0c;3.拥塞发生&#xff0c;4.快速恢复。 慢启动&#xff1a; 首先连接建好的开始先初始化拥塞窗口cwnd大小为1&#xff0c;表明可以传一个MSS大小的数据。 每当收到一个ACK&#xff0c;cwnd大小加一&#xff0c…

二级C语言2024-3易错题

1 结构 一个C语言程序是由&#xff08; &#xff09;。 A. 一个主程序和若干子程序组成 B. 函数组成 C. 若干过程组成 D. 若干子程序组成 一个C语言程序是由多个部分组成的&#xff0c;其中最核心的部分是函数。在C语言中&#xff0c;函数是实现特定功能的代码块&#xff0c;…

人才有约,职为你:颐年集团携手粤荣学校共绘养老行业的美好未来

摘要&#xff1a;广州市白云区粤荣职业培训学校成功举办“人才有约&#xff0c;职为你”颐年集团养老机构线下招聘会 2024年9月19日下午2点30分&#xff0c;广州市白云区金骊城二楼热闹非凡。粤荣职业培训学校携手颐年集团&#xff0c;共同举办了主题为“人才有约&#xff0c;…

Redis基本命令详解

1. 基本命令 命令不区分大小写&#xff0c;而key是区分大小写的 # select 数据库间的切换 数据库共计16个 127.0.0.1:6379> select 1# dbsize 返回当前数据库的 key 的数量 127.0.0.1:6379[1]> dbsize# keys * 查看数据库所有的key 127.0.0.1:6379[1]> keys *# fl…

前端web端项目运行的时候没有ip访问地址

我们发现 没有netWork 的地址 导致 团队内其他同学无法打开我们的地址 进行访问 在page.json 中的运行 指令中 添加 --host 记得加上空格 这样我们就可以看到这个地址了 团队其他同学 就可以访问我们这个地址了

今日leetCode 18. 四数之和

18. 四数之和 给你一个由 n 个整数组成的数组 nums &#xff0c;和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] &#xff08;若两个四元组元素一一对应&#xff0c;则认为两个四元组重复&#xff09;&#xff…

【MPC】无人机模型预测控制复现Data-Driven MPC for Quadrotors项目(Part 1)

无人机模型预测控制复现Data-Driven MPC for Quadrotors项目 参考链接背景和问题方法与贡献实验结果安装ROS创建工作空间下载RotorS仿真器源码和依赖创建Python虚拟环境下载data_driven_mpc仓库代码下载并配置ACADO求解器下载并配置ACADO求解器的Python接口下载并配置rpg_quadr…

pip install、yum install和conda install三者技术区分

pip install、yum install和conda install在安装系统环境时可以从以下几个方面进行区分选择&#xff1a; 一、适用范围 pip install 主要用于安装 Python 包。适用于 Python 项目中特定的库和工具的安装。如果你的项目是纯 Python 开发&#xff0c;并且需要安装各种 Python 库&…