在带有PyTorch的Python中,如果您有一个数组:
torch.linspace(0, 10, 10)
您可以使用例如,只使用前三个元素
reduced_tensor = torch.linspace(0, 10, 10)[:4]。
在C++/lib手电筒中对[:]数组进行切片是否有类似的方法?如果没有,我怎样才能轻易做到这一点呢?
发布于 2020-10-22 12:36:50
是的,你可以使用切片和索引。你可以:
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++的一些示例:
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}) |
+-------------------------+------------------------------------------------------------------------+https://stackoverflow.com/questions/64482223
复制相似问题