You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch Lightning训练MemeNet时出现Trainer未绑定模型报错求助

解决RuntimeError: MemeNet is not attached to a Trainer错误

错误根源

你在validation_epoch_end()方法里错误执行了super().__init__()——这是类的构造函数调用,会重新初始化你的MemeNet实例,直接破坏了模型与Trainer之间的绑定关系,导致日志记录时触发该错误。

修复代码

删掉super().__init__()这一行即可,修正后的validation_epoch_end()代码如下:

def validation_epoch_end(self, outputs) -> None:
    mean_loss = torch.mean(torch.stack([o["Val_Loss"] for o in outputs]))
    mean_rocauc = np.mean(np.array([o["Score"] for o in outputs]))
  
    self.log("val_loss", mean_loss, on_epoch=True, prog_bar=True)
    self.log("val_score", mean_rocauc, on_epoch=True, prog_bar=True)

补充说明

如果你的父类(比如pl.LightningModule)的validation_epoch_end有需要继承的逻辑,应该调用super().validation_epoch_end(outputs),而不是构造函数__init__。同时确认你的MemeNet类正确继承自pl.LightningModule,保证钩子方法能被Trainer正常调度。

内容的提问来源于stack exchange,提问作者NoobC0der

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 20:15:41