How to Fix RuntimeError: expected scalar type Float but found Double

The RuntimeError: expected scalar type Float but found Double error occurs when you try to perform matrix multiplication using two matrices with different data types.

To fix the RuntimeError: expected scalar type Float but found Double error, you must ensure both matrices have the same data type before performing the matrix multiplication.

Reproduce the error

import torch

mat1 = torch.randn(3, 4, dtype=torch.float)
mat2 = torch.randn(4, 3, dtype=torch.double)

result = torch.matmul(mat1, mat2)
print(result)

Output

RuntimeError: expected scalar type Float but found Double

How to fix it?

Solution 1: Convert the first matrix to double

You can convert a matrix to a double data type using the to(torch.double) function.

import torch

mat1 = torch.randn(3, 4, dtype=torch.float)
mat2 = torch.randn(4, 3, dtype=torch.double)

mat1 = mat1.to(torch.double)

result = torch.matmul(mat1, mat2)
print(result)

Output

tensor([[ 1.3840, -0.8513, 2.0411],
        [-1.9259, 0.2761, -1.1765],
        [-2.3187, -0.5894, -1.9486]], dtype=torch.float64)

Solution 2: Convert the second matrix to float

You can convert a matrix to a float data type using the to(torch.float) function.

import torch

mat1 = torch.randn(3, 4, dtype=torch.float)
mat2 = torch.randn(4, 3, dtype=torch.double)

mat2 = mat2.to(torch.float)
result = torch.matmul(mat1, mat2)
print(result)

Output

tensor([[ 0.0354, -0.8440, -5.1320],
        [ 1.1818, -2.3900, 1.5615],
        [ 3.0176, 0.7370, 1.3653]])

Choose one of these solutions based on your precision and memory usage requirements.

Remember that double-precision floating-point tensors (torch.DoubleTensor) will provide higher precision but consume more memory than single-precision floating-point tensors (torch.FloatTensor).

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.