-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_regression_scratch.py
More file actions
56 lines (37 loc) · 1.33 KB
/
Copy pathlinear_regression_scratch.py
File metadata and controls
56 lines (37 loc) · 1.33 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
# Implementing Linear Regression using manual functions
# Function we will use to build and test our model
# F(x) = 2 * x
# F'(x) = w * x. We need to find the value of w(weight)
X = np.array([1,2,3,4], dtype=np.float32) # Training examples
Y = 2*X # Output Function that is to be simulated
# Initialising our weight variable w
w = 0.0
# Model Prediction
# Simulating the forward pass in a Neural Network based model
def forward(x):
return w*x
# Loss function = MSE (Mean Squared Error)
def loss(y, y_predicted):
return ((y_predicted-y)**2).mean()
# Gradient function
# Formula of MSE = 1/N * (w*x - y)**2
# Gradient dj/dw = 1/N 2x (w*x - y)
def gradient(x,y, y_predicted):
return np.dot(2*x, y_predicted-y).mean()
print(f'Prediction before training. Eg input = f(5). It gives {forward(5):.3f}')
# Training
learning_rate = 0.01
epochs = 20
for epoch in range(epochs):
# prediction = forward pass
y_pred = forward(X)
# loss
l = loss(Y, y_pred)
# gradient = backward pass
dw = gradient(X,Y,y_pred)
# update weights
w-= learning_rate * dw
if epoch%2 == 0:
print(f'Epoch {epoch+1}: \n w = {w:.3f}, loss = {l:.8f}')
print(f'Prediction after training. Eg input = f(5). It gives {forward(5):.3f}')