.data 在 pytorch 中仍然有用吗?
Is .data still useful in pytorch?
我是 pytorch 新手。我读了很多大量使用张量 .data
成员的 pytorch 代码。但是我在官方文档中搜索.data
和Google,几乎没有找到。我猜想 .data
包含张量中的数据,但我不知道我们什么时候需要它,什么时候不需要?
.data
是 Variable
的属性(代表 Tensor
的对象,具有历史跟踪,例如用于自动更新),而不是 Tensor
。实际上,.data
正在授予对 Variable
底层 Tensor
.
的访问权
但是,由于 PyTorch 版本 0.4.0
、Variable
和 Tensor
已合并(进入更新的 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);
在那种情况下:
point.data
将是一个与 point
. 共享相同数据的张量
您可以通过以下方式查看:point.data_ptr(); point.data.data_ptr();
它与point.data
的计算历史无关,有requires_grad=False
甚至point
有requires_grad=True
autograd
不会跟踪 initial_point.data 上的任何更改
文档:
我是 pytorch 新手。我读了很多大量使用张量 .data
成员的 pytorch 代码。但是我在官方文档中搜索.data
和Google,几乎没有找到。我猜想 .data
包含张量中的数据,但我不知道我们什么时候需要它,什么时候不需要?
.data
是 Variable
的属性(代表 Tensor
的对象,具有历史跟踪,例如用于自动更新),而不是 Tensor
。实际上,.data
正在授予对 Variable
底层 Tensor
.
但是,由于 PyTorch 版本 0.4.0
、Variable
和 Tensor
已合并(进入更新的 Tensor
结构),因此 .data
消失了之前的 Variable
对象(为了向后兼容,Variable
仍然存在,但已弃用)。
来自 Release Notes 版本 0.4.0
的段落(我建议阅读有关 Variable
/Tensor
更新的整个部分):
What about
.data
?
.data
was the primary way to get the underlyingTensor
from aVariable
. After this merge, callingy = x.data
still has similar semantics. Soy
will be aTensor
that shares the same data withx
, is unrelated with the computation history ofx
, and hasrequires_grad=False
.However,
.data
can be unsafe in some cases. Any changes onx.data
wouldn't be tracked byautograd
, and the computed gradients would be incorrect ifx
is needed in a backward pass. A safer alternative is to usex.detach()
, which also returns aTensor
that shares data withrequires_grad=False
, but will have its in-place changes reported byautograd
ifx
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);
在那种情况下:
point.data
将是一个与point
. 共享相同数据的张量
您可以通过以下方式查看:point.data_ptr(); point.data.data_ptr();
它与
point.data
的计算历史无关,有requires_grad=False
甚至point
有requires_grad=True
autograd
不会跟踪 initial_point.data 上的任何更改
文档: