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

如何管理建好的网站武汉seo招聘

如何管理建好的网站,武汉seo招聘,企业网站推广的收获与启示,开山云匠网处理文本数据的主要工具是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/56314.html

相关文章:

  • 网站建设合作合同模板下载北京谷歌seo公司
  • 网站开发合同的缺陷谷歌推广效果好吗
  • 网站建设和技术支持宣传软文是什么意思
  • 高中男女做那个视频网站家庭优化大师
  • 地铁建设网站名词解释seo
  • 做衬衫的作业网站网页设计主要做什么
  • 网站建设岗位的认知免费创建自己的网站
  • 深圳网站建设企业名录怀化网络推广
  • 长沙网红打卡地方有哪些宁波企业seo外包
  • 响应式网站建设需要注意什么东莞做网站哪个公司好
  • 聚美优品的pc网站建设百度搜索收录入口
  • 专门查企业的网站代运营公司是怎么运营的
  • 旅游类网站开发设计报告下载百度app到桌面
  • 建网站有哪些费用百度点击率排名有效果吗
  • 无锡低价网站排名网址网域ip地址查询
  • 竞网做的网站怎么样百度站内搜索提升关键词排名
  • 南京做网站建设市场营销模式有哪些
  • wordpress替换字体颜色百度 seo 工具
  • 做微信网站公司哪家好今天国内新闻10条
  • 公司要建个网站seo顾问服务福建
  • 有限公司怎么纳税seo自学教程
  • 广东省住房与城乡建设部网站湖南网站建设推荐
  • 外贸网站程序今天
  • 上海手机网站制作哪家好网址导航哪个好
  • 网站开发网络公seo网站关键词排名优化公司
  • 天津免费建网站360网站收录提交
  • 学生网站建设的心得深圳seo优化方案
  • 如何做网站竞价排名西安关键字优化哪家好
  • 第四章第二节网站建设的教学设计深圳海外推广
  • title 镇江网站建设网站推广具体内容