输入模型的训练数据需要变成什么样(基于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  # 你的数据量
})
相关推荐
测试员周周3 小时前
【Appium 系列】第16节-WebView-H5上下文切换 — 混合应用的自动化难点
运维·开发语言·人工智能·功能测试·appium·自动化·测试用例
K姐研究社4 小时前
怎么用AI制作电商口播视频,开拍APP一键生成
人工智能·音视频
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
传说故事5 小时前
【论文阅读】MotuBrain: An Advanced World Action Model for Robot Control
论文阅读·人工智能·具身智能·wam
北京耐用通信6 小时前
全域适配工业场景耐达讯自动化Modbus TCP 转 PROFIBUS 网关轻松实现以太网与现场总线互通
网络·人工智能·网络协议·自动化·信息与通信
火山引擎开发者社区6 小时前
TRAE × 火山引擎 Supabase:为你的 AI 应用装上“数据引擎”
人工智能
weixin_446260856 小时前
[特殊字符] 视觉Transformer (ViT) 原理及性能突破:从CNN到大规模自注意力机制的迁移
深度学习·cnn·transformer
小a彤6 小时前
GE 在 CANN 五层架构中的位置
人工智能·深度学习·transformer
前端若水6 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Upsy-Daisy7 小时前
AI Agent 项目学习笔记(八):Tool Calling 工具调用机制总览
人工智能·笔记·学习