如何解决带有 Pytorch-lightning 的 MT5ForConditionalGeneration 给出了 attribute_error
好几天都无法解决这个问题,因为我是 NLP 新手,实际解决方案可能非常简单
class QAModel(pl.LightningDataModule):
def __init__(self):
super().__init__()
self.model = MT5ForConditionalGeneration.from_pretrained(MODEL_NAME,return_dict=True)
def forward(self,input_ids,attention_mask,labels=None):
output = model(
input_ids=input_ids,attention_mask=attention_mask,labels=labels
)
return output.loss,output.logits
def training_step(self,batch,batch_idx):
input_ids = batch['input_ids']
attention_mask = batch['attention_mask']
labels = batch['labels']
loss,outputs = self(input_ids,labels)
self.log('train_loss',loss,prog_bar=True,logger=True)
return loss
def validation_step(self,labels)
self.log('val_loss',logger=True)
return loss
def test_step(self,labels)
self.log('test_loss',logger=True)
return loss
def configure_optimizers(self):
return AdamW(self.parameters(),lr=0.0001)
model = QAModel()
from pytorch_lightning.callbacks import ModelCheckpoint
checkpoint_callback = ModelCheckpoint(
dirpath='/content/checkpoints',filename='best-checkpoint',save_top_k=1,verbose=True,monitor='val_loss',mode='min'
)
trainer = pl.Trainer(
checkpoint_callback=checkpoint_callback,max_epochs=N_EPOCHS,gpus=1,progress_bar_refresh_rate=30
)
trainer.fit(model,data_module)
运行这段代码给了我 AttributeError: 'QAModel' 对象没有属性 'automatic_optimization' 在 fit() 函数之后 可能问题出在 MT5ForConditionalGeneration 中,因为在将其传递给 funtion() 后,我们遇到了同样的错误
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。