RuntimeError: Found dtype Long but expected Float

You can see the dtype of the b_y is Int64 and I am getting an error

Traceback (most recent call last):
  File "/usr/lib/python3.8/", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/", line 87, in _run_code
    exec(code, run_globals)
  File "/home/Opps_0/Desktop/MNIST/src/", line 69, in <module>
    train(NB_EPOCS, model, loaders)
  File "/home/Opps_0/Desktop/MNIST/src/", line 60, in train
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/", line 245, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/autograd/", line 145, in backward
RuntimeError: Found dtype Long but expected Float


def train(NB_EPOCS, model, loaders):
    # Train the model
    total_step = len(loaders['train'])
    for epoch in range(NB_EPOCS):
        for i, (images, labels) in enumerate(loaders['train']):
            # gives batch data, normalize x when iterate train_loader
            b_x = Variable(images)   # batch x
            b_y = Variable(labels)   # batch y
            print(np.shape(b_x), b_x.dtype, np.shape(b_y), b_y.dtype)
            ohe_target = torch.nn.functional.one_hot(b_y, num_classes=10)
            output = model(b_x)[0]   
            loss = criterion(output, ohe_target)
torch.Size([100, 1, 28, 28]) torch.float32 torch.Size([100]) torch.int64

If I change the dtype of the b_y to Float by the following code


Then I am getting another error,

RuntimeError: one_hot is only applicable to index tensor.

What I have to do to solve the errors?

Double post from here with solution.

1 Like