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[0][0].item())