I encountered an issue while working on a deep learning code in Python. Specifically, when trying to call the 'train_ch3' function from the d2l.torch module, I encountered an AttributeError. I have confirmed that the 'train_ch3' function is indeed present in my d2l.torch module, but for some reason, it cannot be found in my code.
Here is how I import the d2l.torch module in my code:
import torch
from torch import nn
from d2l import torch as d2l
Here is the error message (by 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'
Here is my project directory: enter image description here
I attempted to use the 'train_ch3' function from the 'd2l.torch' module in my Python deep learning code. I expected the function to be recognized and executed without issues since I confirmed its existence in the module. However, I encountered an AttributeError. Despite ensuring the correct version and file path, the problem persists. I expected a successful invocation of 'train_ch3' without any attribute errors, but the actual result was an AttributeError indicating that the module 'd2l.torch' has no attribute 'train_ch3'.
The reason of this problem is lib d2l has already loaded in my PyTorch environment, but this d2l lib has no attribute 'train_ch3', so I solved this problem by downloading the source code, and covered the original d2l lib code in my PyTorch environment.