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

关注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。
  # Tokenize
  input_ids = tokenizer.encode(
          text,
          return_tensors="pt",
          truncation=True,
          max_length=max_input_tokens
  )

  # 生成:获取模型所在设备(model.device),并使用模型生成文本。
  # generate 函数基于输入 token 序列 input_ids 生成文本,生成的 token 总数不超过 max_output_tokens
  # Generate
  device = model.device
  generated_tokens_with_prompt = model.generate(
    input_ids=input_ids.to(device),
    max_length=max_output_tokens
  )

  # 解码:通过分词器 tokenizer 将生成的 token 序列转回可读的文本。batch_decode 函数将生成的 token 解码为字符串,并跳过特殊 token(如 <pad> 等)
  # Decode
  generated_text_with_prompt = tokenizer.batch_decode(generated_tokens_with_prompt, skip_special_tokens=True)

  # 剥离输入提示:由于生成的文本包含了输入文本(prompt),所以通过截取去除输入文本部分,仅保留生成的输出文本
  # Strip the prompt
  generated_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

### 吴恩达关于LLM大模型的研究与资源 吴恩达作为机器学习领域的重要人物之一,在大语言模型(LLM)及相关技术方面也有一定的贡献和研究。以下是与其相关的课程、资料和论文的内容概述: #### 1. **吴恩达的DeepLearning.ai平台** 吴恩达通过其创立的教育平台DeepLearning.ai提供了许多高质量的人工智能课程,其中包括部分涉及大模型的技术内容。虽然这些课程可能并未专门针对LLM展开,但它们为理解大模型背后的理论和技术奠定了坚实的基础[^3]。 #### 2. **推荐的学习路径** 对于希望深入了解LLM及其工程化实践的人来说,可以参考GitHub上的某些开源项目笔记,其中提到了一系列优质的学习资源。这些资源不仅覆盖了基础知识,还包含了前沿算法和架构的设计思路。尽管这些并非由吴恩达本人编写,但他所倡导的学习理念与此类材料高度契合。 #### 3. **评估工具与框架** 在实际应用中,为了更好地理解和优化LLM的表现,研究人员通常会借助特定的评估工具或框架来进行分析。例如,有教程展示了如何利用链式操作来测试不同LLM的能力差异[^4]。这种实践经验能够补充传统教学中的不足之处。 #### 4. **具体论文方向** 目前尚未发现明确标注为“吴恩达 LLM”的独立研究成果;不过在其参与指导的学生团队以及合作机构发表的工作里,确实存在不少探讨自然语言处理(NLP)及大规模预训练模型主题的文章。感兴趣者可以通过查阅斯坦福大学CS229等相关课程主页获取更多信息。 ```python # 示例代码展示如何加载并初步探索一个简单的NLP任务 import transformers as trfms model_name = 'bert-base-uncased' tokenizer = trfms.AutoTokenizer.from_pretrained(model_name) model = trfms.AutoModelForSequenceClassification.from_pretrained(model_name) text_example = ["This is an example sentence."] input_ids = tokenizer(text_example, return_tensors="pt")['input_ids'] outputs = model(**input_ids) print(outputs.logits) ``` 上述脚本片段仅用于演示目的,并不代表任何具体的学术成果关联性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值