Linear Regr GD
Linear Regr GD
Note that linear regression and Adaline are very similar. The only difference is that we apply a threshold function for converting the
outputs from continuous targets for predictions. The derivative and training procedure are identical to Adaline though. You can compare
the two notebooks (this one and adaline-sgd.ipynb ) side by side as shown below to see the relationship:
import pandas as pd
import matplotlib.pyplot as plt
import torch
%matplotlib inline
x1 x2 y
torch.manual_seed(123)
shuffle_idx = torch.randperm(y.size(0), dtype=torch.long)
X, y = X[shuffle_idx], y[shuffle_idx]
percent70 = int(shuffle_idx.size(0)*0.7)
grad_loss_yhat = 2*(yhat - y)
grad_yhat_weights = x
grad_yhat_bias = 1.
# Chain rule: inner times outer
grad_loss_weights = torch.mm(grad_yhat_weights.t(),
grad_loss_yhat.view(-1, 1)) / y.size(0)
return cost
Plot MSE
plt.plot(range(len(cost)), cost)
plt.ylabel('Mean Squared Error')
plt.xlabel('Epoch')
plt.show()
train_pred = model.forward(X_train)
test_pred = model.forward(X_test)
print('Weights', model.weights)
print('Bias', model.bias)
w, b = analytical_solution(X_train, y_train)
print('Analytical weights', w)
print('Analytical bias', b)