pytorch grad is None after .backward()(.backward() 之后 pytorch grad 为 None)
问题描述
我刚刚在 Python 3.7.2 (macOS) 上安装了 torch-1.0.0,并尝试了 教程,但是下面的代码:
I just installed torch-1.0.0 on Python 3.7.2 (macOS), and trying the tutorial, but the following code:
import torch
x = torch.ones(2, 2, requires_grad=True)
y = x + 2
z = y * y * 3
out = z.mean()
out.backward()
print(out.grad)
打印 None 这不是预期的.
prints None which is not what's expected.
有什么问题吗?
推荐答案
这是预期的结果.
.backward 仅在叶节点中累积梯度.out 不是叶节点,因此 grad 是 None.
.backward accumulate gradient only in the leaf nodes. out is not a leaf node, hence grad is None.
autograd.backward 也做同样的事情
autograd.grad 可用于查找任何张量 w.r.t 到任何张量的梯度.所以如果你做 autograd.grad (out, out) 你会得到 (tensor(1.),) 作为输出,这是预期的.
autograd.grad can be used to find the gradient of any tensor w.r.t to any tensor. So if you do autograd.grad (out, out) you get (tensor(1.),) as output which is as expected.
参考:
- Tensor.backward (https://pytorch.org/docs/stable/autograd.html#torch.Tensor.backward)
- autograd.backward (https://pytorch.org/docs/stable/autograd.html#torch.autograd.backward)
- autograd.grad (https://pytorch.org/docs/stable/autograd.html#torch.autograd.grad)
这篇关于.backward() 之后 pytorch grad 为 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.backward() 之后 pytorch grad 为 None
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
