bert调用相关知识点

https://www.cnblogs.com/cxq1126/p/13517394.html

1 print(tokenizer.encode('我不喜欢你'))                    #[101, 2769, 679, 1599, 3614, 872, 102]
2 sen_code = tokenizer.encode_plus('我不喜欢这世界','我只喜欢你')
3 print(sen_code)
4 # {'input_ids': [101, 2769, 679, 1599, 3614, 6821, 686, 4518, 102, 2769, 1372, 1599, 3614, 872, 102], 
5 #  'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], 
6 #  'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
1 print(tokenizer.convert_ids_to_tokens(sen_code['input_ids']))
2 #['[CLS]', '我', '不', '喜', '欢', '这', '世', '界', '[SEP]', '我', '只', '喜', '欢', '你', '[SEP]']
#对编码进行转换,以便输入Tensor
tokens_tensor = torch.tensor([sen_code['input_ids']])           # 添加batch维度并转化为tensor
segments_tensors = torch.tensor([sen_code['token_type_ids']])

bert_model.eval()

#进行编码
with torch.no_grad():

    outputs = bert_model(tokens_tensor, token_type_ids=segments_tensors)
    encoded_layers = outputs         #outputs类型为tuples
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值