THE following code error : Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu

68 Views Asked by At

The data and model are both on GPU, How can i change code to aviod this error?

Here is the code!!!

import numpy as np
import torch.nn as nn
import torch

x_values = [i for i in range(11)]
x_train = np.array(x_values,dtype=np.float32)
x_train = x_train.reshape(-1,1)


y_values = [2*i+1 for i in x_values]
y_train = np.array(y_values,dtype=np.float32)
y_train = y_train.reshape(-1,1)


class MyLinearRegressionModel(nn.Module):
    def __init__(self,input_dim,output_dim):
        super().__init__()
        self.linear = nn.Linear(input_dim,output_dim)

    def forward(self,x):
        out = self.linear(x)
        return out
input_dim = 1
output_dim = 1

model = MyLinearRegressionModel(input_dim,output_dim)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
**model.to(device)**
print(device)

criterion = nn.MSELoss()   # 指定损失函数
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(),lr = learning_rate)
epochs = 1000

for epoch in range(epochs):
    epoch +=1
  **  inputs = torch.from_numpy(x_train).to(device)
    labels = torch.from_numpy(y_train).to(device)**
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs,labels)
    loss.backward()
    optimizer.step()
    if epoch % 50 == 0:
        print("epoch %d ,loss %f" % (epoch ,loss))


predict = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()
print(predict)

When i ran these code above ,it gave this error, Please help change code to solve this problem.

"RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!"

1

There are 1 best solutions below

0
Sekiz On

It seems you missed move inputs to device. Also, after the process done you have to move your data to cpu again to predict part, since numpy only supports CPU

import numpy as np
import torch.nn as nn
import torch

x_values = [i for i in range(11)]
x_train = np.array(x_values,dtype=np.float32)
x_train = x_train.reshape(-1,1)


y_values = [2*i+1 for i in x_values]
y_train = np.array(y_values,dtype=np.float32)
y_train = y_train.reshape(-1,1)


class MyLinearRegressionModel(nn.Module):
    def __init__(self,input_dim,output_dim):
        super().__init__()
        self.linear = nn.Linear(input_dim,output_dim)

    def forward(self,x):
        out = self.linear(x)
        return out
input_dim = 1
output_dim = 1

model = MyLinearRegressionModel(input_dim,output_dim)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
print(device)

criterion = nn.MSELoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(),lr = learning_rate)
epochs = 1000

for epoch in range(epochs):
    epoch += 1
    inputs = torch.from_numpy(x_train).to(device)
    labels = torch.from_numpy(y_train).to(device)
    optimizer.zero_grad()
    
    ## Addition 1: Moved inputs to device here
    inputs = inputs.to(device)
    
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    
    if epoch % 50 == 0:
        print("epoch %d, loss %f" % (epoch, loss))

## First converts numpy array to torch tensor and moved to device, after your process data moved again on cpu (since numpy only supports cpu) and convert to np array again
predict = model(torch.from_numpy(x_train).to(device).requires_grad_()).data.cpu().numpy()
print(predict)