Pytorch getting RuntimeError: Found dtype Double but expected Float

We Are Going To Discuss About Pytorch getting RuntimeError: Found dtype Double but expected Float. So lets Start this Python Article.

Pytorch getting RuntimeError: Found dtype Double but expected Float

  1. How to solve Pytorch getting RuntimeError: Found dtype Double but expected Float

    You need the data type of the data to match the data type of the model.
    Either convert the model to double (recommended for simple nets with no serious performance problems such as yours)
    # nn architecture class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(4, 4) self.fc2 = nn.Linear(4, 2) self.fc3 = nn.Linear(2, 1) self.double()
    or convert the data to float.
    class mydataset(Dataset): def __init__(self): super().__init__() def __getitem__(self, index): x = Getx(index) y = Gety(index) return x.float(), y.float()

  2. Pytorch getting RuntimeError: Found dtype Double but expected Float

    You need the data type of the data to match the data type of the model.
    Either convert the model to double (recommended for simple nets with no serious performance problems such as yours)
    # nn architecture class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(4, 4) self.fc2 = nn.Linear(4, 2) self.fc3 = nn.Linear(2, 1) self.double()
    or convert the data to float.
    class mydataset(Dataset): def __init__(self): super().__init__() def __getitem__(self, index): x = Getx(index) y = Gety(index) return x.float(), y.float()

Solution 1

You need the data type of the data to match the data type of the model.

Either convert the model to double (recommended for simple nets with no serious performance problems such as yours)

# nn architecture
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(4, 4)
        self.fc2 = nn.Linear(4, 2)
        self.fc3 = nn.Linear(2, 1)
        self.double()

or convert the data to float.

class mydataset(Dataset):

    def __init__(self):
        super().__init__()

    def __getitem__(self, index):
        x = Getx(index)
        y = Gety(index)
        
        return x.float(), y.float()

Original Author Gulzar Of This Content

Solution 2

Check data type of “out” and “y”

print(out.dtype)
print(y.dtype)

you may find a difference like

"torch.float32"
"torch.float64"

Set them in the same type.

Original Author Abu Bakar Siddik Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment