PyTorch 深度学习实践(刘二大人)---第二讲线性模型

步骤:数据集-模型选择-训练-应用

Maching Learning:Input-DataSet ( training、testing)-prediction

有监督学习(Supervised Learning):过拟合、泛化能力(训练集=训练+验证)

Linear Model:

y' = wx+b
Training Loss:loss = (y' - y)^2 = (wx+b-y)^2
Mean Square Error: cost=\frac{1}{N}\sum (y'_n-y_n)^2

用visdom可视化

课堂代码 

import numpy as np
import matplotlib.pyplot as plt

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]


def forward(x):  # 前馈
    return x * w


def loss(x, y):  # 损失函数
    y_pred = forward(x)  # y_hat
    return (y_pred - y) * (y_pred - y)


w_list = []
mse_list = []
for w in np.arange(0.0, 4.1, 0.1):  # 间隔为0.1,0.0~4.1
    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  # 求和
        print('\t', x_val, y_val, y_pred_val, loss_val)
    print('MSE = ', l_sum / 3)  # 均值
    w_list.append(w)
    mse_list.append(l_sum / 3)

plt.plot(w_list, mse_list)
plt.ylabel('Loss')  # 纵坐标
plt.xlabel('W')  # 横坐标
plt.show()

作业代码

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 设y=2x+1
x_data = [1.0, 2.0, 3.0]
y_data = [3.0, 5.0, 7.0]


def forward(x):  # 前馈
    return x * w + b


def loss(x, y):  # 损失函数
    y_pred = forward(x)  # y_hat
    return (y_pred - y) * (y_pred - y)


w_list = []
b_list = []
mse_list = []
for w in np.arange(0.0, 4.0, 0.1):  # 间隔为0.1,0.0~4.0
    for b in np.arange(0.0, 4.0, 0.1):
        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  # 求和
        b_list.append(b)
        mse_list.append(l_sum / 3)
    w_list.append(w)

mse_r = np.array(mse_list)
b_r = np.array(b_list)
mse_j = np.transpose(mse_r.reshape(40, 40))
b_j = b_r.reshape(40, 40)

fig = plt.figure()
ax = fig.add_axes(Axes3D(fig, auto_add_to_figure=False))
[w, b] = np.meshgrid(w_list, b_j[1])
ax.plot_surface(w, b, mse_j, cmap='rainbow')
ax.set_xlabel('W', color='b')
ax.set_ylabel('B', color='g')
ax.set_zlabel('MSE', color='r')

plt.draw()
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值