[英]What is the "data.max" of a torch.Tensor?
I have been browsing the documentation of torch.Tensor, but I have not been able to find this (just similar things).我一直在浏览 torch.Tensor 的文档,但一直找不到这个(只是类似的东西)。
If a_tensor
is a torch.Tensor
, what is a_tensor.data.max
?如果
a_tensor
是torch.Tensor
,什么是a_tensor.data.max
? What type, etc.?什么类型等等?
In particular, I am reading a_tensor.data.max(1)[1]
and a_tensor.data.max(1)[1][i].cpu().numpy()
.特别是,我正在阅读
a_tensor.data.max(1)[1]
和a_tensor.data.max(1)[1][i].cpu().numpy()
。
When accessing .data
you are accessing the underlying data of the tensor.访问
.data
时,您正在访问张量的基础数据。 The returned object is a Torch.*Tensor
as well, however, it won't be linked to any computational graph.返回的 object也是一个
Torch.*Tensor
,但是,它不会链接到任何计算图。
Take this example:举个例子:
>>> x = torch.rand(4, requires_grad=True)
>>> y = x**2
>>> y
tensor([0.5272, 0.3162, 0.1374, 0.3004], grad_fn=<PowBackward0>)
While y.data
is somewhat detached from graph (no grad_fn
function), yet it is not a copy of y
as y.detach()
would return:虽然
y.data
与图形有些分离(没有grad_fn
函数),但它不是y
的副本,因为y.detach()
会返回:
>>> y.data
tensor([0.5272, 0.3162, 0.1374, 0.3004]
Therefore, if you modify y.data
's components you end modifying y
itself:因此,如果您修改
y.data
的组件,您将结束修改y
本身:
>>> y.data[0] = 1
>>> y
tensor([1.0000, 0.3162, 0.1374, 0.3004], grad_fn=<PowBackward0>)
Notice how the grad_fn
didn't change there.注意
grad_fn
在那里没有改变。 If you had done y[0] = 1
, grad_fn
would have been updated to <CopySlices>
.如果您完成了
y[0] = 1
, grad_fn
将更新为<CopySlices>
。 This shows that modify your tensor's data through .data
is not accounted for in terms of gradient, ie , you won't be able to backpropagate these operations.这表明通过
.data
修改张量的数据不考虑梯度,即,您将无法反向传播这些操作。 It is required to work with y
- not y.data
- when planning to use Autograd.计划使用 Autograd 时需要使用
y
-而y.data
y.data。
So, to give an answer to your question: a_tensor.data
is a torch.*Tensor
, same type as a_tensor
, and a_tensor.data.max
is a function bound to that tensor.因此,要回答您的问题:
a_tensor.data
是一个torch.*Tensor
,与a_tensor
类型相同,而a_tensor.data.max
是绑定到该张量的 function 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.