我正在尝试使用以下代码冻结我的屏蔽语言模型的一些层:
for param in model.bert.parameters():
param.requires_grad = False然而,当我执行上面的代码时,我得到了这个错误:
AttributeError: 'RobertaForMaskedLM' object has no attribute 'bert'在我的代码中,我为我的屏蔽语言模型导入了以下内容,但我不确定是什么导致了上面的错误:
from transformers import AutoModelForMaskedLM
model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)到目前为止,我已经尝试在我的代码中用model替换bert,但是不起作用。
任何帮助都是好的。
谢谢。
发布于 2021-08-19 09:15:46
如果查看RobertaForMaskedLM代码here的源代码,就会发现没有名为bert的对象。相反,它们有一个属于RobertaModel类型的对象roberta
因此,要冻结Roberta模型并只训练LM头,您应该将代码修改为:
for param in model.roberta.parameters():
param.requires_grad = Falsehttps://stackoverflow.com/questions/68384132
复制相似问题