当前位置:网站首页>Station B, Master Liu Er - back propagation

Station B, Master Liu Er - back propagation

2022-07-06 05:41:00 Ning Ranye


Series articles :
B Stand up, Mr. Liu er - Linear regression and gradient descent


List of articles


Code

import matplotlib.pyplot as plt
import  torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# w Is a tensor with gradient tracking set 
w = torch.Tensor([1.0])
w.requires_grad = True
print("w=",w)
def forward(x):
    return x*w
#  Loss function of single data 
def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y)*(y_pred-y)
#  Learning rate 
alpha = 0.001
epoch_list = []
w_list = []
loss_list = []
for epoch in range(100):
    l = 0
    loss_sum = 0
    for (x ,y) in zip(x_data, y_data):
        # l, loss_sum It's all tensors , Gradient free tracking 
        l = loss(x,y)
        loss_sum += l.data
        #  My question : Back propagation 、 Gradient update is carried out epoch*len(x_data),
        #  Why not epoch Time .
        l.backward()
        w.data = w.data - alpha*w.grad.data
        w.grad.data.zero_()
    w_list.append(w.data)
    epoch_list.append(epoch)
    #  To get the value on the tensor, you need to convert it to numpy
    loss_list.append(loss_sum.data.numpy()[0])
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss_sum")
plt.show()

 Insert picture description here

Back propagation 、 Gradient update lepoch Code for the next time

import matplotlib.pyplot as plt
import  torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# w Is a tensor with gradient tracking set 
w = torch.Tensor([1.0])
w.requires_grad = True
print("w=",w)
def forward(x):
    return x*w
#  Loss function of single data 
def loss(xs, ys):
    # y_pred = forward(x)
    # return (y_pred - y)*(y_pred-y)
    loss_sum = 0
    for(x, y) in zip(xs, ys):
        y_pred = forward(x)
        loss_sum += (y_pred-y)*(y_pred-y)
    return  loss_sum/len(xs)
#  Learning rate 
alpha = 0.001
epoch_list = []
w_list = []
loss_list = []
#  Conduct epoch Sub gradient update 、 Loss function calculation 
for epoch in range(100):
    #  Calculate the loss function of all data 
    l = loss(x_data, y_data)
    l.backward()
    #  Gradient update 
    w.data = w.data - alpha * w.grad.data
    w.grad.data.zero_()
    w_list.append(w.data)
    epoch_list.append(epoch)
    loss_list.append(l.data.numpy()[0])
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss_sum")
plt.show()

 Insert picture description here
The pictures that come out in two ways are Loss reduction rate 、 The results are different .
At present, I also have questions , I don't know which kind is suitable .
B The station teacher wrote the first code

原网站

版权声明
本文为[Ning Ranye]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060533516913.html