刘二大人pytorch入门-笔记

视频教程刘二大人的pytorch

testpytorch

1、测试pytorch版本

刘二大人pytorch入门-笔记

2、线性模型

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)
    return (y_pred - y) *(y_pred - y)

w_list =[]
msa_list = [] #存储loss值
for w in np.arange(0.0,4.1,0.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)
    msa_list.append(l_sum/3.0)

plt.plot(w_list, msa_list)
plt.ylabel('Loss')
plt.xlabel('w')
plt.show()

刘二大人pytorch入门-笔记

3、梯度下降

刘二大人pytorch入门-笔记
第三讲相关代码

上一篇:刘二Pytorch第二章


下一篇:PyTorch深度学习实践(五)---pytorch实现线性回归