NotImplementedError Traceback (most recent call last)

Hi Thre

How do I fix the below error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-db4fff233cd4> in <module>
      6 input = torch.rand(5,3)
      7 print(input)
----> 8 out = model(input)
      9 for epoch in range(2):
     10     running_loss = 0.0

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _forward_unimplemented(self, *input)
    221     # https://github.com/python/mypy/issues/8795
    222     def _forward_unimplemented(self, *input: Any) -> None:
--> 223         raise NotImplementedError
    224 
    225     r"""Defines the computation performed at every call.

NotImplementedError: 

My code is below:

# Setting up the environment
import torch
import torchvision
from torchvision import transforms, datasets
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import torch.optim as optim

#Global variables
batch_size = 10
num_images_display = 60

#Load data                          
train_data = torch.utils.data.DataLoader(
  torchvision.datasets.MNIST('', train=True, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize(
                                (0.1307,), (0.3081,))
                             ])),
  batch_size=batch_size, shuffle=True)

test_data = torch.utils.data.DataLoader(
  torchvision.datasets.MNIST('', train=False, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize(
                                (0.1307,), (0.3081,))
                             ])),
  batch_size=batch_size, shuffle=True)

#Neural network class
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.L1 = nn.Linear(28 * 28,512)
        self.L2 = nn.Linear(512, 10)
        
    def foward(self, x):
        x = self.L1(x)
        x = self.L2(x)
        x = nn.Softmax(x, dim = 1)
        return x
    
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Net().to(device)
print(model)

# Train the model / network
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)
model.train()

for epoch in range(2):
    running_loss = 0.0
    for images, labels in train_data:
        
        # Zero the parameter gradients and training pass
        optimizer.zero_grad()
    
       # Outputs
        outputs = model(images)

You have a typo in forward (you are using foward), so you would need to fix this.
Once this is done, remove the nn.Softmax, as nn.CrossEntropyLoss expects raw logits.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging these issues easier. :wink: I’ve formatted the code for you.

Thank you so much, fixing the typo error and add the below code worked:

images = images.view(images.shape[0], -1)