 # My Linear Regression Model predicting falsely

Please everyone, i am very new to Machine learning and i just tried out linear regression and i’m not getting accurate result from my model.

Aim:
Predict the salary of an employee based on his/her number of experience.

Excel sheet content:

Years of Experience | Salaries
10 | 300000
2 | 20000
8 | 599000
9 | 290000
1 | 20000
7 | 80000
4 | 700000

``````# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import pandas as pd

datasets = pd.read_csv("./salaries.csv")

features_X = datasets.iloc[:, :-1].values
features_Y = datasets.iloc[:, 1:].values

features_X = torch.from_numpy(features_X).float()
features_Y = torch.from_numpy(features_Y).float()

print(datasets, "\n\n", features_X, "\n\n", features_Y, "\n\n", features_X.type(), "\n\n", features_Y.type())

x_train = torch.FloatTensor(features_X)
y_train = torch.FloatTensor(features_Y)

""" Define out Hyper-parameters """
learning_rate = 0.0001
epochs = 500

""" Building the Network """

class Model(nn.Module):
def __init__(self, input_layer, output_layer):
super(Model, self).__init__()
self.linear = nn.Linear(input_layer, output_layer)

def forward(self, x):
y_pred = self.linear(x)
return y_pred

model = Model(input_layer=1, output_layer=1)
cost_func = nn.MSELoss(size_average=False)
optim = torch.optim.SGD(model.parameters(), lr=learning_rate)

for epoch in range(epochs):
y_pred = model(x_train)
loss = cost_func(y_pred, y_train)
print("traning: ", loss.data)
optim.zero_grad()
loss.backward()
optim.step()

test_data = torch.FloatTensor([[2.0]])
print("predicted output: ", model(test_data).data.item())
``````

Your model only has one node. It is to simple to compute anything. But either way, the code looks clean! What is the output and why do you have so little data?

Thank you Oli for your swift response.
I am very new to pytorch and i’m just using this as an example to test neural network in pytorch.
However, when i eject 2.0 as the years of experience, hoping to get something similar to 20,000 as in the dataset, but i’m getting way above it and when i tweak the hyper-parameters i get different values.
Please any idea? what am i doing wrong.