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

如何建设网站方便后期维护郑州网络推广公司

如何建设网站方便后期维护,郑州网络推广公司,邵阳市建设工程造价管理站网站,wordpress guestbook处理文本数据的主要工具是Tokenizer。Tokenizer根据一组规则将文本拆分为tokens。然后将这些tokens转换为数字,然后转换为张量,成为模型的输入。模型所需的任何附加输入都由Tokenizer添加。 如果您计划使用预训练模型,重要的是使用与之关联的…

处理文本数据的主要工具是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 AutoTokenizertokenizer = 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.tj-hxxt.cn/news/113051.html

相关文章:

  • 企业商城网站开发小企业广告投放平台
  • 创建网站目录时我们应该百度营销客户端
  • 网站设计与开发实验报告seo的主要工作内容
  • 网站标题怎么做链接googleplay
  • 网站建设的基本要求公司网站建设教程
  • 合适做服装的国际网站全媒体运营师报名费多少钱
  • 大数据新闻网站怎么做怎样建网站?
  • 广东东莞住建局seo搜索引擎优化入门
  • 做的网站一模一样会被告吗广州网站优化多少钱
  • 乐平城市建设局网站百度指数排行榜
  • 求生之路2怎么做非官方网站抖音seo源码搭建
  • 做js题目的网站天津百度推广公司电话
  • 自己做商品网站怎么做培训机构排名前十
  • 高端网站建设企业官网建设网络营销分类
  • vps挂网站企业网站建站
  • 代运营公司的套路seo深度解析
  • 网站域名在限制域名中成都黑帽seo
  • 桂林设计单位资质升级网站百度怎么免费推广自己的产品
  • 门户网站的建设和管理情况自查长沙全网推广
  • 淘宝客如何做网站全网引擎搜索
  • 怎么自己做网站表白单页网站排名优化
  • 邢台wap网站建设外贸网站平台都有哪些
  • 网上帮别人做网站单页应用seo如何解决
  • 为什么百度地图嵌入网站不显示百度应用平台
  • 百度小程序 wordpressseo搜索引擎优化实训总结
  • wordpress网站备案号许昌网站seo
  • 邻水网站建设网上推广怎么弄?
  • 做相框的网站债务优化是什么意思
  • 常州网站制作市场网络营销的常用方法
  • 网店管家官网搜索引擎优化工具有哪些