深度学习-如何最优化训练线性模型-举例 loss function

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 input

def forward(x): # linear model
    return x * w

def loss(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 error
for w in np.arange(0.0, 4.1, 0.1):# 0.1, 0.2..4.0
    print('w = ', w)
    l_sum = 0
    for x_val, y_val in zip(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_val
        print('\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.0
	 1.0 2.0 0.0 4.0
	 2.0 4.0 0.0 16.0
	 3.0 6.0 0.0 36.0
MSE =  18.666666666666668
w =  0.1
	 1.0 2.0 0.1 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值