-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinreg.py
37 lines (31 loc) · 1.25 KB
/
linreg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
class LinearModel:
def __init__(self, num_features):
self.num_features = num_features
self.W = np.random.randn(num_features, 1)
self.b = np.random.randn()
def forward_pass(self, X):
y = self.b + np.dot(X, self.W)
return y
def compute_loss(self, y, y_true):
loss = np.sum(np.square(y - y_true))
return loss/(2*y.shape[0])
def backward_pass(self, X, y_true, y_hat):
m = y_hat.shape[0]
db = np.sum(y_hat - y_true)/m
dW = np.sum(np.dot(np.transpose(y_hat - y_true), X), axis=0)/m
return dW, db
def update_params(self, dW, db, lr):
self.W = self.W - lr * np.reshape(dW, (self.num_features, 1))
self.b = self.b - lr * db
def train(self, x_train, y_train, iterations, lr):
losses = []
for i in range(iterations):
y_hat = self.forward_pass(x_train)
dW, db = self.backward_pass(x_train, y_train, y_hat)
self.update_params(dW, db, lr)
loss = self.compute_loss(y_hat, y_train)
losses.append(loss)
if i % int(iterations/10) == 0:
print('Iter: {}, Current loss: {:.4f}'.format(i, loss))
return losses