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!"
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