我用我自己的未标记的数据来训练转换器,如下所示:
基于python train_mlm.py sentence-transformers/LaBSE train.txt的https://github.com/UKPLab/sentence-transformers/tree/master/examples/unsupervised_learning/MLM
然后,我想得到设置的嵌入。代码:
model = AutoModelForMaskedLM.from_pretrained('output/sentence-transformers_LaBSE-2021-12-28_13-03-20')
tokenizer = AutoTokenizer.from_pretrained('output/sentence-transformers_LaBSE-2021-12-28_13-03-20')
model = model.eval()
english_sentences = [
"dog",
"Puppies are nice.",
"I enjoy taking long walks along the beach with my dog.",
]
encoded_input = tokenizer(english_sentences, padding=True, truncation=True, max_length=64, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
print(model_output[0].shape)问题是我产出的形状有点像(3,14,500,000)。如果不对我的数据形状进行培训,则为(3,14,768)。我做错了什么?在我的训练之后,我怎样才能得到最后的嵌入?
发布于 2021-12-30 11:30:05
你预先训练了一个关于蒙面语言建模(MLM)的变压器。这并不意味着之后您必须使用MLM头:AutoModelForMaskedLM.from_pretrained;因为您的下游任务实际上是嵌入生成,给出了一些输入。这是通过只使用您的微调模型的基本编码器,而没有任何头部顶部:AutoModel.from_pretrained(...)。这将返回您期望的输出形状。
更多关于输出形状的说明:
AutoModel所提供的。AutoModelForMaskedLM提供的.给定B=批大小和L=批序列长度。
https://stackoverflow.com/questions/70507101
复制相似问题