NotImplementedError:

Sandeep_Kumar_GITAM: <pre><code class="lang-auto">class CNN(nn.Module):
    """CNN."""

    
    def __init__(self):
        """CNN Builder."""
        super(CNN, self).__init__()

        self.conv_layer = nn.Sequential(

            # Conv Layer block 1
            KernelConv2d(in_channels=k, out_channels=30, kernel_size=3, padding=1),
            nn.BatchNorm2d(30),
            nn.ReLU(inplace=True),
            KernelConv2d(in_channels=30, out_channels=60, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            # Conv Layer block 2
            KernelConv2d(in_channels=60, out_channels=60, kernel_size=3, padding=1),
            nn.BatchNorm2d(60),
            nn.ReLU(inplace=True),
            KernelConv2d(in_channels=60, out_channels=60, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Dropout2d(p=0.05),

            # Conv Layer block 3
            KernelConv2d(in_channels=60, out_channels=120, kernel_size=3, padding=1),
            nn.BatchNorm2d(120),
            nn.ReLU(inplace=True),
            KernelConv2d(in_channels=120, out_channels=120, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),
        )
        
        self.fc_layer = nn.Sequential(
            nn.Dropout(p=0.1),
            nn.Linear(4096, 480),
            nn.ReLU(inplace=True),
            nn.Linear(1024, 512),
            nn.ReLU(inplace=True),
            nn.Dropout(p=0.1),
            nn.Linear(512, output_units)
        )



def forward(self, x):
    """Perform forward."""
    
    # conv layers
    x = self.conv_layer(x)
    
    # flatten
    x = x.view(x.size(0), -1)
    
    # fc layer
    x = self.fc_layer(x)

    return x
from tensorflow.keras.optimizers import Adam
model = CNN().to(device)
# defining the optimizer
optimizer = Adam(model.parameters())#, learning_rate=learning_rate)
# defining the loss function
criterion = nn.CrossEntropyLoss()
# checking if GPU is available
if torch.cuda.is_available():
    model = model.cuda()
    criterion = criterion.cuda()
    
print(model)

Dear friends, I am getting "NotImplementedError:" for the following code. Can anyone please help me in resolving the following error.

from torchvision import models
from torchsummary import summary
model = CNN().to(device)
summary(model,(k,9,9))
</code></pre>
<hr>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in ()<br>
2 from torchsummary import summary<br>
3 model = CNN().to(device)<br>
----&gt; 4 summary(model,(k,9,9))</p>
<p>2 frames<br>
/usr/local/lib/python3.7/dist-packages/torchsummary/torchsummary.py in summary(model, input_size, batch_size, device)<br>
70     # make a forward pass<br>
71     # print(x.shape)<br>
—&gt; 72     model(*x)<br>
73<br>
74     # remove these hooks</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1050                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1051             return forward_call(*input, **kwargs)<br>
1052         # Do not call functions when jit is used<br>
1053         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _forward_unimplemented(self, *input)<br>
199         registered hooks while the latter silently ignores them.<br>
200     “”"<br>
 → 201     raise NotImplementedError<br>
202<br>
203</p>
<p>NotImplementedError:</p>

ptrblck: <p>The indentation in the posted code snippet are wrong and <code>def forward</code> should be a class method of <code>CNN</code>.</p>