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/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/Opps_0/Desktop/MNIST/src/train.py", line 69, in <module>
    train(NB_EPOCS, model, loaders)
  File "/home/Opps_0/Desktop/MNIST/src/train.py", line 60, in train
    loss.backward()    
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/tensor.py", 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/__init__.py", line 145, in backward
    Variable._execution_engine.run_backward(
RuntimeError: Found dtype Long but expected Float

Code

def train(NB_EPOCS, model, loaders):
    model.train()
    # 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

b_y=b_y.float()

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