NotimplementedError while densenet implementation

username: s885219s
cooked: <p>I tried to implement the densenet architecture trained on CIFAR and SVHN, which has 100 layers and 3 dense blocks, by changing parameters of densenet code in torchvision package.</p>
<p>Although, I could print my net architecture, I got an NotImplementedError in the very beginning when I ran the code.</p>
<blockquote>
<hr>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in ()<br>
16         print(r_x.size())<br>
17         t_y = torch.cat([r_y,l_y],1)<br>
—&gt; 18         output3 = net(r_x,l_x)<br>
19         loss = loss_func(output3, t_y)<br>
20         acc_loss += loss.data[0]</p>
<p>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
222         for hook in self._forward_pre_hooks.values():<br>
223             hook(self, input)<br>
 → 224         result = self.forward(*input, **kwargs)<br>
225         for hook in self._forward_hooks.values():<br>
226             hook_result = hook(self, input, result)</p>
<p>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in forward(self, *input)<br>
63         Should be overriden by all subclasses.<br>
64         “”"<br>
—&gt; 65         raise NotImplementedError<br>
66<br>
67     def register_buffer(self, name, tensor):</p>
<p>NotImplementedError:</p>
</blockquote>
<p>I have no idea what is going on.<br>
Could anyone help me out with this?<br>
Thanks a lot!!</p>

username: apsvieira
cooked: <p>Hi,</p>
<p>It seems you have not defined a  <code>forward</code> method.<br>
All subclasses of <a href="http://pytorch.org/docs/master/nn.html#torch.nn.Module" rel="nofollow noopener"><code>nn.Module</code></a> should overwrite that method.</p>
<p>Please have a look at the docs/examples.</p>

username: s885219s
cooked: <p>OMG!</p>
<p>I checked my densenet class, and I found that I misspelled the word “forward”…<br>
Now it can run normally.</p>
<p>Thank you!!</p>

username: lesscomfortable
cooked: <p>Haha I had misspelled ‘forward’ too. What are the odds? Anyway, this saved much time…</p>

username: m2q
cooked: <p>Misspelled forward as well…</p>

username: punnerud
cooked: <p>I forgot to indent “def forward(self,x):”</p>
<p>The error can be read as: <em>can’t find <strong>forward</strong> in the Class.</em></p>

username: Rishabh_Tyagi
cooked: <p>Haha I did that too. Thank you, your reply saved my time.</p>

username: marx_was_right
cooked: <p>lol i wrote “forwad”</p>

username: kabada
cooked: <p>🤦 lol, came to share the shame. Made the same spelling mistake, “foward”</p>

username: Reshinth_ER
cooked: <p>Mismatch in Indentation also results in the same error.<strong>Make sure the definitions in the class item is of <em>same indent</em></strong>.</p>

username: nabsabs
cooked: <p>i was like, how can everyone be misspelling forward? that’s not my error!</p>
<p><code>def foward(self,x):</code></p>
<p>killsself.</p>

username: amay_trivedi
cooked: <p>class Fashion_MNIST_NeuralNet(nn.Module) :</p>
<p>def <strong>init</strong>(self) :</p>
<pre><code>#Calling the module class for the reason mentioned above...
super(Fashion_MNIST_NeuralNet,self).__init__()
self.fMnistConv1 = nn.Conv2d(in_channels = 1 , out_channels = 6 , kernel_size = 5)
self.fMnistConv2 = nn.Conv2d(in_channels = 6 , out_channels = 12 , kernel_size = 5)

#adding fully connected layers

self.fMnist_fc1 = nn.Linear(12*4*4 , 120)
self.fMnist_fc2 = nn.Linear(120 , 84)
self.fMnist_fc3 = nn.Linear(84 , 10)
</code></pre>
<p>def forward(self,x) :<br>
<span class="hashtag">#appplying</span> max_pool2d layer to reduce the dimensionality of the matrix obtained from convolutions…<br>
# Max pooling over a (2, 2) window<br>
print(‘Forward pass’)<br>
x = F.max_pool2d(F.relu(self.fMnistConv1(x)), (2, 2))<br>
# If the size is a square you can only specify a single number<br>
x = F.max_pool2d(F.relu(self.fMnistConv2(x)), 2)<br>
print("===x===1" , x.shape)<br>
x = x.view(-1 , self.get_num_features(x))<br>
print("==View==" , x.size())<br>
x = F.relu(self.fMnist_fc1(x))<br>
x = F.relu(self.fMnist_fc2(x))<br>
x = self.fMnist_fc3(x)<br>
return x</p>
<p>def get_num_features(self ,x):<br>
print(‘Number of Features found=====’ , x.shape)<br>
size = x.size()[1:]<br>
print(“Size” , size)<br>
num_features = 1<br>
for s in size:<br>
num_features*=s</p>
<pre><code>print("==Mult==" , num_features)
return num_features
</code></pre>
<p>fn = Fashion_MNIST_NeuralNet()</p>
<p>Im getting an Error in this snippet</p>
<p>import torch.nn as nn<br>
def fit_model(model , train_loader):<br>
<span class="hashtag">#Train</span> Loader is used from the DataLoader done earlier…<br>
optimizer = torch.optim.Adam(model.parameters())<br>
‘’’<br>
Parameters :-<br>
conv1.weight	torch.Size([6, 1, 5, 5])<br>
conv1.bias	torch.Size([6])<br>
conv2.weight	torch.Size([12, 6, 5, 5])<br>
conv2.bias	torch.Size([12])<br>
fc1.weight	torch.Size([120, 192])<br>
fc1.bias	torch.Size([120])<br>
fc2.weight	torch.Size([84, 120])<br>
fc2.bias	torch.Size([84])<br>
fc3.weight	torch.Size([10, 84])<br>
fc3.bias	torch.Size([10])<br>
‘’’<br>
error = nn.CrossEntropyLoss()<br>
EPOCHS = 5<br>
model.train()</p>
<p>for epoch in range(EPOCHS):</p>
<pre><code>print(epoch)

for idx , (image , label) in enumerate(train_loader):
  
  var_X_batch = torch.randn(image.shape)
  print(var_X_batch.shape)
  var_y_batch = torch.tensor(label)
  
  optimizer.zero_grad()
  #passing the batch of images to the FNet_MNIST
  print('Debug 1')
  output = model(var_X_batch)
  print('Debug 2')
  loss = error(output, var_y_batch)
  print('Debug 2')
  loss.backward()
  #backpropagation....
  #Step gradient function....
  optimizer.step()
  
  predicted = torch.max(output.data, 1)[1] 
  correct += (predicted == var_y_batch).sum()
  #print(correct)
  if idx % 50 == 0:
    print('Epoch : {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\t Accuracy:{:.3f}%'.
    format(
      epoch, idx*len(X_batch), len(train_loader.dataset), 100.*idx / len(train_loader), loss.data[0], float(correct*100) / float(BATCH_SIZE*(idx+1))))
</code></pre>
<p><strong>Error : NotImplementedError:</strong></p>

username: ptrblck
cooked: <p>Answered <a href="https://discuss.pytorch.org/t/not-implemented-error/22165/5?u=ptrblck">here</a>.</p>

username: Anish_Pandey
cooked: <p>I used to have this problem. I had difficult time  to figure it out first. when I started looking deep into Model. I figure out that my forward function in the class  was defined inside the <strong>init</strong> function. May be this could be the reason for your problem too.</p>

username: harsh_kumar_Chourasi
cooked: <p>After  45 minutes wasting time, I finally saw your comment. Thank you</p>