2

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
  • 1
    I don't think there's a difference. You can benchmark it yourself and see.
    – Shai
    Commented May 11, 2021 at 5:06

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.