The difference between '.data' and '.detach()'?

Anyone can tell me how to choose the method about ‘.data’ and ‘.detach()’.In some code,I see some use ‘.data’ and another use ‘.detach()’.Anyone can tell me when I use ‘.data’ or ‘.detach()’?Can I only use ‘.detach()’ or ‘.data’?Thanks!


You should always use .detach() if you want to detach a tensor from the graph. The other option .data is for older versions of PyTorch, and it is likely that it will be removed from the future versions of PyTorch.


When backward, if you only update part network. For example, when training GAN, updating only netD and not updating netG, you should write like this:
fake = netG(noise)
out_ = netD(fake.detach())
loss = …
As for .data, from data-type Variable, get data through '.data '.


Thank you very much!

Thanks for your reply!Can I only use ‘.detach()’ in any condition?Thanks!

No. .detach() is to detach a tensor from the network graph, making the tensor no gradient, while ‘.data’ is only to obtain tensor-data from Variable. They have different functions, which are used in different cases.