超简单的pytorch反向传播举例

这篇博客通过一个简单的两层模型y=ax+b,展示了如何使用PyTorch进行反向传播。博主设定好输入x、目标y及初始参数a和b,采用MSE损失函数,仅运行一轮反向传播过程,详细解释了参数如何更新。读者可以设置断点,结合代码和图表深入理解反向传播机制。
摘要由CSDN通过智能技术生成

本例设置了两层y=ax+b,前置条件包括x和目标y的值,初始的两组a和b的值,损失函数为mse loss,可以只跑一轮,看看反向传播各层参数是如何更新。建议在backward函数里打断点,然后将程序与图请对照着看。

在这里插入图片描述

# -*- coding:utf-8 -*-
# reference: https://pytorch.org/docs/stable/notes/extending.html
import torch
from torch import nn
from torch.autograd import Function, Variable
import numpy as np
from collections import OrderedDict

class LinearFunction2(Function):

    # Note that both forward and backward are @staticmethods
    @staticmethod
    # bias is an optional argument
    def forward(ctx, input, weight, bias=None):
        ctx.save_for_backward(input, weight, bias)
        output = input.mm(weight.t())
        if bias is not None:
            output += bias.unsqueeze(0).expand_as(output)
        return output

    # This function has only a single output, so it gets only one gradient
    @staticmethod
    def backward(ctx, grad_output):
        # This is a pattern that is very convenient - at the top of backward
        # unpack saved_tensors and initialize all gradients w.r.t. inputs to
        # None. Thanks to the fact that additional trailing Nones are
        # ignored, the return statement is simple even when the function has
        # optional inputs.

        # 第一个grad_output,是loss对y-y'的梯度。如loss_mse的梯度为2/n*(y-y'),
        # 其中n为一个batch数据的数目,该loss是nx1的矩阵。
        
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值