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

NLP的预处理数据

处理文本数据的主要工具是Tokenizer。Tokenizer根据一组规则将文本拆分为tokens然后将这些tokens转换为数字,然后转换为张量,成为模型的输入。模型所需的任何附加输入都由Tokenizer添加。

如果您计划使用预训练模型,重要的是使用与之关联的预训练Tokenizer。这确保文本的拆分方式与预训练语料库相同,并在预训练期间使用相同的标记-索引的对应关系(通常称为词汇表-vocab)。

开始使用AutoTokenizer.from_pretrained()方法加载一个预训练tokenizer这将下载模型预训练的vocab

from transformers import AutoTokenizer
​
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")

然后将您的文本传递给tokenizer

encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.")
print(encoded_input)
{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102],
 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

tokenizer返回一个包含三个重要对象的字典:

  • input_ids 是与句子中每个token对应的索引。

  • attention_mask 指示是否应该关注一个token

  • token_type_ids 在存在多个序列时标识一个token属于哪个序列。

通过解码 input_ids 来返回您的输入:

tokenizer.decode(encoded_input["input_ids"])

如您所见,tokenizer向句子中添加了两个特殊token - CLSSEP(分类器和分隔符)。并非所有模型都需要特殊token,但如果需要,tokenizer会自动为您添加。

如果有多个句子需要预处理,将它们作为列表传递给tokenizer

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")

batch_sentences = [
    ["But what about second breakfast?","i am a sentence"],
    "Don't think he knows about second breakfast, Pip.",
    "What about elevensies?",
]
encoded_input = tokenizer(batch_sentences, padding=True, truncation = True)
print(encoded_input)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 178, 1821, 170, 5650, 
102, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], 
'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 
'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}

注意token_type_ids在上面的例子中有体现。101与102是CLS与SEP的id,对应句子的开始与结束。 

1.2.3.1.1 填充

句子的长度并不总是相同,这可能会成为一个问题,因为模型输入的张量需要具有统一的形状。填充是一种策略,通过在较短的句子中添加一个特殊的padding token,以确保张量是矩形的。

padding 参数设置为 True,以使批次中较短的序列填充到与最长序列相匹配的长度:

batch_sentences = [
    "But what about second breakfast?",
    "Don't think he knows about second breakfast, Pip.",
    "What about elevensies?",
]
encoded_input = tokenizer(batch_sentences, padding=True)
print(encoded_input)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
               [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
               [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]],
 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                    [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}
1.2.3.1.2 截断

另一方面,有时候一个序列可能对模型来说太长了。在这种情况下,您需要将序列截断为更短的长度。

truncation 参数设置为 True,以将序列截断为模型接受的最大长度:

batch_sentences = [
    "But what about second breakfast?",
    "Don't think he knows about second breakfast, Pip.",
    "What about elevensies?",
]
encoded_input = tokenizer(batch_sentences, padding=True, truncation=True)
print(encoded_input)
{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
               [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
               [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]],
 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                    [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}

查看填充和截断概念指南,了解更多有关填充和截断参数的信息。

1.2.3.1.3 构建张量

最后,tokenizer可以返回实际输入到模型的张量。

return_tensors 参数设置为 pt(对于PyTorch)或 tf(对于TensorFlow):

Pytorch:

batch_sentences = [
    "But what about second breakfast?",
    "Don't think he knows about second breakfast, Pip.",
    "What about elevensies?",
]
encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt")
print(encoded_input)
{'input_ids': tensor([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0],
                      [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102],
                      [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]]),
 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]),
 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                           [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])}

http://www.kler.cn/a/565169.html

相关文章:

  • 怎么实现LLM的微调
  • 【强化学习笔记1】从强化学习的基本概念到近端策略优化(PPO)
  • 某住宅小区地下车库安科瑞的新能源汽车充电桩的配电设计与应用方案
  • 鸿蒙 ArkUI 实现敲木鱼小游戏
  • 深度学习之特征提取
  • C#中使用System.Net库实现自动发送邮件功能
  • vs code默认主题修改配置
  • 变换队列c++
  • 2025网络安全等级测评报告,信息安全风险评估报告(Word模板)
  • 深入了解 NAT 模式:网络地址转换的奥秘
  • 昆仑通态-数据显示
  • Redis的安装教程(Windows+Linux)【超详细】
  • 《数据库系统概论 第六版》资源下载:数据库领域的殿堂级指南
  • 进阶面试题 ——‘说说你对浏览器的V8引擎的理解’
  • AVA面试_进阶部分_kafka面试题
  • docker container 修改 Mounts
  • rust 前端npm依赖工具rsup升级日志
  • hbase笔记总结1
  • 前端面试基础知识整理(一)
  • 2025年React Hooks的进阶面试题130题及其答案解析..