Skip to content

Commit 62af9e3

Browse files
authored
Create gd.py
1 parent d904694 commit 62af9e3

File tree

1 file changed

+58
-0
lines changed
  • Day-23-Gradient-Descent

1 file changed

+58
-0
lines changed

Day-23-Gradient-Descent/gd.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import torch
2+
from torch import nn
3+
from sklearn.datasets import load_boston
4+
5+
class GradientDescent:
6+
def __init__(self, learning_rate=0.01, max_iterations=100):
7+
self.lr = learning_rate
8+
self.max_iterations = max_iterations
9+
10+
def normalization(self, X):
11+
"""
12+
:param X: Input tensor
13+
:return: Normalized input using l2 norm.
14+
"""
15+
l2 = torch.norm(X, p=2, dim=-1)
16+
l2[l2 == 0] = 1
17+
return X / l2.unsqueeze(1)
18+
19+
def compute_error(self, b, m, X, y):
20+
total_error = 0
21+
for i in range(0, X.shape[0]):
22+
total_error += (y - (torch.mm(m , X.T)) + b) ** 2
23+
return total_error / float(X.shape[0])
24+
25+
def step(self, b_curr, m_curr, X, y, learning_rate):
26+
b_gradient = 0
27+
m_gradient = 0
28+
N = float(X.shape[0])
29+
for i in range(X.shape[0]):
30+
b_gradient += -(2/N) * torch.sum(y - (torch.mm(X, m_curr.T) + b_curr), dim=0)
31+
m_gradient += -(2/N) * torch.sum(torch.mm(X.T, (y - (torch.mm(X, m_curr.T) + b_curr))), dim=0)
32+
33+
new_b = b_curr - (learning_rate * b_gradient)
34+
new_m = m_curr - (learning_rate * m_gradient)
35+
return [new_b, new_m]
36+
37+
def gradient_descent(self, X, y, start_b, start_m):
38+
b = start_b
39+
m = start_m
40+
for i in range(self.max_iterations):
41+
b, m = self.step(b_curr=b, m_curr=m, X=X, y=y, learning_rate=self.lr)
42+
43+
return b, m
44+
45+
if __name__ == '__main__':
46+
data = load_boston()
47+
X = torch.tensor(data.data)
48+
y = torch.tensor(data.target).unsqueeze(1)
49+
initial_b = 0.0
50+
initial_m = torch.zeros((X.shape[1], 1), dtype=torch.double).T
51+
nn.init.normal(initial_m)
52+
gd = GradientDescent(learning_rate=0.0001,max_iterations=100)
53+
gd.compute_error(X=gd.normalization(X), y=y, b=initial_b, m=initial_m)
54+
bias, slope = gd.gradient_descent(gd.normalization(X), y, start_b=initial_b, start_m=initial_m)
55+
X = gd.normalization(X)
56+
print('y: ', y[0].item())
57+
print('y_pred: ', (torch.mm(slope, X[0].unsqueeze(0).T)+bias).item())
58+

0 commit comments

Comments
 (0)