PyTorch深度学习实践第二集 线性模型 y=wx

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,w):
    y_p=w*x
    return y_p

def loss(x,y,w):
    y_p=forward(x,w)
    loss_=(y-y_p)*(y-y_p)
    return loss_

w_list=[]
mse_list=[]

for w in np.arange(0.0,4.1,0.1):
    l_sum=0
    w_list.append(w)
    for x,y in zip(x_data,y_data):
        loss_=loss(x,y,w)
        l_sum+=loss_
    mse_list.append(l_sum/len(x_data))

plt.plot(w_list,mse_list)
plt.show()


你可能感兴趣的:(Pytorch,深度学习,pytorch,python)