Created
July 15, 2017 12:01
-
-
Save NinZine/0db6b8af0cb020160d8a4264da1b5b8b to your computer and use it in GitHub Desktop.
Linear regression underfitting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import torch.nn as nn | |
from torch.autograd import Variable | |
%matplotlib inline | |
# From here: https://github.com/Dataweekends/zero_to_deep_learning_udemy/blob/master/data/weight-height.csv | |
df = pd.read_csv('weight-height.csv') | |
df.plot(kind='scatter', x='Height', y='Weight', title='Weight and height in adults') | |
x_train = df[['Height']].values.astype(np.float32) | |
y_train = df['Weight'].values.astype(np.float32) | |
# Hyper Parameters | |
input_size = 1 | |
output_size = 1 | |
num_epochs = 60 | |
learning_rate = 0.1 | |
# Linear Regression Model | |
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) | |
return out | |
model = LinearRegression(input_size, output_size) | |
# Loss 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)) | |
targets = Variable(torch.from_numpy(y_train)) | |
# Forward + Backward + Optimize | |
optimizer.zero_grad() | |
outputs = model(inputs) | |
loss = criterion(outputs, targets) | |
loss.backward() | |
optimizer.step() | |
if (epoch+1) % 5 == 0: | |
print ('Epoch [%d/%d], Loss: %.4f' | |
%(epoch+1, num_epochs, loss.data[0])) | |
# Plot the graph | |
predicted = model(Variable(torch.from_numpy(x_train))).data.numpy() | |
plt.plot(x_train, y_train, 'bo', label='Original data') | |
plt.plot(x_train, predicted, label='Fitted line') | |
plt.legend() | |
plt.show() | |
# Not correct compared to Keras example | |
# https://github.com/Dataweekends/zero_to_deep_learning_udemy/blob/master/course/3%20Machine%20Learning.ipynb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, solved it. Number of epochs needs to be in the thousands.