问题
I'm new to pytorch. I read much pytorch code which heavily uses tensor's .data member. But I search .data in the official document and Google, finding little. I guess .data contains the data in the tensor, but I don't know when we need it and when not?
回答1:
.data was an attribute of Variable (object representing Tensor with history tracking e.g. for automatic update), not Tensor. Actually, .data was giving access to the Variable's underlying Tensor.
However, since PyTorch version 0.4.0, Variable and Tensor have been merged (into an updated Tensor structure), so .data disappeared along the previous Variable object (well Variable is still there for backward-compatibility, but is deprecated).
Paragraph from Release Notes for version 0.4.0 (I recommend reading the whole section about Variable/Tensor updates):
What about
.data?
.datawas the primary way to get the underlyingTensorfrom aVariable. After this merge, callingy = x.datastill has similar semantics. Soywill be aTensorthat shares the same data withx, is unrelated with the computation history ofx, and hasrequires_grad=False.However,
.datacan be unsafe in some cases. Any changes onx.datawouldn't be tracked byautograd, and the computed gradients would be incorrect ifxis needed in a backward pass. A safer alternative is to usex.detach(), which also returns aTensorthat shares data withrequires_grad=False, but will have its in-place changes reported byautogradifxis needed in backward.
来源:https://stackoverflow.com/questions/51743214/is-data-still-useful-in-pytorch