ValueError: Expected input batch_size (25) to match target batch_size (100).

libra: <p>The error message is:<br>
ValueError: Expected input batch_size (25) to match target batch_size (100).<br>
How can I deal it? <img src="https://discuss.pytorch.org/images/emoji/apple/smiling_face_with_tear.png?v=12" title=":smiling_face_with_tear:" class="emoji" alt=":smiling_face_with_tear:" loading="lazy" width="20" height="20"></p>
<pre><code>import torch.nn as nn
import torch.nn.functional as F
class MyModel(nn.Module):
# in_channals : 前一層的layer的output的out_channels大小, 若為第一層則為圖片的channals
# out_channels : 每層計算後得到的輸出
# kernal_size : filter size
# stride : 步長
# padding

def __init__(self):
    # 此處是定義你所需的layer，並非真正執行的地方
    super(MyModel, self).__init__()

    #Convolution Layer 01 start here
    self.conv_01 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
    self.relu_01 = nn.ReLU()
    self.maxpool_01 = nn.MaxPool2d(kernel_size=2, stride=2)
    #Convolution Layer 01 end here


    #Convolution Layer 02 start here
    self.conv_02 = nn.Conv2d(in_channels=16, out_channels=8, kernel_size=3, stride=1, padding=1)
    self.relu_02 = nn.ReLU()
    self.maxpool_02 = nn.MaxPool2d(kernel_size=2, stride=2)
    #Convolution Layer 02 end here


    #Convolution Layer 03 start here
    self.conv_03 = nn.Conv2d(in_channels=8, out_channels=4, kernel_size=3, stride=1, padding=1)
    self.relu_03 = nn.ReLU()
    self.maxpool_03 = nn.MaxPool2d(kernel_size=2, stride=2)
    #Convolution Layer 03 end here


    
    #Convolution Layer 04 start here
    self.conv_04 = nn.Conv2d(in_channels=4, out_channels=2, kernel_size=3, stride=1, padding=1)
    self.relu_04 = nn.ReLU()
    self.maxpool_04 = nn.MaxPool2d(kernel_size=2, stride=2)
    #Convolution Layer 04 end here



    
    #Convolution Layer 05 start here
    self.conv_05 = nn.Conv2d(in_channels=2, out_channels=2, kernel_size=3, stride=1, padding=1)
    self.relu_05 = nn.ReLU()
    self.maxpool_05 = nn.MaxPool2d(kernel_size=2, stride=2)
    #Convolution Layer 05 end here



    #fully connected layer start here
    self.fc = nn.Linear(8 * 4 * 4, 4)
    self.fc2 = nn.Linear(4, 2)
    self.softmax = nn.LogSoftmax(dim=1)
    #fully connected layer end here

    

def forward(self, x): 
    # 進行前向傳播
    # 真正執行的地方

    #example
    #x = self.conv_xx(x)
    #x = self.relu_xx(x)
    #x = self.maxpool_xx(x)
    
    #Convolution Layer 01 start here
    x = self.conv_01(x)
    x = self.relu_01(x)
    x = self.maxpool_01(x)
    #Convolution Layer 01 end here


    #Convolution Layer 02 start here
    x = self.conv_02(x)
    x = self.relu_02(x)
    x = self.maxpool_02(x)
    #Convolution Layer 02 end here

    #Convolution Layer 03 start here
    x = self.conv_03(x)
    x = self.relu_03(x)
    x = self.maxpool_03(x)
    #Convolution Layer 03 end here

    #Convolution Layer 04 start here
    x = self.conv_04(x)
    x = self.relu_04(x)
    x = self.maxpool_04(x)
    #Convolution Layer 04 end here



    
    #Convolution Layer 05 start here
    x = self.conv_05(x)
    x = self.relu_05(x)
    x = self.maxpool_05(x)
    #Convolution Layer 05 end here

    #fully connected layer start here
    x = x.view(-1, 8 * 4 * 4) # calucate by yourself
    x = self.fc(x)
    x = self.fc2(x)
    x = self.softmax(x)
    #fully connected layer end here


    return x


net = MyModel()
print(net)



from torch import optim
import numpy as np

# epochs:模型在訓練集重複訓練的總次數。一個epoch = 所有的資料量/batch_size
epochs = 40
trainloss = []
validloss = []

criterion = nn.CrossEntropyLoss()
# criterion = nn.NLLLoss()

# SGD-準確率梯度下降法
# lr為learning rate
# optimzier的作用就是需要根據神經網路反向傳播的梯度訊息来更新網路的參數，用以降低loss
optimizer = optim.Adam(net.parameters(), lr=0.001)

device = torch.device("cuda:0")

for epoch in range(epochs):

# batch_idx為第幾個batch
# data內含圖片以及label
for batch_idx, data in enumerate(trainloader):
    inputs, labels = data

    # 歸零
    # optimizer.zero_grad()

    # 將data轉換成顯示卡格式並輸入顯示卡
    inputs = inputs.to(device)
    labels = labels.to(device)
    optimizer.zero_grad()

    # 將model轉換成顯示卡格式並輸入顯示卡
    net = net.to(device)

    # 將圖片輸入至model進行訓練
    outputs = net(inputs)

    # 計算loss
    loss = criterion(outputs, labels)

    # 反向傳播
    loss.backward()

    # 更新權重參數
    optimizer.step()
    
    # 印出訓練過程的loss
    print('Epoch {}, Batch idx {}, loss {:.7f}'.format(epoch+1, batch_idx+1, loss.item()))
    
    if batch_idx % 20 == 0 :
      !nvidia-smi
trainloss.append(loss.data.cpu().numpy())

for batch_idx, data in enumerate(validloader):
    inputs, labels = data

    # 歸零
    # optimizer.zero_grad()

    # 將data轉換成顯示卡格式並輸入顯示卡
    inputs = inputs.to(device)
    labels = labels.to(device)
    optimizer.zero_grad()

    # 將model轉換成顯示卡格式並輸入顯示卡
    net = net.to(device)

    # 將圖片輸入至model進行訓練
    outputs = net(inputs)
    # 計算loss
    loss = criterion(outputs, labels)

    # 反向傳播
    loss.backward()

    # 更新權重參數
    optimizer.step()
validloss.append(loss.data.cpu().numpy())

print("Finish !")
plt.plot(trainloss ,label = 'Training loss')
plt.plot(validloss ,label = 'Validation loss')
plt.legend()
plt.show
</code></pre>

ptrblck: <p>Often these errors are caused by a wrong <code>view</code> operation. Replace <code>x = x.view(-1, 8 * 4 * 4)</code> with <code>x = x.view(x.size(0), -1)</code> and fix potential shape mismatch errors in the subsequent linear layer by adapting its <code>in_features</code>.</p>

libra: <p>Thankyou very much. <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=12" title=":grinning:" class="emoji" alt=":grinning:" loading="lazy" width="20" height="20"></p>