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(&quot;cuda:0&quot; if torch.cuda.is_available() else &quot;cpu&quot;)

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 &lt; 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 &lt; 0.5:
                test_target += 1
        out = net(test_data)
        error = test_target - out
        if error &lt; 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 &quot;test1.py&quot;, line 70, in &lt;module&gt;
    test()
  File &quot;test1.py&quot;, line 59, in test
    out = net(test_data)
  File &quot;/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;test1.py&quot;, line 15, in forward
    x = self.lin1(x)
  File &quot;/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/modules/linear.py&quot;, line 94, in forward
    return F.linear(input, self.weight, self.bias)
  File &quot;/vol/fob-vol7/mi18/radtklau/SP/sem_project/lib64/python3.6/site-packages/torch/nn/functional.py&quot;, 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!

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)

