深入浅出Pytorch函数——torch.Tensor.backward

这篇具有很好参考价值的文章主要介绍了深入浅出Pytorch函数——torch.Tensor.backward。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

分类目录:《深入浅出Pytorch函数》总目录
相关文章:
· 深入浅出Pytorch函数——torch.Tensor


计算当前张量相对于图的梯度,该函数使用链式法则对图进行微分。如果张量不是一个标量(即其数据具有多个元素)并且需要梯度,则函数还需要指定梯度,指定的梯度应该是一个与当前张量类型和形状匹配的张量,包含微分函数的梯度。此函数在累积了图中各叶子结点的梯度,在调用它之前,我们可能需要将grad属性置零或将其设置为None

语法
Tensor.backward(gradient=None, retain_graph=None, create_graph=False, inputs=None)
参数
  • gradient:[Tensor/None] 相对张量的梯度。如果它是张量,它将自动转换为不需要grad的张量,除非create_graphTrue。对于标量或不需要梯度的张量应指定为None
  • retain_graph:[可选, bool] 如果为False,则用于计算梯度的图将被释放。请注意,在几乎所有情况下,都不需要将此选项设置为True,而且通常可以以更有效的方式解决。默认为create_graph的值。
  • create_graph:[可选, bool] 如果为True,将构建导数的图,从而可以计算更高阶的导数乘积,默认值为False
  • inputs:[List[Tensor]] 输入张量的梯度将累积为.grad。所有其他张量都将被忽略。如果没有提供,梯度将累积到用于计算的所有叶张量中。
参数详解
retain_graph

Pytorch是动态图计算机制,在每一次反向传播计算梯度的循环内,Pytorch先建立正向计算图,然后使用反向传播计算梯度,同时被销毁计算图,下一次循环时再重新建立正向计算图。他的官方定义为:

retain_graph (bool, optional):If False, the graph used to compute the grad will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value of create_graph.

例如下面的代码:

import torch import torch.nn as nn

class Function(torch.nn.Module):
    def __init__(self):
        super(Function, self).__init__()
        self.Linear = torch.nn.Linear(1,1)
    def forward(self, input):
        output = self.Linear(input)
        return output

func = Function()
x= torch.tensor([1.0]) x.requires_grad=True y = func(x) ** 2
y.backward(retain_graph=True)
y.backward()

从第一行至倒数第三行,Pytorch依据代码建立了正向计算图,若倒数第二行改为y.backward(),则在第一次反向传播后,正向计算图会被销毁,但是我们使用了retain_graph=True保持正向计算图不被销毁,故倒数第一行y.backward()可以进行第二次反向传播。否则,在默认情况retain_graph=Flase下会报错:文章来源地址https://www.toymoban.com/news/detail-611497.html

Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

到了这里,关于深入浅出Pytorch函数——torch.Tensor.backward的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入浅出Pytorch函数——torch.ones

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.Tensor · 深入浅出Pytorch函数——torch.ones · 深入浅出Pytorch函数——torch.zeros · 深入浅出Pytorch函数——torch.full · 深入浅出Pytorch函数——torch.ones_like · 深入浅出Pytorch函数——torch.zeros_like · 深

    2023年04月26日
    浏览(90)
  • 深入浅出Pytorch函数——torch.maximum

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.max · 深入浅出Pytorch函数——torch.maximum 计算 input 和 other 的元素最大值。 语法 参数 input :[ Tensor ] 输入张量 other :[ Tensor ] 输入的第二个张量 实例

    2024年02月15日
    浏览(32)
  • 深入浅出Pytorch函数——torch.full

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.Tensor · 深入浅出Pytorch函数——torch.ones · 深入浅出Pytorch函数——torch.zeros · 深入浅出Pytorch函数——torch.full · 深入浅出Pytorch函数——torch.ones_like · 深入浅出Pytorch函数——torch.zeros_like · 深

    2024年02月07日
    浏览(40)
  • 深入浅出Pytorch函数——torch.arange

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.range · 深入浅出Pytorch函数——torch.arange · 深入浅出PaddlePaddle函数——paddle.arange 语法 当 dtype 表示浮点类型时,为了避免浮点计算误差,建议给 end 加上一个极小值 epsilon ,使边界可以更加明

    2024年02月04日
    浏览(35)
  • 深入浅出Pytorch函数——torch.max

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.max · 深入浅出Pytorch函数——torch.maximum torch.max 有三种输入形式,根据其输入形式及参数的不同有下列三种返回形式: torch.max(input) :返回输入张量所有元素的最大值。 torch.max(input, dim, keep

    2024年02月15日
    浏览(32)
  • 深入浅出Pytorch函数——torch.exp

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.exp · 深入浅出TensorFlow2函数——tf.math.exp · 深入浅出Pytorch函数——torch.exp · 深入浅出PaddlePaddle函数——paddle.exp 对输入 input 逐元素进行以自然数 e e e 为底指数运算。 语法 参数 input :[ Te

    2024年02月11日
    浏览(32)
  • 深入浅出Pytorch函数——torch.squeeze

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.squeeze · 深入浅出Pytorch函数——torch.unsqueeze 将输入张量形状为1的维度去除并返回。比如输入向量的形状为 A × 1 × B × 1 × C × 1 × D Atimes1times Btimes1times Ctimes1times D A × 1 × B × 1 × C × 1 ×

    2024年02月16日
    浏览(34)
  • 深入浅出Pytorch函数——torch.sum

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.reduce_sum · 深入浅出TensorFlow2函数——tf.math.reduce_sum · 深入浅出Pytorch函数——torch.sum · 深入浅出PaddlePaddle函数——paddle.sum 语法 参数 input :[ Tensor ] 输入的张量。 dim :[可选, int / tuple ] 求和

    2024年02月04日
    浏览(57)
  • 深入浅出PyTorch函数torch.rand与torch.randn

    torch.rand 和 torch.randn 都是PyTorch中用于生成随机张量的函数,但它们生成随机数的方式有所不同。 torch.rand 生成在区间 [0, 1) 内均匀分布的随机数。 size 参数是一个表示所需张量形状的元组或整数。可以生成任何形状的随机张量。 torch.randn 生成从标准正态分布(均值为0,标准

    2024年02月09日
    浏览(29)
  • 深入浅出Pytorch函数——torch.nn.Module

    分类目录:《深入浅出Pytorch函数》总目录 Pytorch中所有网络的基类,我们的模型也应该继承这个类。 Modules 也可以包含其它 Modules ,允许使用树结构嵌入他们,我们还可以将子模块赋值给模型属性。 语法 方法 torch.nn.Module.apply 实例 通过上面方式赋值的 submodule 会被注册,当调

    2024年02月12日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包