Heuristic on whether to use `.data` on a Tensor

What is the best way to determine whether using .data on a Tensor is need? Here’s an example from Lesson #9 where I’m unclear it’s needed (removing .data seems to have no effect):

overlaps = jaccard(bbox.data, self.anchor_cnr.data)

It used to be necessary with pytorch < 0.4, I don’t know what version you’re using now.

I’m on pytorch 1.0.1. Good to know it’s not necessary - thanks! Here’s an example where .data is needed (in defining the output convolution layer for SSD):

        self.oconv1.bias.data.zero_().add_(bias)

Removing it causes an error:

    self.oconv1.bias.zero_().add_(bias)
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.

That’s because it is a Parameter of a model, not a Tensor. In general, when dealing with model parameters, if you want to do something that doesn’t require gradient tracking, you should use the data attribute.
But for pure Tensor, the data attribute isn’t useful anymore. You might have to detach it from its history, but that’s all.

2 Likes