湖上湖
是的,您可以在 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}) |+-------------------------+------------------------------------------------------------------------+