NotImplementedError Traceback (most recent call last)

Sakhiwo_Mtwenka: <p>Hi Thre</p>
<p>How do I fix the below error:</p>
<pre><code class="lang-python">NotImplementedError                       Traceback (most recent call last)
&lt;ipython-input-2-db4fff233cd4&gt; in &lt;module&gt;
      6 input = torch.rand(5,3)
      7 print(input)
----&gt; 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:
--&gt; 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) -&gt; None:
--&gt; 223         raise NotImplementedError
    224 
    225     r"""Defines the computation performed at every call.

NotImplementedError: 
</code></pre>
<p>My code is below:</p>
<pre><code class="lang-python"># 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)
</code></pre>

ptrblck: <p>You have a typo in <code>forward</code> (you are using <code>foward</code>), so you would need to fix this.<br>
Once this is done, remove the <code>nn.Softmax</code>, as <code>nn.CrossEntropyLoss</code> expects raw logits.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging these issues easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> I’ve formatted the code for you.</p>

Sakhiwo_Mtwenka: <p>Thank you so much, fixing the typo error and add the below code worked:</p>
<p>images = images.view(images.shape[0], -1)</p>

Ruchi_Tiwari: <h1><a name="test-the-quantized-model-1" class="anchor" href="#test-the-quantized-model-1"></a>Test the quantized model</h1>
<p>total = 0<br>
correct = 0<br>
start = time.time()</p>
<p>with torch.no_grad():<br>
for data in testloader:<br>
images, labels = data</p>
<pre><code>    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()
</code></pre>
<p>print(‘Testing Completed in: {} secs’.format(time.time()-start))<br>
print(‘Test accuracy : {:.2f}% - {}’.format((correct/total)*100, ‘Quantized’))</p>
<h1><a name="save-the-quantized-model-2" class="anchor" href="#save-the-quantized-model-2"></a>Save the quantized model</h1>
<h2><a name="torchsavemodelstate_dict-mnist_resnet_quantizedpth-printquantized-model-saved-as-mnist_resnet_quantizedpth-3" class="anchor" href="#torchsavemodelstate_dict-mnist_resnet_quantizedpth-printquantized-model-saved-as-mnist_resnet_quantizedpth-3"></a>torch.save(model.state_dict(), ‘mnist_resnet_quantized.pth’)<br>
print(“Quantized model saved as ‘mnist_resnet_quantized.pth’”)</h2>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in &lt;cell line: 6&gt;()<br>
11             images, labels = images.cuda(), labels.cuda()<br>
12<br>
—&gt; 13         outputs = model(images)<br>
14         _, predicted = torch.max(outputs.data, 1)<br>
15         total += labels.size(0)</p>
<p>5 frames<br>
/usr/local/lib/python3.10/dist-packages/torch/_ops.py in <strong>call</strong>(self, *args, **kwargs)<br>
500         # We save the function ptr as the <code>op</code> attribute on<br>
501         # OpOverloadPacket to access it here.<br>
 → 502         return self._op(*args, **kwargs or {})<br>
503<br>
504     # TODO: use this to make a <strong>dir</strong></p>
<p>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 <a href="https://fburl.com/ptmfixes" class="inline-onebox" rel="noopener nofollow ugc">Internal Login</a> 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</p>