[SOLVED]Resolving NotImplementedError?

username: Petro_Key
cooked: <p>Hi, I have an issue on creating a super class of autoencoder.<br>
The following class causes an error of “NotImplementedError” in module.py when z = model(x) is evaluated.</p>
<p>I am working on Python 3.6 and got the same error on either Spyder and Prompt.</p>
<p>How can I solve this?<br>
Thank you in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">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
</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>Could you give the stack trace that comes with this error, it is hard to know where the error comes from otherwise.</p>

username: Petro_Key
cooked: <p>Hi albanD,<br>
Thank you again for your quick reply!<br>
Here is the trace with the error below.</p>
<p>Thank you.</p>
<p>################################################<br>
IPython 6.4.0 – An enhanced Interactive Python.</p>
<p>runfile(‘C:/usr/local/Anaconda3/mylib/Autoencoder_test.py’, wdir=‘C:/usr/local/Anaconda3/mylib’)<br>
Traceback (most recent call last):</p>
<p>File “”, line 1, in <br>
runfile(‘C:/usr/local/Anaconda3/mylib/Autoencoder_test.py’, wdir=‘C:/usr/local/Anaconda3/mylib’)</p>
<p>File “C:\usr\local\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile<br>
execfile(filename, namespace)</p>
<p>File “C:\usr\local\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “C:/usr/local/Anaconda3/mylib/Autoencoder_test.py”, line 85, in <br>
xhat = model(x)</p>
<p>File “C:\usr\local\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 477, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)</p>
<p>File “C:\usr\local\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 83, in forward<br>
raise NotImplementedError</p>
<p>NotImplementedError</p>

username: InnovArul
cooked: <p>It seems your model’s <code>forward()</code> function is not indented correctly and hence the error.</p>

username: Petro_Key
cooked: <p>Hi, InnovArul,</p>
<p>Thank you for your kind reply. I tried it out, but unfortunately could not figure out the problem.<br>
Instead, I tried another structure of class as follows, and then the code seems to be working!<br>
I will also keep in mind the correct indent as you suggest.</p>
<p>I really appreciate your help, albanD and InnovArul <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">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
</code></pre>

username: Insight_Gong
cooked: <p>So have you solved it?</p>

username: ptrblck
cooked: <p>Usually this error is thrown, if you have a typo in the <code>forward</code> definition or if you are using a wrong indentation, which was the case here.</p>

username: noreddine.belhadjche
cooked: <p>I have also faced this problem, and although I tried the solutions, I kept getting the same error.<br>
I restarted the notebooks kernel. and it tried the solutions again. It WORKED <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> . I just wanted to mention this maybe one of you face the same thing <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: Sunetra_Banerjee
cooked: <p>Thank you. It worked for me.</p>

username: mehdibabakhan
cooked: <p>thank you so much. Wrong indentation was the roadblock</p>

username: Youssof_R
cooked: <p>I have had the same error because I have been passing the input of the model directly to a <code>nn.ModuleList</code> object, instead of passing it through each of its contained modules.</p>
<p>So my original code looked like:</p>
<pre><code class="lang-auto">class Discriminator(nn.Module):
    
    def __init__(self, hidden_dim = 128, depth = 3, in_dim = 200):
        super(Discriminator, self).__init__()

        # parameter
        self.in_dim = in_dim

        # model arch.
        self.layers = nn.ModuleList([nn.Linear(in_dim, hidden_dim),
                                     nn.BatchNorm1d(hidden_dim),
                                     nn.LeakyReLU()])
        for i in range(0, depth-1):
            self.layers.extend([nn.Linear(hidden_dim//2**i,hidden_dim//2**(i+1)),
                                nn.BatchNorm1d(hidden_dim//2**(i+1)),
                                nn.LeakyReLU()])
        self.layers.extend([nn.Linear(hidden_dim//2**(depth-1), 1)])
    
    def forward(self, x):
        x = x.view(-1, self.in_dim)
        return self.layers(x)
</code></pre>
<p>And to fix it I just replaced the last line with the last line to:</p>
<pre><code class="lang-auto">    for layer in self.layers:
                x = layer(x)
            return x
</code></pre>
<p>In case this might help anyone in the future.</p>

username: HarBatt
cooked: <p>Wow, this is the answer I am looking for. Thanks!</p>

username: Jeonsam
cooked: <p>Thanks! It works for me.</p>