No errors before saving, but an error during testing

Mathews_Vinoy: <p>When training my model, I didn’t encounter any errors. However, after saving and loading the model, I encountered an error when testing it with the loaded model. mat1 and mat2 shapes cannot be multiplied (10x576 and 5760x12)</p>

ptrblck: <p>Could you post your model definition as well as the input shapes here, please?</p>

Mathews_Vinoy: <p>The input size of the model = torch.Size([3, 224, 224])</p>
<pre><code class="lang-auto">class TinyVGG(nn.Module):
    def __init__(self, input_shape: int, hidden_units: int, output_shape: int):
        super().__init__()
        self.block_1 = nn.Sequential(
            nn.Conv2d(in_channels=input_shape, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.block_2 = nn.Sequential(
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(in_features=hidden_units*24*24, out_features=output_shape)
        )

    def forward(self, x: torch.Tensor):
        return self.classifier(self.block_2(self.block_2(self.block_1(x))))


model_0 = TinyVGG(input_shape=3, hidden_units=10, output_shape=len(class_names))
</code></pre>

ptrblck: <p>Thanks for the code!<br>
I cannot reproduce any issues and the model works for me after loading it and calling <code>.eval()</code> on it (assuming that’s also your workflow):</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
import torch.nn as nn

class TinyVGG(nn.Module):
    def __init__(self, input_shape: int, hidden_units: int, output_shape: int):
        super().__init__()
        self.block_1 = nn.Sequential(
            nn.Conv2d(in_channels=input_shape, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.block_2 = nn.Sequential(
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=0),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(in_features=hidden_units*24*24, out_features=output_shape)
        )

    def forward(self, x: torch.Tensor):
        return self.classifier(self.block_2(self.block_2(self.block_1(x))))


model_0 = TinyVGG(input_shape=3, hidden_units=10, output_shape=10)

# test before saving
x = torch.randn(1, 3, 224, 224)
out = model_0(x)
out.mean().backward()

# save model
torch.save(model_0.state_dict(), "tmp.pt")

# restore model
model = TinyVGG(input_shape=3, hidden_units=10, output_shape=10)
model.load_state_dict(torch.load("tmp.pt"))
model.eval()

# test after restoring
x = torch.randn(1, 3, 224, 224)
out = model(x)
out.mean().backward()
</code></pre>

Mathews_Vinoy: <pre><code class="lang-auto">with torch.inference_mode():
    pred = model_0(data)
pred
</code></pre>
<h2><a name="p-458563-after-loading-this-code-i-and-testing-with-an-data-the-error-is-1" class="anchor" href="#p-458563-after-loading-this-code-i-and-testing-with-an-data-the-error-is-1"></a>after loading this code i and testing with an data<br>
the error is</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
Cell In[88], line 2<br>
1 with torch.inference_mode():<br>
----&gt; 2     pred = model_0(data)<br>
3 pred</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1552 else:<br>
 → 1553     return self._call_impl(*args, **kwargs)</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)<br>
1557 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1558 # this function, and just call forward.<br>
1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1560         or _global_backward_pre_hooks or _global_backward_hooks<br>
1561         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1562     return forward_call(*args, **kwargs)<br>
1564 try:<br>
1565     result = None</p>
<p>Cell In[65], line 27, in TinyVGG.forward(self, x)<br>
26 def forward(self, x: torch.Tensor):<br>
—&gt; 27     return self.classifier(self.block_2(self.block_2(self.block_1(x))))</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1552 else:<br>
 → 1553     return self._call_impl(*args, **kwargs)</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)<br>
1557 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1558 # this function, and just call forward.<br>
1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1560         or _global_backward_pre_hooks or _global_backward_hooks<br>
1561         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1562     return forward_call(*args, **kwargs)<br>
1564 try:<br>
1565     result = None</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/container.py:219, in Sequential.forward(self, input)<br>
217 def forward(self, input):<br>
218     for module in self:<br>
 → 219         input = module(input)<br>
220     return input</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1552 else:<br>
 → 1553     return self._call_impl(*args, **kwargs)</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)<br>
1557 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1558 # this function, and just call forward.<br>
1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1560         or _global_backward_pre_hooks or _global_backward_hooks<br>
1561         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1562     return forward_call(*args, **kwargs)<br>
1564 try:<br>
1565     result = None</p>
<p>File ~/anaconda3/lib/python3.12/site-packages/torch/nn/modules/linear.py:117, in Linear.forward(self, input)<br>
116 def forward(self, input: Tensor) → Tensor:<br>
 → 117     return F.linear(input, self.weight, self.bias)</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x576 and 5760x12)</p>

ptrblck: <p>No errors using my code:</p>
<pre data-code-wrap="python"><code class="lang-python">with torch.inference_mode():
    out = model(x)
print(out.shape)
# torch.Size([1, 10])

with torch.inference_mode():
    out = model_0(x)
print(out.shape)
# torch.Size([1, 10])
</code></pre>

Mathews_Vinoy: <p>Here are the links to the GitHub repository and the Kaggle dataset.</p>
<aside class="onebox githubrepo" data-onebox-src="https://github.com/MathewsVinoy/Pest_classification_using_CNN">
  <header class="source">

      <a href="https://github.com/MathewsVinoy/Pest_classification_using_CNN" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <div class="github-row" data-github-private-repo="false">
  <img width="690" height="344" src="https://opengraph.githubassets.com/2a9fd883502ffb9457303f2ff43e2d35/MathewsVinoy/Pest_classification_using_CNN" class="thumbnail">

  <h3><a href="https://github.com/MathewsVinoy/Pest_classification_using_CNN" target="_blank" rel="noopener nofollow ugc">GitHub - MathewsVinoy/Pest_classification_using_CNN</a></h3>

    <p><span class="github-repo-description">Contribute to MathewsVinoy/Pest_classification_using_CNN development by creating an account on GitHub.</span></p>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.kaggle.com/datasets/gauravduttakiit/agricultural-pests-dataset">
  <header class="source">
      <img src="https://www.kaggle.com/static/images/favicon.ico" class="site-icon" width="32" height="32">

      <a href="https://www.kaggle.com/datasets/gauravduttakiit/agricultural-pests-dataset" target="_blank" rel="noopener nofollow ugc">kaggle.com</a>
  </header>

  <article class="onebox-body">
    <img width="500" height="500" src="https://storage.googleapis.com/kaggle-datasets-images/new-version-temp-images/default-backgrounds-1.png-4760409/dataset-card.png" class="thumbnail onebox-avatar">

<h3><a href="https://www.kaggle.com/datasets/gauravduttakiit/agricultural-pests-dataset" target="_blank" rel="noopener nofollow ugc">Agricultural Pests Dataset</a></h3>

  <p>Agricultural Pests Classification</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I am implementing the same thing in the above code, but I am getting this error.</p>

Mathews_Vinoy: <p>Thank you, sir. I solved the problem. The error was because when testing with test data, I didn’t provide the unsquace. Thank you for helping me with this</p>
<pre data-code-wrap="python"><code class="lang-python">with torch.inference_mode():
    pred = model(data.unsqueeze(0))
pred

</code></pre>