'RuntimeError: Found dtype Double but expected Float on call to backward

I have the following PyTorch deep learning code in the middle of a training loop:

        loss.backward()

loss has the following value:

tensor(434.3251, device='cuda:0', dtype=torch.float64, grad_fn=<MeanBackward0>)

I am getting this error:

  File "[,,,]/torch/autograd/__init__.py", line 154, in backward
    Variable._execution_engine.run_backward(
RuntimeError: Found dtype Double but expected Float

What am I doing wrong here and how can I fix it? I can provide more context if needed.



Solution 1:[1]

Check your data type of model's weights, input tensor, and label tensor.

There might be a type mismatch and the automatic conversion of the type.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hayoung