Tensor Indexing API#
Indexing a tensor in the PyTorch C++ API works very similar to the Python API.All index types such asNone /... / integer / boolean / slice / tensorare available in the C++ API, making translation from Python indexing code to C++very simple. The main difference is that, instead of using the[]-operatorsimilar to the Python API syntax, in the C++ API the indexing methods are:
It’s also important to note that index types such asNone /Ellipsis /Slicelive in thetorch::indexing namespace, and it’s recommended to putusingnamespacetorch::indexingbefore 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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|