Multiple classification with cnn

Jake1: <p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 32, 5)<br>
self.conv2 = nn.Conv2d(32, 64, 5)<br>
self.conv3 = nn.Conv2d(64, 128, 5)<br>
self.pool = nn.MaxPool2d(2, 2)</p>
<pre><code>    self.fc1 = nn.Linear(128 * 9 * 9, 256)
    self.fc2 = nn.Linear(256, 128)
    self.fc3 = nn.Linear(128, 84)
    self.fc4 = nn.Linear(84, 10)

    self.dropout1 = nn.Dropout(p=0.5, inplace=False)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

    x = F.relu(self.conv3(x))
    #x = self.dropout1(x)
    x = x.view(-1, 128*9*9)
    x = F.relu(self.fc1(x))
    x = self.dropout1(x)
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    x = self.fc4(x)
    return x
</code></pre>
<p>this is cnn with inputsize(3,64,64)</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(trainloader, 0):
    # get the inputs; data is a list of [inputs, labels]
    inputs, labels = data
    # zero the parameter gradients
    inputs = concat_img(inputs)

    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = net(inputs)

    labels = torch.stack(labels.split(4))
    one_hot_labels = torch.zeros(inputs.size(0), 10, dtype=torch.long).scatter_(1, labels, 1)
    one_hot_labels = one_hot_labels.float()

    loss = criterion(outputs, one_hot_labels)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 2000 == 1999:    # print every 2000 mini-batches
        print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
        running_loss = 0.0
</code></pre>
<p>but with this code, there is a value eror: Target size (torch.Size([3, 10])) must be the same as input size (torch.Size([1, 10])) what should i do? And there is no decrese of loss at all. help me please</p>

ptrblck: <p>Change:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.view(-1, 128*9*9)
</code></pre>
<p>to:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.view(x.size(0), -1)
</code></pre>
<p>to keep the batch size equal and adapt the <code>in_features</code> of the next linear layer, if needed.</p>

Jake1: <p>thanks for replying. but there is a still error in &gt;&gt;outputs=net(inputs) with<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x81 and 10368x256)<br>
i also want to know how to reduce loss.<br>
although there are lots of epoch, loss is still 0.680 .0.680 0.680 … like this<br>
I think it means there is no improvement in my model.</p>

ptrblck: <aside class="quote no-group" data-username="Jake1" data-post="3" data-topic="193253">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/73ab20/48.png" class="avatar"> Jake1:</div>
<blockquote>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x81 and 10368x256)</p>
</blockquote>
</aside>
<p>This would be expected as your <code>in_features</code> is set to a wrong value:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="193253">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>and adapt the <code>in_features</code> of the next linear layer, if needed.</p>
</blockquote>
</aside>
<p>Change the <code>in_features</code> of <code>self.fc1</code> to <code>81</code> and it should work.</p>