Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何理解在PyTorch中创建叶张量?_Python_Pytorch_Documentation - Fatal编程技术网

Python 如何理解在PyTorch中创建叶张量?

Python 如何理解在PyTorch中创建叶张量?,python,pytorch,documentation,Python,Pytorch,Documentation,来自PyTorch: 但是为什么e和f叶张量也从CPU张量转换成Cuda张量(一种操作) 是因为Tensore在原位操作需要_grad()之前铸入Cuda吗 因为当张量第一次被创建时,f是通过赋值device=“cuda”而不是通过方法.cuda()?来强制转换的,所以它成为一个叶节点 基本上,神经网络的所有输入和权重都是计算图的叶节点 当对张量执行任何操作时,它不再是叶节点 b = torch.rand(10, requires_grad=True) # create a leaf node

来自PyTorch:

但是为什么
e
f
叶张量也从CPU张量转换成Cuda张量(一种操作)

是因为Tensor
e
在原位操作
需要_grad(
)之前铸入Cuda吗


因为当张量第一次被创建时,
f
是通过赋值
device=“cuda”
而不是通过方法
.cuda()

来强制转换的,所以它成为一个叶节点

基本上,神经网络的所有输入和权重都是计算图的叶节点

当对张量执行任何操作时,它不再是叶节点

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\u grad()
cuda()
或其他操作的方式不同。
它创建了一个新的张量,因为需要梯度(可训练权重)的张量不能依赖于其他任何东西

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()
操作会创建一个不需要梯度的新张量:

b = torch.rand(10, requires_grad=True)
b.is_leaf # True
b = b.detach()
b.is_leaf # True
在上一个示例中,我们创建了一个新的张量,它已经在cuda设备上了。
我们不需要任何手术来投下它

f = torch.rand(10, requires_grad=True, device="cuda") # create a leaf node on cuda
f = torch.rand(10, requires_grad=True, device="cuda") # create a leaf node on cuda