There is no problem when I run only the neural network part，but when i train it ,i meet the problem

username: jackson
cooked: <pre><code>class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv1d(in_channels=1, out_channels=10, kernel_size=1)
        self.max_pool1 = nn.MaxPool1d(kernel_size=1, stride=1)
        self.conv2 = nn.Conv1d(10, 20, 1, 1)
        self.max_pool2 = nn.MaxPool1d(1, 1)
        self.conv3 = nn.Conv1d(20, 40, 1, 1)
        self.liner1 = nn.Linear(20*10, 120)
        self.liner2 = nn.Linear(120, 84)
        self.liner3 = nn.Linear(84, 1)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.max_pool1(x)
        x = F.relu(self.conv2(x))
        x = self.max_pool2(x)
        x = F.relu(self.conv3(x))

        x = x.view(-1, 20*10)
        x = F.relu(self.liner1(x))
        x = F.relu(self.liner2(x))
        x = self.liner3(x)
        x = torch.unsqueeze(x, dim=1)
        x = torch.unsqueeze(x, dim=1)

        return x

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(10):
    running_loss = 0
    for input_data in x_train:
        i = 0
        label = -1.989e-12
        optimizer.zero_grad()

        outputs = net(input_data)
        loss = criterion(outputs, label)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        i += 1
        if i % 2000 == 1999:
             print('[%d,%5d] loss : %3f' % (epoch + 1, i + 1, running_loss / 2000))
             running_loss = 0.0
</code></pre>

username: jackson
cooked: <p>i meet the problem about:IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2),if someone have idea to solve the problem, please tell me, thank you</p>

username: ptrblck
cooked: <p>Your setup looks wrong regarding the used criterion, the model output, and the target.<br>
<code>nn.CrossEntropyLoss</code> is used for a multi-class classification or segmentation use case. The model outputs should thus have the shape <code>[batch_size, nb_classes, *]</code> and the target <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code> where the <code>*</code> denotes additional dimensions.<br>
In your example the model is returning an output in the shape <code>[batch_size, 1, 1, 1]</code> which would only represent a single class so that your model would only “learn” to output <code>class0</code> for all samples.<br>
However, the target is initialized as a negative Python float, which has the wrong shape, the wrong <code>type</code>, and doesn’t represent a class index.<br>
After fixing this by using <code>nn.MSELoss</code>, the code works fine, so feel free to update your code with a minimal and executable code snippet which reproduces the error.</p>

username: jackson
cooked: <p>Thank you for your reply.My neural network is for regression prediction, so am I using the wrong loss function?</p>

username: ptrblck
cooked: <p>Yes, <code>nn.CrossEntropyLoss</code> is used for a multi-class classification or segmentation use case as mentioned before and will raise the expected error.<br>
For a regression use case you might want to use e.g. <code>nn.MSELoss</code>.</p>