Tensor Indexing API¶
Indexing a tensor in the PyTorch C++ API works very similar to the Python API.
All index types such as None
/ ...
/ integer / boolean / slice / tensor
are available in the C++ API, making translation from Python indexing code to C++
very simple. The main difference is that, instead of using the []
-operator
similar to the Python API syntax, in the C++ API the indexing methods are:
It’s also important to note that index types such as None
/ Ellipsis
/ Slice
live in the torch::indexing
namespace, and it’s recommended to put using namespace torch::indexing
before any indexing code for convenient use of those index types.
Here are some examples of translating Python indexing code to C++:
Getter¶
Python |
C++ (assuming |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Setter¶
Python |
C++ (assuming |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Translating between Python/C++ index types¶
The one-to-one translation between Python and C++ index types is as follows:
Python |
C++ (assuming |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|