我在使用 Python 编写深度学习代码时遇到了问题。具体来说,当尝试从 d2l.torch 模块调用“train_ch3”函数时,我遇到了 AttributeError。我已经确认“train_ch3”函数确实存在于我的 d2l.torch 模块中,但由于某种原因,在我的代码中找不到它。
这是我在代码中导入 d2l.torch 模块的方法:
import torch
from torch import nn
from d2l import torch as d2l
这是错误消息(通过 VSCode):
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[16], line 2
1 num_epochs = 10
----> 2 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
AttributeError: module 'd2l.torch' has no attribute 'train_ch3'
我尝试在 Python 深度学习代码中使用“d2l.torch”模块中的“train_ch3”函数。自从我确认该函数存在于模块中以来,我希望该函数能够被识别并执行而不会出现问题。但是,我遇到了 AttributeError。尽管确保了正确的版本和文件路径,问题仍然存在。我期望成功调用“train_ch3”,没有任何属性错误,但实际结果是一个 AttributeError,表明模块“d2l.torch”没有属性“train_ch3”。
这个问题的原因是我的PyTorch环境中已经加载了lib d2l,但是这个d2l lib没有属性'train_ch3',所以我通过下载源代码解决了这个问题,并在我的PyTorch环境中覆盖了原始的d2l lib代码.