首页 > 其他分享 >Huggingface简介及BERT tansformer 开源

Huggingface简介及BERT tansformer 开源

时间:2023-01-25 21:08:37浏览次数:41  
标签:BERT name text ids Huggingface input tansformer model


Huggingface简介及BERT代码浅析
import torch
from transformers import BertModel, BertTokenizer
# 这里我们调用bert-base模型,同时模型的词典经过小写处理
model_name = 'bert-base-uncased'
# 读取模型对应的tokenizer
tokenizer = BertTokenizer.from_pretrained(model_name)
# 载入模型
model = BertModel.from_pretrained(model_name)
# 输入文本
input_text = "Here is some text to encode"
# 通过tokenizer把文本变成 token_id
input_ids = tokenizer.encode(input_text, add_special_tokens=True)
# input_ids: [101, 2182, 2003, 2070, 3793, 2000, 4372, 16044, 102]
input_ids = torch.tensor([input_ids])
# 获得BERT模型最后一个隐层结果
with torch.no_grad():
last_hidden_states = model(input_ids)[0] # Models outputs are now tuples


标签:BERT,name,text,ids,Huggingface,input,tansformer,model
From: https://blog.51cto.com/u_15202985/6022883

相关文章