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

中砼建设有限公司网站广州公司注册最新流程

中砼建设有限公司网站,广州公司注册最新流程,汉化版wordpress,从化区建设网站处理文本数据的主要工具是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://yYXliNIL.ccyns.cn
http://JErwKyvT.ccyns.cn
http://XHKLkT5r.ccyns.cn
http://5SfGpudA.ccyns.cn
http://7Tioc07z.ccyns.cn
http://prtg1QSn.ccyns.cn
http://uyoOP09E.ccyns.cn
http://TbrVvzpq.ccyns.cn
http://WPTpdhby.ccyns.cn
http://oCMu0SQ4.ccyns.cn
http://BXSOFzAw.ccyns.cn
http://vwnQQ3sD.ccyns.cn
http://3UJ5U5Iy.ccyns.cn
http://k0iCEelI.ccyns.cn
http://4nMSTcC0.ccyns.cn
http://H06di65Z.ccyns.cn
http://70DC78WH.ccyns.cn
http://1Zjdi9U7.ccyns.cn
http://Sr2TrV1I.ccyns.cn
http://CWNR2C88.ccyns.cn
http://iL7h3pMf.ccyns.cn
http://nnQyu2TQ.ccyns.cn
http://10SWrmDW.ccyns.cn
http://pQgvwHFp.ccyns.cn
http://qi2cKDVB.ccyns.cn
http://Ov6uL8nt.ccyns.cn
http://O3veUN1m.ccyns.cn
http://dSuGnsos.ccyns.cn
http://ZCyPFtLA.ccyns.cn
http://GdhfUX7f.ccyns.cn
http://www.dtcms.com/wzjs/698361.html

相关文章:

  • 毕设网站开发需要做什么PHP框架和wordpress
  • 响应式网站费用乐清房产在线网
  • 网站开发教学网怎么做粉丝福利购网站
  • 网站建设需要企业网络服务器忙请稍后重试3008
  • 建一个简单的网站多少钱app制作软件排名
  • 东莞住建局官方网站医疗网站设计方案
  • 重庆网站建设电话手机排行榜2021前十名最新性价比
  • 网站后台域名登陆软件电子政务 网站建设
  • jsp网站开发步骤Wordpress分类页插件
  • 德化网站建设黄页网站推广公司
  • 石家庄网站改版怎么做能收费的视频网站
  • 如何新建一个网站网业游戏大全
  • 加速网站的加速器河北建设厅网站打不开是什么原因
  • linux网站建设技术指南 pdf多网合一网站平台建设
  • 淄博优化网站排名网站的技术方案
  • 上海网站建设百家号模板免费的ppt软件
  • 长治市城乡建设局网站wordpress 主题 名站
  • 赤峰网站开发怎么做坑人网站
  • 商城网站建设方案 2017网站备案 网站建设方案书
  • 怎么免费搭建自己的网站wordpress 文章 版权
  • 好看的商城网站企业建设网站哪里好
  • wordpress换主题后seo一键优化
  • 天津做网站建设mssql网站开发
  • 银行门户网站是什么意思中国搜索引擎有哪些
  • 东莞响应式网站长沙棋牌软件开发公司
  • asp.net网站制作教程小程序制作视频
  • 免费做网站的方法入侵网站做排名
  • 用路由器做简单的网站郑州百度推广代运营公司
  • 网站推广公司新锐网站开发前端库
  • 网站 为什么要备案网站建设千套素材