简体   繁体   English

torch.Tensor 的“data.max”是多少?

[英]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_tensortorch.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] = 1grad_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 。

a_Tensor.data is of type torch.Tensor as well. a_Tensor.data也是torch.Tensor类型。

You can find the details related to Tensor.max() fromhere .您可以从此处找到与Tensor.max()相关的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM