在 Google Colab 中,我使用 Hugging Face 转换器库加载了 BERT 模型,然后使用 Seq2SeqTrainer 对其进行了微调。然后我使用 model.save_pretrained("folder/path") 将此模型保存到我的 Google Drive。但是,当我使用 EncoderDecoder.from_pretrained() 在另一个 Google Colab 笔记本中加载此模型时,我收到以下消息:
The following encoder weights were not tied to the decoder ['bert/pooler']
The following encoder weights were not tied to the decoder ['bert/pooler']
The following encoder weights were not tied to the decoder ['bert/pooler']
The following encoder weights were not tied to the decoder ['bert/pooler']
现在,事情变得奇怪了:我的模型似乎在第一次运行时就可以工作(与在微调时在同一个 Colab 笔记本中运行时有一些差异)。但随后我将得到的输出放回到模型中,得到了完全相同的输出。例如,我第一次输入“苹果”,我得到“香蕉”。然后我在模型中输入“banana”,再次得到“banana”!这是正常现象,还是因为池化器权重尚未正确设置?
这是我的最小代码示例:
model = EncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-uncased", "bert-base-uncased", tie_encoder_decoder=True)
model.config.max_length = 512
model.config.min_length = 10
model.config.no_repeat_ngram_size = 0
model.config.early_stopping = True
model.config.length_penalty = 2.0
model.config.num_beams = 4
training_args = Seq2SeqTrainingArguments(
predict_with_generate=True,
fp16=True,
output_dir="./",
logging_steps=2,
save_steps=10
)
trainer = Seq2SeqTrainer(
model=model,
tokenizer=tokenizer,
args=training_args,
train_dataset=train
)
model.save_pretrained("/folder/path")
所以我发现我得到“以下编码器权重与解码器['bert/pooler']无关”的原因是因为在微调之前热启动模型时我有“tie_encoder_decoder=True”作为选项然后保存它。当我删除该消息后,该消息就消失了。
我仍然遇到模型第二次运行后输出基本相同的问题,但现在有一些差异,所以更好。如果有人可以向我解释为什么我在第二次运行模型时得到相同的输出,那就太好了,但现在比以前好一点了。