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)

Test the quantized model

total = 0
correct = 0
start = time.time()

with torch.no_grad():
for data in testloader:
images, labels = data

    if torch.cuda.is_available():
        images, labels = images.cuda(), labels.cuda()
    
    outputs = model(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum().item()

print(‘Testing Completed in: {} secs’.format(time.time()-start))
print(‘Test accuracy : {:.2f}% - {}’.format((correct/total)*100, ‘Quantized’))

Save the quantized model

torch.save(model.state_dict(), ‘mnist_resnet_quantized.pth’)
print(“Quantized model saved as ‘mnist_resnet_quantized.pth’”)

NotImplementedError Traceback (most recent call last)
in <cell line: 6>()
11 images, labels = images.cuda(), labels.cuda()
12
—> 13 outputs = model(images)
14 _, predicted = torch.max(outputs.data, 1)
15 total += labels.size(0)

5 frames
/usr/local/lib/python3.10/dist-packages/torch/_ops.py in call(self, *args, **kwargs)
500 # We save the function ptr as the op attribute on
501 # OpOverloadPacket to access it here.
→ 502 return self._op(*args, **kwargs or {})
503
504 # TODO: use this to make a dir

NotImplementedError: Could not run ‘quantized::conv2d.new’ with arguments from the ‘CUDA’ backend. This could be because the operator doesn’t exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit Internal Login for possible resolutions. ‘quantized::conv2d.new’ is only available for these backends: [QuantizedCPU, QuantizedCUDA, BackendSelect, Python, FuncTorchDynamicLayerBackMode, Functionalize, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradMPS, AutogradXPU, AutogradHPU, AutogradLazy, AutogradMeta, Tracer, AutocastCPU, AutocastCUDA, FuncTorchBatched, FuncTorchVmapMode, Batched, VmapMode, FuncTorchGradWrapper, PythonTLSSnapshot, FuncTorchDynamicLayerFrontMode, PythonDispatcher]. what kind of error is this in mnist dataset after quantized model and eval on test