Python 的数组切片在 C++(libtorch)中有类比吗?

Is there an analogy for Python's array slicing in C++ (libtorch)?

在 Python 中使用 PyTorch,如果你有一个数组:

torch.linspace(0, 10, 10)

您可以使用例如只说前三个元素

reduced_tensor = torch.linspace(0, 10, 10)[:4].

是否有类似于 C++/libtorch 中的 [:] 数组切片的方法?如果没有,我怎样才能轻松做到这一点?

是的,您可以在 libtorch 中使用 Slice 和索引。你可以这样做:

auto tensor = torch::linspace(0, 10, 10).index({ Slice(None, 4) });

您可以阅读有关索引的更多信息here
基本上如文档中所示:

The main difference is that, instead of using the []-operator similar to the Python API syntax, in the C++ API the indexing methods are:

torch::Tensor::index (link)

torch::Tensor::index_put_ (link)

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.

为方便起见,这里有一些 Python 与 C++ 的转换,取自 link 我刚刚给出的:

以下是一些将 Python 索引代码转换为 C++ 的示例:

Getter
------

+----------------------------------------------------------+--------------------------------------------------------------------------------------+
| Python                                                   | C++  (assuming  using namespace torch::indexing )                                    |
+==========================================================+======================================================================================+
|  tensor[None]                                            |  tensor.index({None})                                                                |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[Ellipsis, ...]                                   |  tensor.index({Ellipsis, "..."})                                                     |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[1, 2]                                            |  tensor.index({1, 2})                                                                |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[True, False]                                     |  tensor.index({true, false})                                                         |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[1::2]                                            |  tensor.index({Slice(1, None, 2)})                                                   |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[torch.tensor([1, 2])]                            |  tensor.index({torch::tensor({1, 2})})                                               |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+
|  tensor[..., 0, True, 1::2, torch.tensor([1, 2])]        |  tensor.index({"...", 0, true, Slice(1, None, 2), torch::tensor({1, 2})})            |
+----------------------------------------------------------+--------------------------------------------------------------------------------------+


Translating between Python/C++ index types
------------------------------------------

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                                                              |
+-------------------------+------------------------------------------------------------------------+
|  ...                    |  "..."                                                                 |
+-------------------------+------------------------------------------------------------------------+
|  123                    |  123                                                                   |
+-------------------------+------------------------------------------------------------------------+
|  True                   |  true                                                                  |
+-------------------------+------------------------------------------------------------------------+
|  False                  |  false                                                                 |
+-------------------------+------------------------------------------------------------------------+
|  :  or  ::              |  Slice()  or  Slice(None, None)  or  Slice(None, None, None)           |
+-------------------------+------------------------------------------------------------------------+
|  1:  or  1::            |  Slice(1, None)  or  Slice(1, None, None)                              |
+-------------------------+------------------------------------------------------------------------+
|  :3  or  :3:            |  Slice(None, 3)  or  Slice(None, 3, None)                              |
+-------------------------+------------------------------------------------------------------------+
|  ::2                    |  Slice(None, None, 2)                                                  |
+-------------------------+------------------------------------------------------------------------+
|  1:3                    |  Slice(1, 3)                                                           |
+-------------------------+------------------------------------------------------------------------+
|  1::2                   |  Slice(1, None, 2)                                                     |
+-------------------------+------------------------------------------------------------------------+
|  :3:2                   |  Slice(None, 3, 2)                                                     |
+-------------------------+------------------------------------------------------------------------+
|  1:3:2                  |  Slice(1, 3, 2)                                                        |
+-------------------------+------------------------------------------------------------------------+
|  torch.tensor([1, 2])   |  torch::tensor({1, 2})                                                 |
+-------------------------+------------------------------------------------------------------------+