Fix RunTimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same – PyTorch Tutorial

By | May 18, 2022

When we are training pytorch model using GPU, we may get this error: RunTimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same. In this tutorial, we will introduce you how to fix it.

How to fix this RunTimeError?

From this error, we can find:

Input = torch.FloatTensor, which means Input is on cpu

weight = torch.cuda.FloatTensor, which mean weight is on gpu

In order to fix this error, we should make Input be in gpu.

We can do this as follows:

import torch

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 
input = input.to(device)

Then, you will find this error is fixed.