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

transformers训练(NLP)阅读理解(多项选择)

简介

在阅读理解任务中,有一种通过多项选择其中一个答案来训练机器的阅读理解。比如:给定一个或多个文档h,以及一个问题S和对应的多个答案候选,输出问题S的答案E,E是答案候选中的某一个选项。
这样的目的就是通过文档,问题,多个答案选其中一个,就是让机器更好文档中的含义,提高机器的阅读理解。
是不是感觉似陈相识,这不就是语文考试的必考题,阅读理解吗。。。。。。。。。。。。。。

给机器训练的数据集示例

如:

  • Context:老师把一个大玻璃瓶子带到学校,瓶子里装着满满的石头、玻璃碎片和沙子。之后,老师请学生把瓶子里的东西都倒出来,然后再装进去,先从沙子开始。每个学生都试了试,最后都发现没有足够的空间装所有的石头。老师指导学生重新装这个瓶子。这次,先从石头开始,最后再装沙子。石头装进去后,沙子就沉积在石头的周围,最后,所有东西都装进瓶子里了。老师说:“如果我们先从小的东西开始,把小东西装进去之后,大的石头就放不进去了。生活也是如此,如果你的生活先被不重要的事挤满了,那你就无法再装进更大、更重要的事了。”
  • Question:正确的装法是,先装?
  • Choices / Candidates:[“小东西”,“大东西”,“轻的东西”,“软的东西” ]
  • Answer:大东西

技术实现思路

多项选择任务,技术实现,这里难点涉及数据处理和训练与推理。其实就是将数据处理好,喂给模型进行训练与推理,让其理解文本。
这里采用格式是:

[CLS] 文本内容 [SEP] 问题 答案 [SEP]

这里涉及在多个候选答案中只取一个答案,让大模型理解文本。所以需要将文本、问题、答案,拆分为4条数据喂给大模型,在告知它正确答案,这样处理大模型才能读懂数据,也是它读取数据逻辑。

代码部分

# 导入包
import evaluate
import numpy as np
from datasets import DatasetDict
from transformers import AutoTokenizer, AutoModelForMultipleChoice, TrainingArguments, Trainer# 读取数据集
c3 = DatasetDict.load_from_disk("./c3/")# 打印训练的前条数据开下接口
examples=c3["train"][:5]print(examples)# 将数据处理为训练和测试
c3.pop("test")# 加载分词器,模型已下载到本地
tokenizer = AutoTokenizer.from_pretrained("chinese-macbert-large")# 对数据集进行处理,验证数据处理阶段
question_choice = []
labels = []
for idx in range(len(examples["context"])):ctx = "\n".join(examples["context"][idx])question = examples["question"][idx]choices = examples["choice"][idx]for choice in choices:context.append(ctx)question_choice.append(question + " " + choice)if len(choices) < 4:for _ in range(4 - len(choices)):context.append(ctx)question_choice.append(question + " " + "不知道")print("========:", choices.index(examples["answer"][idx]))labels.append(choices.index(examples["answer"][idx]))
tokenized_examples = tokenizer(context, question_choice, truncation="only_first", max_length=256, padding="max_length")     # input_ids: 4000 * 256,
for k, v in tokenized_examples.items():print("k:", k, "v:", v)
tokenized_examples = {k: [v[i: i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()}     # 1000 * 4 *256
tokenized_examples["labels"] = labels# 处理数据函数
def process_function(examples):# examples, dict, keys: ["context", "quesiton", "choice", "answer"]# examples, 1000context = []question_choice = []labels = []for idx in range(len(examples["context"])):ctx = "\n".join(examples["context"][idx])question = examples["question"][idx]choices = examples["choice"][idx]for choice in choices:context.append(ctx)question_choice.append(question + " " + choice)if len(choices) < 4:for _ in range(4 - len(choices)):context.append(ctx)question_choice.append(question + " " + "不知道")labels.append(choices.index(examples["answer"][idx]))# 使用分词器,对数据进行分词处理    tokenized_examples = tokenizer(context, question_choice, truncation="only_first", max_length=256, padding="max_length")     # input_ids: 4000 * 256,tokenized_examples = {k: [v[i: i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()}     # 1000 * 4 *256tokenized_examples["labels"] = labelsreturn tokenized_examples# 处理数据
tokenized_c3 = c3.map(process_function, batched=True)# 加载模型
model = AutoModelForMultipleChoice.from_pretrained("chinese-macbert-large")# 创建评估函数
accuracy = evaluate.load("accuracy")def compute_metric(pred):predictions, labels = predpredictions = np.argmax(predictions, axis=-1)return accuracy.compute(predictions=predictions, references=labels)# 配置训练参数
args = TrainingArguments(output_dir="./muliple_choice",per_device_train_batch_size=16,per_device_eval_batch_size=16,num_train_epochs=1,logging_steps=50,eval_strategy="epoch",save_strategy="epoch",load_best_model_at_end=True,fp16=True
) # 创建训练器 
trainer = Trainer(model=model,args=args,tokenizer=tokenizer,train_dataset=tokenized_c3["train"],eval_dataset=tokenized_c3["validation"],compute_metrics=compute_metric
)  # 模型训练
trainer.train()# 模型预测
from typing import Any
import torchclass MultipleChoicePipeline:def __init__(self, model, tokenizer) -> None:self.model = modelself.tokenizer = tokenizerself.device = model.devicedef preprocess(self, context, quesiton, choices):cs, qcs = [], []for choice in choices:cs.append(context)qcs.append(quesiton + " " + choice)return tokenizer(cs, qcs, truncation="only_first", max_length=256, return_tensors="pt")def predict(self, inputs):inputs = {k: v.unsqueeze(0).to(self.device) for k, v in inputs.items()}return self.model(**inputs).logitsdef postprocess(self, logits, choices):predition = torch.argmax(logits, dim=-1).cpu().item()return choices[predition]def __call__(self, context, question, choices) -> Any:inputs = self.preprocess(context, question, choices)logits = self.predict(inputs)result = self.postprocess(logits, choices)return resultpipe = MultipleChoicePipeline(model, tokenizer)
pipe("小明在北京上班", "小明在哪里上班?", ["北京", "上海", "河北", "海南", "河北", "海南"])

以上就是完整多项选择阅读理解的大模型训练代码

问题

1,这里的难点就是多项选择如何让大模型进行阅读理解训练思想,这里参考的就是语文里的阅读理解。
2,将数据处理成什么样子,大模型才能理解,才能去进行正确的训练。

http://www.lryc.cn/news/493741.html

相关文章:

  • 微软企业邮箱:安全可靠的企业级邮件服务!
  • 什么是分布式锁
  • 【含开题报告+文档+PPT+源码】基于SpringBoot的艺术培训学校管理系统的设计与实现
  • 【网络安全 | 漏洞挖掘】绕过SAML认证获得管理员面板访问权限
  • Flutter:列表分页,上拉加载下拉刷新,在GetBuilder模板使用方式
  • 硬件基础22 反馈放大电路
  • 挑战用React封装100个组件【001】
  • linux高级系统编程之进程
  • nextjs+nestjs+prisma写todolist全栈项目
  • 基于Matlab的图像去噪算法仿真
  • Docker pull镜像拉取失败
  • fastjson不出网打法—BCEL链
  • vue2 中使用 Ag-grid-enterprise 企业版
  • Redis开发03:常见的Redis命令
  • 研0找实习【学nlp】14--BERT理解
  • mysql之基本常用的语法
  • 基于Linux的patroni搭建标准
  • 2024年第十三届”认证杯“数学中国数学建模国际赛(小美赛)
  • Unity类银河战士恶魔城学习总结(P149 Screen Fade淡入淡出菜单)
  • (四)3D视觉机器人的手眼标定(眼在手外)
  • 安达发|制造业APS智能优化排产软件的四类制造模型解决方案
  • 命令行使用ssh隧道连接远程mysql
  • 力扣第 71 题 简化路径
  • 使用ENSP实现OSPF
  • 分布式下怎么优化处理数据,怎么代替Join
  • 51单片机快速入门之中断的应用 2024/11/23 串口中断
  • [Java]微服务配置管理
  • c/c++ 用easyx图形库写一个射击游戏
  • Rust eyre 错误处理实战教程
  • 面试小札:JVM虚拟机