Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

I am trying to train a DNN model using pytorch, and I want to use GPU to train my model. I am able to successfully copy my model to the GPU using model.to(device), where device = cuda:0.

However, the standard methods for copying input to the GPU, (RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same), that is, X.to(device) and X.cuda() does not give me the desired output. Following is the method I am currently implementing:

def train_loop(self, dataloader, device):
    size = len(dataloader.dataset)
    for batch, (X, y) in enumerate(dataloader):
        # Compute prediction and loss
        print(device)
        X.to(device)
        print(X.is_cuda)
        y.to(device)
        
        pred = self.model(X)
        loss = self.loss_fn(pred, y)

On printing the device value print(device) it shows as: cuda:0. But when I run print(X.is_cuda) it returns false. (Screenshot attached below).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error Message

Please let me know where I am going wrong. Thank you!

>Solution :

X.to(device) does nothing.
change it to:

x=x.to(device)

Of course this should be done to any parameter\variable you want on the GPU

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading