Bert是通过在输入中增加
import torch
from transformers import BertTokenizer, BertModel
# 初始化Bert模型和分词器
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
# 输入文本
text_batch = ['This is a sentence.', 'This is another sentence which is longer than the first one.']
# 分词并生成输入
encoding = tokenizer(text_batch, return_tensors='pt', padding=True, truncation=True)
# 在Bert模型上运行输入
output = model(input_ids=encoding['input_ids'], attention_mask=encoding['attention_mask'])
# 输出形状
print(output.last_hidden_state.shape)
上一篇:BERT示例中的类型错误
下一篇:BERT是如何利用TPU内存的?