multi-variable linear regression with pytorch

jojo kim Source

I'm working on linear_regression with Pytorch.
I success with one variable. but with multi_variable linear_regression with pytorch.
got some error. how should I work linear regression with multi-variable ?

TypeError Traceback (most recent call last) in () 9 optimizer.zero_grad() #gradient 10 outputs = model(inputs) #output ---> 11 loss = criterion(outputs,targets) #loss function 12 loss.backward() #backward propogation 13 optimizer.step() #1-step optimization(gradeint descent)

/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/ in call(self, *input, **kwargs) 204 205 def call(self, *input, **kwargs): --> 206 result = self.forward(*input, **kwargs) 207 for hook in self._forward_hooks.values(): 208 hook_result = hook(self, input, result)

/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/ in forward(self, input, target) 22 _assert_no_grad(target) 23 backend_fn = getattr(self._backend, type(self).name) ---> 24 return backend_fn(self.size_average)(input, target) 25 26

/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/_functions/thnn/ in forward(self, input, target) 39 output = 40 getattr(self._backend,, input, target, ---> 41 output, *self.additional_args) 42 return output 43

TypeError: FloatMSECriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, torch.DoubleTensor, torch.FloatTensor, bool), but expected (int state, torch.FloatTensor input, torch.FloatTensor target, torch.FloatTensor output, bool sizeAverage)

here is code

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.]])

class LinearRegression(nn.Module):
    def __init__(self,input_size,output_size):
        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,[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')


answered 1 year ago Roger Trullo #1

You need to make sure that the data has the same type. In this case x_train is a 32 bit float while y_train is a Double. You have to use:

y_train = np.array([[152.],[185.],[180.],[196.],[142.]],dtype=np.float32)

comments powered by Disqus