Mat1 and mat2 shapes cannot be multiplied (256x320 and 640x8)

astra1234567: <p>Hi ! I have problem with my CNN model. I don’t understand why my model works for summary when I give (1, 64, 16) and why it doesn’t work for real input ( it is batch, so it is tensor of inputs (256, 1, 64, 16)).  I use torch.utils.data.DataLoader.<br>
Summary:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02d0d19ad642c5144cbcfea09210165deefee63f.png" data-download-href="https://discuss.pytorch.org/uploads/default/02d0d19ad642c5144cbcfea09210165deefee63f" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02d0d19ad642c5144cbcfea09210165deefee63f.png" alt="image" data-base62-sha1="oUlwdeptO3lAP0KKbJmJ8CsBsz" width="581" height="500" data-dominant-color="161616"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">602×518 10.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>My model:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b0b909ddb654a023fe0434112166c81c0a4c932.png" data-download-href="https://discuss.pytorch.org/uploads/default/6b0b909ddb654a023fe0434112166c81c0a4c932" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b0b909ddb654a023fe0434112166c81c0a4c932.png" alt="image" data-base62-sha1="fgXRKU4ph3fKi5o4sBta8bTF71g" width="514" height="500" data-dominant-color="212323"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">871×847 33 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Error:<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x320 and 640x8)<br>
and it goes ahead in this line:  self.fc1 = nn.Linear(64 * 5 * 2 , 8)</p>
<p>Can someone help me with this? Could you explain me, how solve this problem ? Thank you !</p>

InnovArul: <p>It doesn’t raise any error for me in pytorch 1.12.1. Not sure why it didn’t work for you.</p><aside class="onebox githubgist" data-onebox-src="https://gist.github.com/InnovArul/7af1684ccc50fa245954da2cb4a5eca1">
  <header class="source">

      <a href="https://gist.github.com/InnovArul/7af1684ccc50fa245954da2cb4a5eca1" target="_blank" rel="noopener">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/InnovArul/7af1684ccc50fa245954da2cb4a5eca1" target="_blank" rel="noopener">https://gist.github.com/InnovArul/7af1684ccc50fa245954da2cb4a5eca1</a></h4>

  <h5>cnn_sample.py</h5>
  <pre><code class="Python">from turtle import forward
import torch
import torch.nn as nn, torch.nn.functional as F

class CNN(nn.Module):
    def __init__(self) -&gt; None:
        super().__init__()

        self.conv1 = nn.Conv2d(in_channels=1, out_channels=8, kernel_size=6, stride=1, padding=2)
        self.RL1 = nn.ReLU()</code></pre>
    This file has been truncated. <a href="https://gist.github.com/InnovArul/7af1684ccc50fa245954da2cb4a5eca1" target="_blank" rel="noopener">show original</a>

<p>
</p>

  </article>

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

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

<p>P.S: If you paste the code directly here instead of an image, it’s easier for others to execute the code and you might get a faster reply <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

astra1234567: <p><a class="mention" href="/u/innovarul">@InnovArul</a> Thank you for your help. I found mistake. It was in pre-processing data. In your example, you used  <code>d = torch.randn(256, 1, 64, 16)</code> but what if I will use <code>d = torch.randn(1, 64, 16)</code>, I know it will be probably error. It is possible use single tensor (1, 64, 16) if I trained model with batch (256, 1, 64, 16) ?</p>

InnovArul: <p>The <code>nn</code> modules are written to work on batches of data. i.e., they expect a batch dimension at 0’th position. To pass one data point, you can use batch dimension with size 1. i.e., data of size <code>(1, 1, 64, 16)</code>.</p>