import numpy as np
import matplotlib .pyplot as plt
x_data =[1.0,2.0,3.0]# training list input
y_data =[2.0,4.0,6.0]# training list inputdefforward(x):# linear modelreturn x * w
defloss(x, y):# loss function
y_pred = forward(x)return(y_pred - y)*(y_pred - y)
w_list =[]# weight list, w = 2 is minimal
mse_list =[]# mean square errorfor w in np.arange(0.0,4.1,0.1):# 0.1, 0.2..4.0print('w = ', w)
l_sum =0for x_val, y_val inzip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(x_val, y_val)
l_sum += loss_val # sum of all loss_valprint('\t', x_val, y_val, y_pred_val, loss_val)print('MSE = ', l_sum /3)# mean
w_list.append(w)
mse_list.append(l_sum /3)# mean
plt.plot(w_list, mse_list)# plot the graph
plt.ylabel('Loss')# y axis
plt.xlabel('w')#x axis
plt.show()
w =0.01.02.00.04.02.04.00.016.03.06.00.036.0
MSE =18.666666666666668
w =0.11.02.00.1