我正在使用Pytorch进行linear_regression 我用一个变量成功了。但是使用pytorch的multi_variable linear_regression 得到了一些错误。 我应该如何使用多变量进行线性回归?
TypeError Traceback(最近一次调用 最后)in() 9 optimizer.zero_grad()#gradient 10个输出=模型(输入)#output ---> 11损失=标准(输出,目标)#loss功能 12 loss.backward()#backward propogation 13 optimizer.step()#1步优化(gradeint descent)
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/module.py 在调用(自我,*输入,** kwargs) 204 205 def 调用(self,* input,** kwargs): - > 206 result = self.forward(* input,** kwargs) 207 for hook in self._forward_hooks.values(): 208 hook_result = hook(自我,输入,结果)
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/loss.py 在前进(自我,输入,目标) 22 _assert_no_grad(目标) 23 backend_fn = getattr(self._backend,type(self)。 name ) ---> 24 return backend_fn(self.size_average)(输入,目标) 25 26
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/_functions/thnn/auto.py 在前进(自我,输入,目标) 39输出= input.new(1) 40 getattr(self._backend,update_output.name)(self._backend.library_state,input,target, ---> 41输出,* self.additional_args) 42返回输出 43
TypeError:FloatMSECriterion_updateOutput收到无效 参数组合 - got(int,torch.FloatTensor, torch.DoubleTensor,torch.FloatTensor,bool),但是预期的(int state, torch.FloatTensor输入,torch.FloatTensor目标,torch.FloatTensor 输出,bool sizeAverage)
这是代码
#import
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
#input_size = 1
input_size = 3
output_size = 1
num_epochs = 300
learning_rate = 0.002
#Data set
#x_train = np.array([[1.564],[2.11],[3.3],[5.4]], dtype=np.float32)
x_train = np.array([[73.,80.,75.],[93.,88.,93.],[89.,91.,90.],[96.,98.,100.],[73.,63.,70.]],dtype=np.float32)
#y_train = np.array([[8.0],[19.0],[25.0],[34.45]], dtype= np.float32)
y_train = np.array([[152.],[185.],[180.],[196.],[142.]])
print('x_train:\n',x_train)
print('y_train:\n',y_train)
class LinearRegression(nn.Module):
def __init__(self,input_size,output_size):
super(LinearRegression,self).__init__()
self.linear = nn.Linear(input_size,output_size)
def forward(self,x):
out = self.linear(x) #Forward propogation
return out
model = LinearRegression(input_size,output_size)
#Lost and Optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
#train the Model
for epoch in range(num_epochs):
#convert numpy array to torch Variable
inputs = Variable(torch.from_numpy(x_train)) #convert numpy array to torch tensor
#inputs = Variable(torch.Tensor(x_train))
targets = Variable(torch.from_numpy(y_train)) #convert numpy array to torch tensor
#forward+ backward + optimize
optimizer.zero_grad() #gradient
outputs = model(inputs) #output
loss = criterion(outputs,targets) #loss function
loss.backward() #backward propogation
optimizer.step() #1-step optimization(gradeint descent)
if(epoch+1) %5 ==0:
print('epoch [%d/%d], Loss: %.4f' % (epoch +1, num_epochs, loss.data[0]))
predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
plt.plot(x_train,y_train,'ro',label='Original Data')
plt.plot(x_train,predicted,label='Fitted Line')
plt.legend()
plt.show()
答案 0 :(得分:4)
您需要确保数据具有相同的类型。在这种情况下,x_train是32位浮点数,而y_train是Double。你必须使用:
y_train = np.array([[152.],[185.],[180.],[196.],[142.]],dtype=np.float32)