ValueError: Expected input batch_size (72) to match target batch_size (32)

ycy: <p>I’m getting the error <code>ValueError: Expected input batch_size (72) to match target batch_size (32).</code> for the following code:<br>
import torch<br>
import torch.nn as nn<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
from torch.utils.data import Dataset<br>
from torch.utils.data import DataLoader<br>
import pandas as pd</p>
<p>class DiabetesDataset(Dataset):<br>
def <strong>init</strong>(self, filepath):<br>
data = pd.read_csv(filepath)<br>
self.len = data.shape[0]  # shape[0]指行数，shape[1]指列数，data.shape返回的是行数和列数<br>
self.x_data = torch.tensor(np.array(data)[:, :-1].astype(np.float32))<br>
# 第二列到最后一列的数<br>
self.y_data = torch.tensor(np.array(data)[:, [0]].astype(np.float32))</p>
<pre><code>def __getitem__(self, index):
    return self.x_data[index], self.y_data[index]

def __len__(self):
    return self.len
</code></pre>
<p>trainsets = DiabetesDataset(‘data/us-101.csv’)<br>
testsets = DiabetesDataset(‘data/us-101.csv’)</p>
<p>batch_size = 32<br>
epoches = 40<br>
count=0</p>
<p>train_loader =DataLoader(dataset=trainsets, batch_size=batch_size, shuffle=True)<br>
test_loader = DataLoader(dataset=testsets, batch_size=batch_size, shuffle=False)</p>
<p><span class="hashtag">#构建模型</span><br>
class LSTM_Model(nn.Module):<br>
def <strong>init</strong>(self, input_dim, hidden_dim, layer_dim, output_dim):<br>
super(LSTM_Model, self).<strong>init</strong>()<br>
self.hidden_dim = hidden_dim<br>
self.layer_dim = layer_dim<br>
self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True)<br>
self.fc = nn.Linear(hidden_dim, 64)<br>
self.fc1 = nn.Linear(64, output_dim)<br>
self.so = nn.Softmax(dim=1)<br>
self.drop = nn.Dropout(0.3)<br>
self.relu = nn.ReLU()</p>
<pre><code>def forward(self, x):
    ho = torch.zeros(self.layer_dim, x.size(0),  self.hidden_dim).requires_grad_().to(device)
    co = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_().to(device)
    out, (hn, cn) = self.lstm(x, (ho.detach(), co.detach()))
    out = self.so(self.drop(self.fc1(self.relu(self.drop(self.fc(out[:, -1, :]))))))
    return out
</code></pre>
<p><span class="hashtag">#参数设置</span><br>
input_dim = 4<br>
hidden_dim = 128<br>
layer_dim = 1<br>
output_dim = 3<br>
sequence_dim = 1<br>
iter = 0</p>
<p><span class="hashtag">#模型调用</span><br>
model = LSTM_Model(input_dim, hidden_dim, layer_dim, output_dim)</p>
<p><span class="hashtag">#使用Gpu</span><br>
device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’)<br>
model.to(device)<br>
<span class="hashtag">#构建损失器和优化器</span><br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)</p>
<h1>
<a name="length-lenlistmodelparameters-1" class="anchor" href="#length-lenlistmodelparameters-1"></a>length = len(list(model.parameters()))</h1>
<h1>
<a name="for-i-in-rangelength-2" class="anchor" href="#for-i-in-rangelength-2"></a>for i in range(length):</h1>
<h1>
<a name="print-d-i1-3" class="anchor" href="#print-d-i1-3"></a>print(‘参数： %d’ % (i+1))</h1>
<h1>
<a name="printlistmodelparametersisize-4" class="anchor" href="#printlistmodelparametersisize-4"></a>print(list(model.parameters())[i].size())</h1>
<p>loss_list = []<br>
accurary_list = []<br>
iteration_list = []</p>
<p><span class="hashtag">#模型训练</span><br>
if <strong>name</strong> == ‘<strong>main</strong>’:<br>
for epoch in range(epoches):<br>
for i, (images, labels) in enumerate(train_loader, 0):<br>
images = images.view(-1,sequence_dim, input_dim).requires_grad_().to(device)<br>
labels = labels.to(device)<br>
optimizer.zero_grad()<br>
outputs = model(images)</p>
<pre><code>        print(outputs.shape)
        print(labels.shape)
        loss = criterion(outputs, labels.squeeze(dim=1).long())
        # loss = criterion(outputs, labels)
        optimizer.step()
        iter += 1
        if iter % 500 == 0:
            model.eval()
            correct = 0.0
            total = 0.0
            with torch.no_grad():
                for images, labels in test_loader:
                    images = images.view(-1, sequence_dim, input_dim).to(device)
                    outputs = model(images)
                    _, predict = torch.max(outputs.data, dim=1)
                    total += labels.size(0)
                    if torch.cuda.is_available():
                        correct += (predict.gpu() == labels.gpu()).sum()
                    else:
                        correct += (predict == labels).sum()
                accurary = correct / total
                loss_list.append(loss.data)
                accurary_list.append(accurary)
                iteration_list.append(iter)
                print("loop: {}, Loss: {}".format(iter, loss.item()))
                print('Accuracy on test set: %d %%' % (correct / total))
</code></pre>
<p><span class="hashtag">#可视化</span><br>
plt.plot(iteration_list, loss_list)<br>
plt.xlabel(‘Number of Iteration’)<br>
plt.ylabel(‘Loss’)<br>
plt.title(‘LSTM’)<br>
plt.show()</p>
<p>plt.plot(iteration_list, accurary_list)<br>
plt.xlabel(‘Number of Iteration’)<br>
plt.ylabel(‘accurary’)<br>
plt.title(‘LSTM’)<br>
plt.show()</p>
<p>the traceback is:<br>
torch.Size([72, 3])<br>
torch.Size([32, 1])<br>
Traceback (most recent call last):<br>
File “C:\Users\yin\PycharmProjects\ngsim\predict.py”, line 91, in <br>
loss = criterion(outputs, labels.squeeze(dim=1).long())<br>
File “C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 1194, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\loss.py”, line 1174, in forward<br>
return F.cross_entropy(input, target, weight=self.weight,<br>
File “C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py”, line 3026, in cross_entropy<br>
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)<br>
ValueError: Expected input batch_size (72) to match target batch_size (32).</p>
<p>Can anyone help me, please? Thanks</p>

ptrblck: <p>Check if these lines of code:</p>
<pre><code class="lang-python">images = images.view(-1,sequence_dim, input_dim).requires_grad_().to(device)
labels = labels.to(device)
</code></pre>
<p>change the batch size for <code>images</code> since you are moving the “rest” of the elements to <code>dim0</code>.<br>
If so, change the <code>view</code> operation to <code>images = images.view(images.size(0), sequence_dim, -1)</code> and fix potential shape mismatches caused by a wrong number of features.</p>
<p>Also, since you are using <code>nn.CrossEntropyLoss</code>, which expects raw logits as the model output, remove the <code>nn.Softmax</code> layer in your model applied to the outputs.</p>

ycy: <p>Through your guidance, I resolved the issue of appeal,however,there is another problem：</p>
<pre><code class="lang-auto">class DiabetesDataset(Dataset):
    def __init__(self, filepath):
        data = pd.read_csv(filepath)
        self.len = data.shape[0]  # shape[0]指行数，shape[1]指列数，data.shape返回的是行数和列数
        self.x_data = torch.tensor(np.array(data)[:, :-1].astype(np.float32))
        # 第二列到最后一列的数
        self.y_data = torch.tensor(np.array(data)[:, [0]].astype(np.float32))

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

trainsets = DiabetesDataset('data/us-101.csv')
testsets = DiabetesDataset('data/us-101.csv')

batch_size = 32
epoches = 40
count=0

train_loader =DataLoader(dataset=trainsets, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=testsets, batch_size=batch_size, shuffle=False)

#构建模型
class LSTM_Model(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
        super(LSTM_Model, self).__init__()
        self.hidden_dim = hidden_dim
        self.layer_dim = layer_dim
        self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, 64)
        self.fc1 = nn.Linear(64, output_dim)
        self.so = nn.Softmax(dim=1)
        self.drop = nn.Dropout(0.3)
        self.relu = nn.ReLU()

    def forward(self, x):
        ho = torch.zeros(self.layer_dim, x.size(0),  self.hidden_dim).requires_grad_()#.to(device)
        co = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()#.to(device)
        out, (hn, cn) = self.lstm(x, (ho.detach(), co.detach()))
        out = self.so(self.drop(self.fc1(self.relu(self.drop(self.fc(out[:, -1, :]))))))
        return out

#参数设置
input_dim = 9
hidden_dim = 128
layer_dim = 1
output_dim = 3
sequence_dim = 1
iter = 0

#模型调用
model = LSTM_Model(input_dim, hidden_dim, layer_dim, output_dim)

#使用Gpu
#device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
#model.to(device)
#构建损失器和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# length = len(list(model.parameters()))
# for i in range(length):
#     print('参数： %d' % (i+1))
#     print(list(model.parameters())[i].size())

loss_list = []
accurary_list = []
iteration_list = []

#模型训练
if __name__ == '__main__':
    for epoch in range(epoches):
        for i, (images, labels) in enumerate(train_loader, 0):
            images = images.view(images.size(0), sequence_dim, -1)#.requires_grad_().to(device)
            #labels = labels.to(device)
            optimizer.zero_grad()
            outputs = model(images)

            print(outputs.shape)
            print(labels.shape)
            loss = criterion(outputs, labels.squeeze(dim=1).long())
            # loss = criterion(outputs, labels)
            optimizer.step()
            iter += 1
            if iter % 500 == 0:
                model.eval()
                correct = 0.0
                total = 0.0
                with torch.no_grad():
                    for images, labels in test_loader:
                        images = images.view(-1, sequence_dim, input_dim)#.to(device)
                        outputs = model(images)
                        _, predict = torch.max(outputs.data, dim=1)
                        total += labels.size(0)
                        if torch.cuda.is_available():
                            correct += (predict.gpu() == labels.gpu()).sum()
                        else:
                            correct += (predict == labels).sum()
                    accurary = correct / total
                    loss_list.append(loss.data)
                    accurary_list.append(accurary)
                    iteration_list.append(iter)
                    print("loop: {}, Loss: {}".format(iter, loss.item()))
                    print('Accuracy on test set: %d %%' % (correct / total))



#可视化
plt.plot(iteration_list, loss_list)
plt.xlabel('Number of Iteration')
plt.ylabel('Loss')
plt.title('LSTM')
plt.show()

plt.plot(iteration_list, accurary_list)
plt.xlabel('Number of Iteration')
plt.ylabel('accurary')
plt.title('LSTM')
plt.show()
</code></pre>
<p>the traceback is：</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\yin\PycharmProjects\ngsim\predict.py", line 92, in &lt;module&gt;
    loss = criterion(outputs, labels.squeeze(dim=1).long())
  File "C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\loss.py", line 1174, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\yin\miniconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 3026, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
IndexError: Target 372 is out of bounds.

</code></pre>
<p>I will appreciate it if you can help me.</p>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects model outputs containing raw logits in the shape <code>[batch_size, nb_classes, *]</code> and target in the shape <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code>.<br>
Based on the error message you are trying to use a target class index of <code>372</code> while the model output represents logits for less classes.<br>
In your model you are defining <code>output_dim = 3</code> which indicates you are dealing with 3 classes and the targets should thus contain values in <code>[0, 1, 2]</code>.</p>