首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中,Python的数组切片有类似之处吗?

在C++中,Python的数组切片有类似之处吗?
EN

Stack Overflow用户
提问于 2020-10-22 12:28:14
回答 1查看 1.7K关注 0票数 1

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

torch.linspace(0, 10, 10)

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

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

在C++/lib手电筒中对[:]数组进行切片是否有类似的方法?如果没有,我怎样才能轻易做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-22 12:36:50

是的,你可以使用切片和索引。你可以:

代码语言:javascript
复制
auto tensor = torch::linspace(0, 10, 10).index({ Slice(None, 4) });

您可以阅读更多关于索引这里的内容。

基本上如文件中所述:

主要区别在于,在Python中,索引方法不是使用类似于Python语法的[]-operator,而是使用以下索引方法: 火炬::张量::索引(链接) torch::Tensor::index_put_ (链接) 还需要注意的是,索引类型(如None / Ellipsis / Slice )活动在torch::index命名空间中,建议在索引代码之前使用名称空间torch::索引,以便方便地使用这些索引类型。

为了方便起见,这里是从我刚才提供的链接中提取的一些Python C++转换:

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

代码语言:javascript
复制
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})                                                 |
+-------------------------+------------------------------------------------------------------------+
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64482223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档