BERT/Transformer模型可以接受不同大小的输入批次。下面是一个使用PyTorch的代码示例,展示了如何处理不同大小的输入批次:
import torch
from transformers import BertModel, BertTokenizer
# 初始化BERT模型和分词器
model = BertModel.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# 输入文本
text_list = ["Hello, how are you?", "I am doing great!"]
# 使用分词器对文本进行编码
input_ids = []
attention_masks = []
for text in text_list:
encoded_dict = tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=512,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt'
)
input_ids.append(encoded_dict['input_ids'])
attention_masks.append(encoded_dict['attention_mask'])
# 将编码后的输入转换为张量
input_ids = torch.cat(input_ids, dim=0)
attention_masks = torch.cat(attention_masks, dim=0)
# 计算模型的输出
outputs = model(input_ids, attention_mask=attention_masks)
# 输出模型的结果
print(outputs)
在上面的代码中,我们首先使用BertTokenizer
将输入文本编码为BERT模型可接受的输入。我们使用encode_plus
方法对每个文本进行编码,并设置max_length
参数来控制编码后的最大长度。然后,我们将编码后的输入转换为张量,并传入BERT模型进行计算。最后,我们可以获取模型的输出结果。
这个示例展示了如何处理具有不同长度的输入文本,并且能够处理多个文本一起计算的情况。BERT模型会自动对不同长度的输入进行处理,并生成相应的输出。