输入模型的训练数据需要变成什么样(基于bert模型)

一 问题定义

使用Hugging Face transformers 库中的的 Trainer 工具( 旨在简化和标准化 NLP(自然语言处理)模型的训练过程。它抽象了训练循环中的大量样板代码,让开发者能够以更少的代码实现强大的训练功能。)进行模型训练,其基本代码如下:

python 复制代码
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized["train"],
    eval_dataset=tokenized["validation"],
    tokenizer=tokenizer,
    data_collator=data_collator, 
    compute_metrics=compute_metrics,
)
trainer.train()

通过参数 train_dataset 和 eval_dataset 传递模型的训练数据和验证数据,那模型所需要的训练数据 tokenized [" train "] 和 tokenized [" validation "] 到底被构建成了什么样被输入?

二 自然语言中的分词

使用的数据集 英文 sst2

python 复制代码
dataset = load_dataset("glue", "sst2")

该数据集合中的10个数据示例如下所示:

python 复制代码
{'sentence': ['hide new secretions from the parental units ',
  'contains no wit , only labored gags ',
  'that loves its characters and communicates something rather beautiful about human nature ',
  'remains utterly satisfied to remain the same throughout ',
  'on the worst revenge-of-the-nerds clichés the filmmakers could dredge up ',
  "that 's far too tragic to merit such superficial treatment ",
  'demonstrates that the director of such hollywood blockbusters as patriot games can still turn out a small , personal film with an emotional wallop . ',
  'of saucy ',
  "a depressed fifteen-year-old 's suicidal poetry ",
  "are more deeply thought through than in most ` right-thinking ' films "],
 'label': [0, 0, 1, 0, 0, 0, 1, 1, 0, 1],
 'idx': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}

每一个训练数据包含着句子(sentence),句子对应的标签(label)以及该数据的坐标位置(idx)。

对于模型来说,其输入不能是一句话,计算器看不懂一句话,它只能看懂数字,所以我们需要将句子进行数字化之后才能输入模型。其将句子数字化的过程就是自然语言处理中的文本向量化

在向量化之前还需要考虑一个事情,我们向量化,难道是使用一个向量来代表每一句话吗?答案是否定的,会进行相应的分词操作,比如 "我喜欢你" 这句话进行分词操作的话,也许得到的分词结果就是 - 我 - 喜 - 欢 - 你 - 这四个字,然后每个分词对应一个向量,"我喜欢你" 这句话进行向量化的结果可能就是 [ [0.2,0.4,1.0], [0.5,1.0,1.0], [0.4,0.6,1.0], [0.2,0.5,1.0] ],"我"被向量化后就是[0.2,0.4,1.0]。

在transformers 库中已经有了特定的分词器,中文 英文 日语 等不同分词器,其分词器内部包括着所有某种语言所有分词片段向量化后的结果。以中文进行举例,中文中其一个分词(one token)几乎就是一个汉字,每个汉字都对应着一个向量,至于这个向量多少维根据分词器的不同则不同。

python 复制代码
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", use_fast=True)

以上就是加载分词器,加载的是bert处理英文数据的分词器,tokenizer 就是一个分词器。将待输入模型的数据依次进行分词操作。

python 复制代码
def preprocess(batch):
    return tokenizer(batch["sentence"], truncation=True, padding=False, max_length=128)

tokenized = dataset.map(preprocess, batched=True)

示例:

  • res = tokenizer("I love you")
  • 分词后的tokens列表: ['[CLS]', 'i', 'love', 'you', '[SEP]']
  • 对应数字ID(input_ids): [101, 1045, 2293, 2017, 102]
  • 注意力掩码(attention_mask): [1, 1, 1, 1, 1]

其中的 1045 表示 "I" 这个 token 在这个 tokenizer 的词表的第1045个向量。

经过 tokenizer 向量化过后的数据再经 map 处理就可将数据处理成可直接输入Trainer的数据。其数据会被处理成一下结构

python 复制代码
Dataset({
    features: ['input_ids', 'attention_mask', 'token_type_ids', 'label'],
    num_rows: 10000  # 你的数据量
})
相关推荐
会飞的老朱1 小时前
医药集团数智化转型,智能综合管理平台激活集团管理新效能
大数据·人工智能·oa协同办公
聆风吟º3 小时前
CANN runtime 实战指南:异构计算场景中运行时组件的部署、调优与扩展技巧
人工智能·神经网络·cann·异构计算
Codebee5 小时前
能力中心 (Agent SkillCenter):开启AI技能管理新时代
人工智能
聆风吟º5 小时前
CANN runtime 全链路拆解:AI 异构计算运行时的任务管理与功能适配技术路径
人工智能·深度学习·神经网络·cann
uesowys6 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
AI_56786 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
User_芊芊君子6 小时前
CANN大模型推理加速引擎ascend-transformer-boost深度解析:毫秒级响应的Transformer优化方案
人工智能·深度学习·transformer
智驱力人工智能6 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
qq_160144876 小时前
亲测!2026年零基础学AI的入门干货,新手照做就能上手
人工智能
Howie Zphile6 小时前
全面预算管理难以落地的核心真相:“完美模型幻觉”的认知误区
人工智能·全面预算