网站建设大概要多少钱,建设银行 企业,网站外链建设方法,wordpress5.2火车头发布encode和encode_plus的区别可以参考我的另一篇博客#xff1a;https://blog.csdn.net/weixin_41862755/article/details/120070535
encode和tokenize的区别
区别
encode编码出来的结果#xff0c;包含开始的[CLS]和结尾的[SEP]#xff0c;所以程序输出结果比原sentence多…encode和encode_plus的区别可以参考我的另一篇博客https://blog.csdn.net/weixin_41862755/article/details/120070535
encode和tokenize的区别
区别
encode编码出来的结果包含开始的[CLS]和结尾的[SEP]所以程序输出结果比原sentence多两位。tokenize编码出来的结果就是sentence对应的id无多余项。
import torch
from transformers import BertTokenizermodel_name bert-base-uncased# a.通过词典导入分词器
tokenizer BertTokenizer.from_pretrained(model_name)
sentence Hello, my son is laughing.input_ids torch.tensor(tokenizer.encode(sentence))input_id tokenizer.tokenize(sentence)
input_id2 tokenizer.convert_tokens_to_ids(input_id)print(input_ids结果是,input_ids)
print(input_id2的结果是,input_id2)执行结果如下
input_ids结果是 tensor([ 101, 7592, 1010, 2026, 2365, 2003, 5870, 1012, 102])
input_id2的结果是 [7592, 1010, 2026, 2365, 2003, 5870, 1012]注意
input_id2[0:len(input_id2)]) #取得和input_ids一样的结果