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

MD9*_*D98 1 c++ python indexing pytorch libtorch

在带有 PyTorch 的 Python 中,如果你有一个数组:

torch.linspace(0, 10, 10)

你可以只使用前三个元素,例如

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

C++/libtorch 中是否有类似于数组切片的模拟[:]?如果没有,我怎样才能轻松实现这一目标?

Rik*_*ika 9

是的,您可以在 libtorch 中使用切片和索引。你可以做:

\n
auto tensor = torch::linspace(0, 10, 10).index({ Slice(None, 4) });\n
Run Code Online (Sandbox Code Playgroud)\n

您可以在此处阅读有关索引的更多信息。
\n基本上如文档中所示:

\n
\n

主要区别在于,在 C++ API 中,索引方法是:

\n

torch::Tensor::index(链接

\n

torch::Tensor::index_put_ (链接

\n

\xe2\x80\x99s 还需要注意的是,像 None / Ellipsis /\nSlice 这样的索引类型存在于 torch::indexing 命名空间中,并且 \xe2\x80\x99s 建议在 \n之前使用命名空间 torch::indexing用于方便使用这些索引类型的任何索引代码。

\n
\n

为了方便起见,这里是从我刚刚给出的链接中获取的一些 Python 与 C++ 转换:

\n

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

\n
Getter\n------\n\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n| Python                                                   | C++  (assuming  using namespace torch::indexing )                                    |\n+==========================================================+======================================================================================+\n|  tensor[None]                                            |  tensor.index({None})                                                                |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[Ellipsis, ...]                                   |  tensor.index({Ellipsis, "..."})                                                     |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[1, 2]                                            |  tensor.index({1, 2})                                                                |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[True, False]                                     |  tensor.index({true, false})                                                         |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[1::2]                                            |  tensor.index({Slice(1, None, 2)})                                                   |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[torch.tensor([1, 2])]                            |  tensor.index({torch::tensor({1, 2})})                                               |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n|  tensor[..., 0, True, 1::2, torch.tensor([1, 2])]        |  tensor.index({"...", 0, true, Slice(1, None, 2), torch::tensor({1, 2})})            |\n+----------------------------------------------------------+--------------------------------------------------------------------------------------+\n\n\nTranslating between Python/C++ index types\n------------------------------------------\n\nThe one-to-one translation between Python and C++ index types is as follows:\n\n+-------------------------+------------------------------------------------------------------------+\n| Python                  | C++ (assuming  using namespace torch::indexing )                       |\n+=========================+========================================================================+\n|  None                   |  None                                                                  |\n+-------------------------+------------------------------------------------------------------------+\n|  Ellipsis               |  Ellipsis                                                              |\n+-------------------------+------------------------------------------------------------------------+\n|  ...                    |  "..."                                                                 |\n+-------------------------+------------------------------------------------------------------------+\n|  123                    |  123                                                                   |\n+-------------------------+------------------------------------------------------------------------+\n|  True                   |  true                                                                  |\n+-------------------------+------------------------------------------------------------------------+\n|  False                  |  false                                                                 |\n+-------------------------+------------------------------------------------------------------------+\n|  :  or  ::              |  Slice()  or  Slice(None, None)  or  Slice(None, None, None)           |\n+-------------------------+------------------------------------------------------------------------+\n|  1:  or  1::            |  Slice(1, None)  or  Slice(1, None, None)                              |\n+-------------------------+------------------------------------------------------------------------+\n|  :3  or  :3:            |  Slice(None, 3)  or  Slice(None, 3, None)                              |\n+-------------------------+------------------------------------------------------------------------+\n|  ::2                    |  Slice(None, None, 2)                                                  |\n+-------------------------+------------------------------------------------------------------------+\n|  1:3                    |  Slice(1, 3)                                                           |\n+-------------------------+------------------------------------------------------------------------+\n|  1::2                   |  Slice(1, None, 2)                                                     |\n+-------------------------+------------------------------------------------------------------------+\n|  :3:2                   |  Slice(None, 3, 2)                                                     |\n+-------------------------+------------------------------------------------------------------------+\n|  1:3:2                  |  Slice(1, 3, 2)                                                        |\n+-------------------------+------------------------------------------------------------------------+\n|  torch.tensor([1, 2])   |  torch::tensor({1, 2})                                                 |\n+-------------------------+------------------------------------------------------------------------+\n
Run Code Online (Sandbox Code Playgroud)\n