当前位置:   article > 正文

使用Hugging Face快速实现Tokenizer_autotokenizer.from_pretrained("bert-base-chinese")

autotokenizer.from_pretrained("bert-base-chinese")

使用Hugging Face快速上手Tokenizer

方法

step1

进入huggingface网站
在搜索栏中搜索chinese【根据自己的需求来,如果数据集是中文这的搜索】
在这里插入图片描述

step2

打开第一个bert-base-chinese
在这里插入图片描述

在这里插入图片描述
复制下面这段话到vscode里

from transformers import AutoTokenizer, AutoModelForMaskedLM
tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
model = AutoModelForMaskedLM.from_pretrained("bert-base-chinese")
  • 1
  • 2
  • 3
step3
'''
Description: 快速入门
Autor: 365JHWZGo
Date: 2022-01-21 11:54:58
LastEditors: 365JHWZGo
LastEditTime: 2022-01-21 12:13:41
'''

from transformers import  AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('bert-base-chinese')
# encode
word_embeding = tokenizer('我是中国人,我骄傲!')
print(word_embeding)
'''
{
    'input_ids': [101, 2769, 3221, 704, 1744, 782, 8024, 2769, 7734, 1000, 8013, 102], 
    'token_type_ids': [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]
}
'''

# decode
decode_word = tokenizer.decode(word_embeding['input_ids'])
print(decode_word)
# [CLS] 我 是 中 国 人 , 我 骄 傲 ! [SEP]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/281432
推荐阅读
相关标签
  

闽ICP备14008679号