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

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

torch.linspace(0, 10, 10)

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

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

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


HUX布斯
浏览 64回答 0
0回答

湖上湖

是的,您可以在 libtorch 中使用切片和索引。你可以做:auto tensor = torch::linspace(0, 10, 10).index({ Slice(None, 4) });基本上如文档中所示:主要区别在于,在 C++ API 中,索引方法不是使用类似于 Python API 语法的 [] 运算符,而是:torch::Tensor::index(链接)torch::Tensor::index_put_ (链接)还需要注意的是,诸如 None / Ellipsis / Slice 之类的索引类型位于 torch::indexing 命名空间中,建议将 using 命名空间 torch::indexing 放在任何索引代码之前,以方便使用这些索引类型。为了方便起见,这里是从我刚刚给出的链接中获取的一些 Python 与 C++ 转换:以下是将 Python 索引代码转换为 C++ 的一些示例:Getter------+----------------------------------------------------------+--------------------------------------------------------------------------------------+| Python                                                   | C++  (assuming  using namespace torch::indexing )                                    |+==========================================================+======================================================================================+|  tensor[Noe]                                            |  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})                                                 |+-------------------------+------------------------------------------------------------------------+
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python