import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
study = [1,2,3]
marks = [2,4,6]
def forward(x):
return x *w
def loss(x,y):
y_pred = forward(x)
return (y_pred - y)**2
np.random.seed(80)
w = np.random.rand()
w
0.5219157165002548
forward(4)
2.0876628660010192
It says if we study for 4 hours, we will be getting 2.08 marks. Now, lets try to find it after training
def grad(x,y):
return 2*x *(x*w - y)
epoch_list = []
loss_list = []
epoch = 30
for i in range(epoch):
total_loss = 0
for x,y in zip(study,marks):
y_pred = forward(x)
total_loss += loss(x,y)
dw = grad(x,y)
w = w - 0.01 * dw
epoch_list.append(i)
loss_list.append(total_loss/3)
plt.plot(epoch_list,loss_list)
plt.xlabel("Epoch")
plt.ylabel("Total Loss")
Text(0,0.5,'Total Loss')
forward(4)
7.999313576958697
After training, our model says if we study for 4 hours, we will be getting 7.99 marks.