[SOLVED]Resolving NotImplementedError?

Hi, I have an issue on creating a super class of autoencoder.
The following class causes an error of “NotImplementedError” in module.py when z = model(x) is evaluated.

I am working on Python 3.6 and got the same error on either Spyder and Prompt.

How can I solve this?
Thank you in advance :slight_smile:

class Autoencoder(nn.Module): # NN module
    def __init__(self):
        super(Autoencoder, self).__init__() # Super class from Autoencoder
        self.encoder = nn.Sequential(
            nn.Linear(28*28, 128),
            nn.ReLU(True),
            nn.Linear(128, 64),
            nn.ReLU(True),
            nn.Linear(64, 12),
            nn.ReLU(True),
            nn.Linear(12, 2)
            )
        
        self.decoder = nn.Sequential(
            nn.Linear(2, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.ReLU(True),
            nn.Linear(128, 28 * 28),
            nn.Tanh()
            )
        
        def forward(self, x):
            x = self.encoder(x)
            x = self.decoder(x)
            return x

Hi,

Could you give the stack trace that comes with this error, it is hard to know where the error comes from otherwise.

Hi albanD,
Thank you again for your quick reply!
Here is the trace with the error below.

Thank you.

################################################
IPython 6.4.0 – An enhanced Interactive Python.

runfile(‘C:/usr/local/Anaconda3/mylib/Autoencoder_test.py’, wdir=‘C:/usr/local/Anaconda3/mylib’)
Traceback (most recent call last):

File “”, line 1, in
runfile(‘C:/usr/local/Anaconda3/mylib/Autoencoder_test.py’, wdir=‘C:/usr/local/Anaconda3/mylib’)

File “C:\usr\local\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)

File “C:\usr\local\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “C:/usr/local/Anaconda3/mylib/Autoencoder_test.py”, line 85, in
xhat = model(x)

File “C:\usr\local\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 477, in call
result = self.forward(*input, **kwargs)

File “C:\usr\local\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 83, in forward
raise NotImplementedError

NotImplementedError

It seems your model’s forward() function is not indented correctly and hence the error.

1 Like

Hi, InnovArul,

Thank you for your kind reply. I tried it out, but unfortunately could not figure out the problem.
Instead, I tried another structure of class as follows, and then the code seems to be working!
I will also keep in mind the correct indent as you suggest.

I really appreciate your help, albanD and InnovArul :slight_smile:

class Encoder(nn.Module):
    def __init__(self):
        super(Encoder, self).__init__()
        self.fc = nn.Sequential(
            nn.Linear(28*28, 128),
            nn.ReLU(True),
            nn.Linear(128, 64),
            nn.ReLU(True),
            nn.Linear(64, 12),
            nn.ReLU(True),
            nn.Linear(12, 2)
        )
    def forward(self, x):
        return self.fc(x)

class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()
        self.fc = nn.Sequential(
            nn.Linear(2, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.ReLU(True),
            nn.Linear(128, 28 * 28),
            nn.Tanh()
        )
    def forward(self, x):
        return self.fc(x)

class AutoEncoder(nn.Module):
    def __init__(self):
        super(AutoEncoder, self).__init__()
        self.fc1 = Encoder()
        self.fc2 = Decoder()

    def forward(self, x):
        code = self.fc1(x)
        out = self.fc2(code)
        return out

So have you solved it?

Usually this error is thrown, if you have a typo in the forward definition or if you are using a wrong indentation, which was the case here.

3 Likes