Machine learning----PyTorch forward propagation and back propagation

forward propagation

  • Neural network is essentially a complex compound function with many parameters. The data is the input of the function and the result is the output of the function.
  • Forward propagation is to obtain the output of the function through the input of the function and the neural network.
  • It is usually used to verify whether the current parameters are the optimal solution, or the trained model is used for detection.
  1. For example function: y = w ∗ xy = w*xy=wx

  2. By adjusting the parameter www and dataxxPass in x : Get the predicted valuey ′ y'y

  3. Obtain the loss value: loss = ( y − y ′ ) 2 loss = (y - y')^2loss=(yy)2

import torch

def forward(x, y, w):
	# 模型
    y_predicted = w * x
    # 计算出真实值和预测值的损失值
    loss = (y- y_predicted )**2
    return loss

x = torch.tensor(1.0)
y = torch.tensor(2.0)
w = torch.tensor(1.0, requires_grad=True)

forward(x, y, w)

Obtaining the difference between the predicted value and the real value is the whole process of forward propagation.

前面线性回归中求MSE的过程就是正向传播


Backpropagation

  • The purpose of backpropagation is to calculate the gradient relationship between the predicted value and the parameter (calculated through partial derivatives)
  • Find the optimal solution of w parameter by adjusting the value calculated by partial derivative and gradient descent ( 模型训练的过程)
loss = forward(x, y, w)
loss.backward()
print(f'loss关于w的偏导值:{
      
      w.grad}')
# 清空梯度计算 避免下次计算进行累加
w.grad.zero_()

Summarize

  • In general, we usually solve equations y=wxby finding the values ​​of x and y
  • Now that we know the x value and y value, how to find the relationship between x and y
  • How to find the optimal solution for the w parameter so that x and y have a certain relationship
  • Finding w parameters is the process of machine learning
    • 正向传播➡查看MSE是否符合设定的关系➡反向传播求偏导➡梯度下降➡循环训练模型

Guess you like

Origin blog.csdn.net/bjsyc123456/article/details/124847558