当前位置:   article > 正文

BertTokenizer详解_bert tokenizer

bert tokenizer

文章目录

import numpy as np
# 指定bert预训练模型文件夹路径
bert_path='your_bert_model_path'
#加载BertTokenizer
tokenizer = BertTokenizer.from_pretrained(bert_path)

'''
方式一,直接使用tokenizer进行序列化

'''
out_puts= tokenizer(['我','爱我'], ['的祖国','这是一种'], padding='max_length', return_tensors="pt", max_length=6)
'''
{
	'input_ids':       tensor([[ 101, 2769,  102, 4638, 4862, 1744,  102,    0,    0,    0],
        			           [ 101, 4263, 2769,  102, 6821, 3221,  671, 4905,  102,    0]]),         
     'token_type_ids': tensor([[0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
        					   [0, 0, 0, 0, 1, 1, 1, 1, 1, 0]]), 
     'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
        					   [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]])}
'''
print(out_puts.input_ids)


#方式二,使用encode进行序列化
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/781129
推荐阅读
相关标签
  

闽ICP备14008679号