Assign value to sub-tensor in Pytorch

Viewed 372

Suppose I have a PyTorch tensor t of shape (3, 3, 3, 3), and I want to set t[0, 1, x, y] to z for all x and y. I noticed all the following syntaxes work:

  • t[0, 1] = z
  • t[0, 1, :] = z
  • t[0, 1, :, :] = z
  • t[0, 1, ...] = z

Is there any difference in terms of how things are executed under the hood? Is any one of these methods preferred over the others?

1 Answers

There is a subtle difference in how : and ... are translated to C++ under the hood:

The one-to-one translation between Python and C++ index types is as follows:

Python C++ (assuming using namespace torch::indexing)
None None
Ellipsis Ellipsis
... "..."
: or :: Slice() or Slice(None, None) or Slice(None, None, None)

Practically this has no effect on performance however. Use whichever style is most readable in your context.

Related