Laurids
Laurids

Reputation: 23

Pytorch Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)

I am a beginner to machine learning and trying to train a model on counting the amount of numbers below 0.5 in a 1D Vector with the length of 10. The input vectors contain number between 0 and 1. I generate the input data and the labels in my script instead of having them in a seperate file, because the data is so simple. This is the Code:

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class MyNet(nn.Module):
    def __init__(self):
        super(MyNet, self).__init__()
        self.lin1 = nn.Linear(10,10)
        self.lin2 = nn.Linear(10,1)
        
    def forward(self,x):
        x = self.lin1(x)
        x = F.relu(x)
        x = self.lin2(x)
        return x

net = MyNet()
net.to(device)

def train():
    criterion = nn.MSELoss()
    optimizer = optim.SGD(net.parameters(), lr=0.1)
    for epochs in range(100):
        target = 0
        data = torch.rand(10)
        for entry in data:
            if entry < 0.5:
                target += 1
        # print(target)
        # print(data)
        data = data.to(device)

        out = net(data)
        # print(out)
    
        target = torch.Tensor(target)
        target = target.to(device)

        loss = criterion(out, target)
        print(loss)

        net.zero_grad()
        loss.backward()

        optimizer.step()

def test():
    acc_error = 0
    for i in range(100):
        test_data = torch.rand(10)
        test_data.to(device)
        test_target = 0
        for entry in test_data:
            if entry < 0.5:
                test_target += 1
        out = net(test_data)
        error = test_target - out
        if error < 0:
            error *= -1
        acc_error += error 
    
    overall_error = acc_error / 100

    print(overall_error)

train()
test() 

This is the error:

Traceback (most recent call last):
  File "test1.py", line 70, in <module>
    test()
  File "test1.py", line 59, in test
    out = net(test_data)
  File "/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "test1.py", line 15, in forward
    x = self.lin1(x)
  File "/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/linear.py", line 94, in forward
    return F.linear(input, self.weight, self.bias)
  File "/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/functional.py", line 1753, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)

The other posts regarding the topic have not solved my problem. Maybe somebody can help. Thanks!

Upvotes: 0

Views: 1155

Answers (1)

Ivan
Ivan

Reputation: 40668

Notice how your error message traces back to test, while train works fine.

You've transfered your data correctly in train:

data = data.to(device)

But not in test:

test_data.to(device)

Instead it should be reassigned to test_data, since torch.Tensor.to makes a copy:

test_data = test_data.to(device)

Upvotes: 1

Related Questions