网站备案名字,国内黑色风格的网站,wordpress显示一个分类列表名称,只会html wordpress处理文本数据的主要工具是Tokenizer。Tokenizer根据一组规则将文本拆分为tokens。然后将这些tokens转换为数字#xff0c;然后转换为张量#xff0c;成为模型的输入。模型所需的任何附加输入都由Tokenizer添加。
如果您计划使用预训练模型#xff0c;重要的是使用与之关联的…处理文本数据的主要工具是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 - CLS 和 SEP分类器和分隔符。并非所有模型都需要特殊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],Dont think he knows about second breakfast, Pip.,What about elevensies?,
]
encoded_input tokenizer(batch_sentences, paddingTrue, 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?,Dont think he knows about second breakfast, Pip.,What about elevensies?,
]
encoded_input tokenizer(batch_sentences, paddingTrue)
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?,Dont think he knows about second breakfast, Pip.,What about elevensies?,
]
encoded_input tokenizer(batch_sentences, paddingTrue, truncationTrue)
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?,Dont think he knows about second breakfast, Pip.,What about elevensies?,
]
encoded_input tokenizer(batch_sentences, paddingTrue, truncationTrue, return_tensorspt)
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.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.kndst.cn.gov.cn.kndst.cn