b2bb2c网站电子商务网站建设前期方案,seo研究院,网页设计公司主要业务,免费设计app软件简介
在阅读理解任务中#xff0c;有一种通过多项选择其中一个答案来训练机器的阅读理解。比如#xff1a;给定一个或多个文档h,以及一个问题S和对应的多个答案候选#xff0c;输出问题S的答案E#xff0c;E是答案候选中的某一个选项。 这样的目的就是通过文档#xff0c…简介
在阅读理解任务中有一种通过多项选择其中一个答案来训练机器的阅读理解。比如给定一个或多个文档h,以及一个问题S和对应的多个答案候选输出问题S的答案EE是答案候选中的某一个选项。 这样的目的就是通过文档问题多个答案选其中一个就是让机器更好文档中的含义提高机器的阅读理解。 是不是感觉似陈相识这不就是语文考试的必考题阅读理解吗。。。。。。。。。。。。。。
给机器训练的数据集示例
如
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/)# 打印训练的前条数据开下接口
examplesc3[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, truncationonly_first, max_length256, paddingmax_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, truncationonly_first, max_length256, paddingmax_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, batchedTrue)# 加载模型
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(predictionspredictions, referenceslabels)# 配置训练参数
args TrainingArguments(output_dir./muliple_choice,per_device_train_batch_size16,per_device_eval_batch_size16,num_train_epochs1,logging_steps50,eval_strategyepoch,save_strategyepoch,load_best_model_at_endTrue,fp16True
) # 创建训练器
trainer Trainer(modelmodel,argsargs,tokenizertokenizer,train_datasettokenized_c3[train],eval_datasettokenized_c3[validation],compute_metricscompute_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, truncationonly_first, max_length256, return_tensorspt)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将数据处理成什么样子大模型才能理解才能去进行正确的训练。