How to Solve RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu

In this case, the data and models are generally one in the CPU, one in the GPU, or the data used for calculation. Some have been put in the GPU, and some still exist in the CPU. Here is an idea.

First, find the error reporting line, see which variables or data are used in the calculation, and then use. Is_ CUDA this attribute to check which are on the GPU and which are on the CPU, and then put them on the CPU or GPU. for instance:

The error message indicates that there is a problem in line 46 of utils.py. Then enter the file and locate line 46

Print idx_ Range and reverse_ Mapping to check whether it is on the GPU or the CPU at the same time,

print(idx_ range.is_cuda,reverse_mapping.is_cuda)

After verification, IDX was found_ Range on CPU, reverse_ Mapping on GPU, idx_ Range on GPU (IDX)_ Range. To (device)), problem-solving

Read More: