Size mismatch, m1: [5 x 44944], m2: [400 x 120] im new at this please help

username: Manasvi_Varma
cooked: <p>torch.Size([3, 224, 224])</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 6, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 5 * 5, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)</p>
<pre><code>def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(x.size(0), 16*53*53)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x
</code></pre>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)</p>
<p>for epoch in range(2):  # loop over the dataset multiple times</p>
<pre><code>running_loss = 0.0
for i, data in enumerate(train_loader, 0):
    # get the inputs; data is a list of [inputs, labels]
    inputs, labels = data

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = net(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 2000 == 1999:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 2000))
        running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)Preformatted text</p>
<blockquote>
<p>RuntimeError  Traceback (most recent call last)</p>
</blockquote>
<blockquote>
<p>RuntimeError: size mismatch, m1: [5 x 44944], m2: [400 x 120] at …\aten\src\TH/generic/THTensorMath.cpp:41</p>
</blockquote>

username: planet_pluto
cooked: <p>Can you post the complete stack trace and also format the code?</p>

username: planet_pluto
cooked: <p>I ran the code and the mistake is this:</p>
<p>In your model definition, the first linear layer (<code>self.fc1</code>) takes as input 400 neurons whereas during the <code>forward()</code> call, the input to the <code>self.fc1()</code> has the shape <code>torch.Size([1, 44944])</code>. So change your model definition</p>
<p>from:<br>
<code>self.fc1 = nn.Linear(16*5*5, 120)</code><br>
to:<br>
<code>self.fc1 = nn.Linear(16*53*53, 120)</code></p>

username: Manasvi_Varma
cooked: <p>Target 18 is out of bounds error  when i changed it</p>
<blockquote>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 6, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 53 * 53, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)<br>
def forward(self, x):<br>
x = self.pool(F.relu(self.conv1(x)))<br>
x = self.pool(F.relu(self.conv2(x)))<br>
x = x.view(x.size(0), 16<em>53</em>53)<br>
x = F.relu(self.fc1(x))<br>
x = F.relu(self.fc2(x))<br>
x = self.fc3(x)<br>
return x</p>
</blockquote>
<blockquote>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)</p>
</blockquote>
<blockquote>
<p>for epoch in range(2):  # loop over the dataset multiple times</p>
<pre><code>running_loss = 0.0
for i, data in enumerate(train_loader, 0):
    # get the inputs; data is a list of [inputs, labels]
    inputs, labels = data

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = net(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 2000 == 1999:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 2000))
        running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)</p>
</blockquote>
<p>error:<br>
IndexError                                Traceback (most recent call last)<br>
 in <br>
11         # forward + backward + optimize<br>
12         outputs = net(inputs)<br>
—&gt; 13         loss = criterion(outputs, labels)<br>
14         loss.backward()<br>
15         optimizer.step()</p>
<p>IndexError: Target 18 is out of bounds.</p>

username: RaLo4
cooked: <p>Your last layer</p>
<aside class="quote no-group" data-username="Manasvi_Varma" data-post="5" data-topic="100008">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manasvi_varma/48/30059_2.png" class="avatar"> Manasvi_Varma:</div>
<blockquote>
<p>self.fc3 = nn.Linear(84, 10)</p>
</blockquote>
</aside>
<p>has only 10 output neurons which means you classify into 10 classes.</p>
<p>But your targets/labels go even higher than that.</p>
<aside class="quote no-group" data-username="Manasvi_Varma" data-post="5" data-topic="100008">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manasvi_varma/48/30059_2.png" class="avatar"> Manasvi_Varma:</div>
<blockquote>
<p>—&gt; 13 loss = criterion(outputs, labels)</p>
</blockquote>
</aside>
<p>Your loss-function gets the output of your network, which classifies into 10 classes, but gets <code>class 18</code> as ground-truth target/label, thats why it says:</p>
<aside class="quote no-group" data-username="Manasvi_Varma" data-post="5" data-topic="100008">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manasvi_varma/48/30059_2.png" class="avatar"> Manasvi_Varma:</div>
<blockquote>
<p>IndexError: Target 18 is out of bounds.</p>
</blockquote>
</aside>
<p>Change the <code>10</code> in this line to the number of classes your dataset has.</p>
<aside class="quote no-group" data-username="Manasvi_Varma" data-post="5" data-topic="100008">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manasvi_varma/48/30059_2.png" class="avatar"> Manasvi_Varma:</div>
<blockquote>
<p>self.fc3 = nn.Linear(84, 10)</p>
</blockquote>
</aside>