.data 在 pytorch 中仍然有用吗?

Is .data still useful in pytorch?

我是 pytorch 新手。我读了很多大量使用张量 .data 成员的 pytorch 代码。但是我在官方文档中搜索.data和Google,几乎没有找到。我猜想 .data 包含张量中的数据,但我不知道我们什么时候需要它,什么时候不需要?

.dataVariable 的属性(代表 Tensor 的对象,具有历史跟踪,例如用于自动更新),而不是 Tensor。实际上,.data 正在授予对 Variable 底层 Tensor.

的访问权

但是,由于 PyTorch 版本 0.4.0VariableTensor 已合并(进入更新的 Tensor 结构),因此 .data 消失了之前的 Variable 对象(为了向后兼容,Variable 仍然存在,但已弃用)。


来自 Release Notes 版本 0.4.0 的段落(我建议阅读有关 Variable/Tensor 更新的整个部分):

What about .data?

.data was the primary way to get the underlying Tensor from a Variable. After this merge, calling y = x.data still has similar semantics. So y will be a Tensor that shares the same data with x, is unrelated with the computation history of x, and has requires_grad=False.

However, .data can be unsafe in some cases. Any changes on x.data wouldn't be tracked by autograd, and the computed gradients would be incorrect if x is needed in a backward pass. A safer alternative is to use x.detach(), which also returns a Tensor that shares data with requires_grad=False, but will have its in-place changes reported by autograd if x is needed in backward.

除了@benjaminplanche 的回答,我会用它来手动更改参数值。

例如,我有以下模型:

model = nn.Sequential(nn.Linear(10, 1))

出于某种原因,我想手动更新其参数值。那么,我可以这样做:

for param in model.parameters():
    param.data = 10 * param.data  # multiply the parameter values by 10.

请注意,如果我们删除param后面的.data,参数值将不会更新。

这种用法可以在BYOL (Bootstrap your own latent) and this Github webpage for BYOL pytorch implementation中找到。

从 PyTorch 0.4.0 和至少版本 1.7.1 - 您可以通过此属性“.data”获取 torch.Tensor 内容。

假设您有 point=torch.Tensor(size=(1,2,)); point.requires_grad_(True);

在那种情况下:

  1. point.data 将是一个与 point.
  2. 共享相同数据的张量

您可以通过以下方式查看:point.data_ptr(); point.data.data_ptr();

  1. 它与point.data的计算历史无关,有requires_grad=False甚至pointrequires_grad=True

  2. autograd

    不会跟踪 initial_point.data 上的任何更改

文档:

https://github.com/pytorch/pytorch/releases/tag/v0.4.0