RuntimeError: Expected floating point type for target with class probabilities, got Long

I am getting this error, when using
nn.CrossEntropyLoss()

def train(model, device, train_loader, optimizer, epoch, batchSize, test_loader, ValidationPrice):
    model.train() # When you call train(), tensor function
    total_loss = 0
    total_classification = 0
    total_correct = 0

    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad() # Sets gradients of all model parameters to zero
        output = model(data) #insert the actual data Tensor[Batch][Channels In Each Batch][Height of Matrix][Width of Matrix]
        loss = torch.nn.CrossEntropyLoss()
        print(target.long())
        print(output)
        output = output[:, -1] # THis is when we are using convolutional nets etc
        loss = loss(output, target.long()) #Error here
        loss.backward()
        optimizer.step()
        classification = output.detach().numpy()
        realTarget = target.numpy()

        for i in range (0, len(realTarget)):
            x = np.argmax(classification[i])
            if x == int(realTarget[i]):
                #print("correct")
                total_correct += 1

        total_loss += loss.item()
        total_classification += target.size(0)
        #if batch_idx % 10 == 0:
        #    print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
        #        epoch, batch_idx * len(data), len(train_loader.dataset),
        #        100. * batch_idx / len(train_loader), loss.item()))
    model_accuracy = total_correct / total_classification * 100
    ValidationResults = Validation(model, device, test_loader, ValidationPrice)
    print('epoch {0} total_correct: {1} loss: {2:.2f} acc: {3:.2f} '.format(
        epoch, total_correct, total_loss, model_accuracy))
    print(f"epoch {epoch} Validation Accuracy = {ValidationResults[0]}, Validation Simulation Predicted = {ValidationResults[1]}, Validation Simulations Real = {ValidationResults[2]}")


#Custom Network Architecture
class ConvNetwork(torch.nn.Module):
    def __init__(self, window):
        super(ConvNetwork, self).__init__()
        self.C1 = nn.Conv2d(in_channels=1, out_channels=3, kernel_size=(3,1))
        self.HiddenLayer1 = nn.Linear(3*window, window)
        self.HiddenLayer2 = nn.Linear(window, int(window/2))
        self.HiddenLayer3 = nn.Linear(int(window/2), int(window/4))
        self.OutputLayer = nn.Linear(int(window/4), 3)
        self.out = nn.LogSoftmax()
        self.dropout = nn.Dropout(0.25)

    def forward(self, x):
        #print(f"0 x.shape = {x.shape}")
        x = self.C1(x)
        print(f"1 x.shape = {x.shape}")
        x = x.reshape(x.shape[0], -1)
        print(f"2 x.shape = {x.shape}")
        x = self.HiddenLayer1(x)
        print(f"3 x.shape = {x.shape}")
        self.L1 = torch.relu(x)
        print(f"4 x.shape = {self.L1.shape}")
        self.L2 = torch.relu(self.HiddenLayer2(self.L1))
        x = self.L2
        print(f"5 x.shape = {x.shape}")
        self.L3 = torch.relu(self.HiddenLayer3(x))
        print(f"6 x.shape = {self.L3.shape}")
        self.result = self.OutputLayer(self.L3)
        print(f"7 x.shape = {self.result.shape}")
        self.Output = self.out(self.result)
        print(f"8 x.shape = {self.Output.shape}")
        return self.Output

#Error Message:
Traceback (most recent call last):
  File "C:/Users/premp/Desktop/Trading/Source/NNIndicator.py", line 635, in <module>
    train(net, device, Train_Loader, optimizer, epoch, batchSize, Test_Loader, ValidationPrice)
  File "C:/Users/premp/Desktop/Trading/Source/NNIndicator.py", line 35, in train
    loss = loss(output, target.long())
  File "C:\Users\premp\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\premp\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 1150, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\premp\anaconda3\lib\site-packages\torch\nn\functional.py", line 2846, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
RuntimeError: Expected floating point type for target with class probabilities, got Long


#Shapes 
0 x.shape = torch.Size([10, 1, 3, 12])
1 x.shape = torch.Size([10, 3, 1, 12])
2 x.shape = torch.Size([10, 36])
3 x.shape = torch.Size([10, 12])
4 x.shape = torch.Size([10, 12])
5 x.shape = torch.Size([10, 6])
6 x.shape = torch.Size([10, 3])
7 x.shape = torch.Size([10, 3])
8 x.shape = torch.Size([10, 3])

As you can see:

Initially I have a batch size of 10.
Input channel of 1
Number of Distinct Features = 3
Sequence Length of each of those features = 12

Most likely the shape of the targets for nn.CrossEntropyLoss is wrong.
Check the docs and the expected shape of the input and target. E.g. if you are passing class indices, make sure that the target contains the class indices (not one-hot encoded) as a LongTensor and thus does not use a “channel/class” dimension. If you are passing probabilities, make sure it’s a FloatTensor in the same shape as the model output.

1 Like

Hey, yeah I had realised I had not changed the line output = output[:-1], thank you for your response.