Getting size mismatch error

carologawa: <p>I am creating designing a Convolutional Neural Network to predict labels of the images, using CIFAR10.</p>
<pre><code class="lang-auto">num_workers = 2
# how many samples per batch to load
BATCH_SIZE = 4
# percentage of training set to use as validation
valid_size = 0.2

# convert data to a normalized torch.FloatTensor
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((.5,.5,.5),(.5,.5,.5))])

# choose the training and test datasets
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

# prepare data loaders (combine dataset and sampler)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=num_workers)
testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=num_workers)

rgb_img = np.squeeze(images[3])

channels = ['red channel', 'green channel', 'blue channel']

fig = plt.figure(figsize = (36, 36)) 
for idx in np.arange(rgb_img.shape[0]):
    ax = fig.add_subplot(1, 3, idx + 1)
    img = rgb_img[idx]
    ax.imshow(img, cmap='gray')
    ax.set_title(channels[idx])
    width, height = img.shape
    thresh = img.max()/2.5

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(400, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
# create a complete CNN
net = Net()
print(net)

# move tensors to GPU if CUDA is available
if train_on_gpu:
    net.cuda()

n_epochs = 10           
BATCH_SIZE = 20
LR = 0.001    

# specify loss function
loss_func = nn.CrossEntropyLoss()

# specify optimizer
optimizer = optim.SGD(model.parameters(), lr=LR)

net.train()
for data, target in trainloader:
        # move tensors to GPU if CUDA is available
    if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        # clear the gradients of all optimized variables
    optimizer.zero_grad()
        # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)
        # calculate the batch loss
    loss = loss_func(output, target)
        # backward pass: compute gradient of the loss with respect to model parameters
    loss.backward()
        # perform a single optimization step (parameter update)
    optimizer.step()
        # update training loss
    train_loss += loss.item()*data.size(0)

# validate the model
net.eval()
for data, target in testloader:
        # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
        # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)[0]
        # calculate the batch loss
    loss = loss_func(output, target)
        # update average validation loss 
    valid_loss += loss.item()*data.size(0)
    
# calculate average losses
train_loss = train_loss/len(trainloader.dataset)
valid_loss = valid_loss/len(testloader.dataset)
train_losslist.append(train_loss)
        
# print training/validation statistics 
print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss)) 
</code></pre>
<p>Getting a mismatch error</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8172/3310213662.py in &lt;module&gt;
      8     output = net(data)[0]
      9         # calculate the batch loss
---&gt; 10     loss = loss_func(output, target)
     11         # update average validation loss
     12     valid_loss += loss.item()*data.size(0)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
   1161 
   1162     def forward(self, input: Tensor, target: Tensor) -&gt; Tensor:
-&gt; 1163         return F.cross_entropy(input, target, weight=self.weight,
   1164                                ignore_index=self.ignore_index, reduction=self.reduction,
   1165                                label_smoothing=self.label_smoothing)

~\anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2994     if size_average is not None or reduce is not None:
   2995         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2996     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
   2997 
   2998 

RuntimeError: size mismatch (got input: [10], target: [4])
</code></pre>

ptrblck: <p>Usually these shape mismatches are caused by a wrong <code>view</code> operation, which would change the batch size.<br>
In your code you are using:</p>
<pre><code class="lang-python">x = x.view(-1, 16 * 5 * 5)
</code></pre>
<p>which might indeed change the size of <code>dim0</code>.<br>
Use</p>
<pre><code class="lang-python">x = x.view(x.size(0), -1)
</code></pre>
<p>instead and check for shape mismatches in <code>self.fc1</code>. If a shape mismatch is raised in <code>self.fc1</code> set the <code>in_features</code> to the expected value (the feature dimension of the input activation, i.e. <code>x.size(1)</code> after the flattening operation).</p>

carologawa: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="148877">
<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>set the <code>in_features</code> to the expected value</p>
</blockquote>
</aside>
<p>Thank you so much!</p>
<p>If you could help me again… I got another error</p>
<p>IndexError                                Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_10600/2418918815.py in <br>
23     # calculate test accuracy for each object class<br>
24     for i in range(BATCH_SIZE):<br>
—&gt; 25         label = target.data[i]<br>
26         class_correct[label] += correct[i].item()<br>
27         class_total[label] += 1</p>
<p>IndexError: index 4 is out of bounds for dimension 0 with size 4</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<pre><code class="lang-auto">`test_loss = 0.0
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))

net.eval()
# iterate over test data
for data, target in testloader:
    # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
    # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)
    # calculate the batch loss
    loss = loss_func(output, target)
    # update test loss 
    test_loss += loss.item()*data.size(0)
    # convert output probabilities to predicted class
    _, pred = torch.max(output, 1)    
    # compare predictions to true label
    correct_tensor = pred.eq(target.data.view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
    
    # calculate test accuracy for each object class
    for i in range(BATCH_SIZE):
        label = target.data[i]
        class_correct[label] += correct[i].item()
        class_total[label] += 1`
</code></pre>

ptrblck: <p>Check the shape of <code>class_total</code> and make sure that al valid <code>label</code> indices are able to be used as an index.<br>
Currently it seems <code>class_total</code> has a size of <code>4</code> and will thus accept indices as <code>[0, 1, 2, 3]</code> while you are passing an index as <code>4</code> to it.</p>