How to understand creating leaf tensors in PyTorch?(如何理解在 PyTorch 中创建叶张量?)
问题描述
来自 PyTorch 文档:
From PyTorch documentation:
b = torch.rand(10, requires_grad=True).cuda()
b.is_leaf
False
# b was created by the operation that cast a cpu Tensor into a cuda Tensor
e = torch.rand(10).cuda().requires_grad_()
e.is_leaf
True
# e requires gradients and has no operations creating it
f = torch.rand(10, requires_grad=True, device="cuda")
f.is_leaf
True
# f requires grad, has no operation creating it
但是为什么 e 和 f 叶子张量,当它们都是从一个 CPU 张量转换时,变成一个 Cuda 张量(一个操作)?
But why are e and f leaf Tensors, when they both were also cast from a CPU Tensor, into a Cuda Tensor (an operation)?
是不是因为在就地操作 requires_grad_() 之前将 Tensor e 投射到 Cuda 中?
Is it because Tensor e was cast into Cuda before the in-place operation requires_grad_()?
并且因为 f 是通过赋值 device="cuda" 而不是通过方法 .cuda() 进行转换的?
And because f was cast by assignment device="cuda" rather than by method .cuda()?
推荐答案
当张量第一次被创建时,它变成了一个叶子节点.
When a tensor is first created, it becomes a leaf node.
基本上,神经网络的所有输入和权重都是计算图的叶节点.
Basically, all inputs and weights of a neural network are leaf nodes of the computational graph.
当对张量执行any操作时,它不再是叶节点.
When any operation is performed on a tensor, it is not a leaf node anymore.
b = torch.rand(10, requires_grad=True) # create a leaf node
b.is_leaf # True
b = b.cuda() # perform a casting operation
b.is_leaf # False
requires_grad_() 与 cuda() 或其他操作方式不同.
它创建了一个新的张量,因为需要梯度(可训练权重)的张量不能依赖于其他任何东西.
requires_grad_() is not an operation in the same way as cuda() or others are.
It creates a new tensor, because tensor which requires gradient (trainable weight) cannot depend on anything else.
e = torch.rand(10) # create a leaf node
e.is_leaf # True
e = e.cuda() # perform a casting operation
e.is_leaf # False
e = e.requires_grad_() # this creates a NEW tensor
e.is_leaf # True
此外,detach() 操作会创建一个不需要梯度的新张量:
Also, detach() operation creates a new tensor which does not require gradient:
b = torch.rand(10, requires_grad=True)
b.is_leaf # True
b = b.detach()
b.is_leaf # True
在最后一个例子中,我们创建了一个已经在 cuda 设备上的新张量.
我们不需要任何操作来转换它.
In the last example we create a new tensor which is already on a cuda device.
We do not need any operation to cast it.
f = torch.rand(10, requires_grad=True, device="cuda") # create a leaf node on cuda
这篇关于如何理解在 PyTorch 中创建叶张量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何理解在 PyTorch 中创建叶张量?
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
