是的,您可以在 libtorch 中使用切片和索引。你可以做:
\nauto tensor = torch::linspace(0, 10, 10).index({ Slice(None, 4) });\n
Run Code Online (Sandbox Code Playgroud)\n您可以在此处阅读有关索引的更多信息。
\n基本上如文档中所示:
\n\n主要区别在于,在 C++ API 中,索引方法是:
\ntorch::Tensor::index(链接)
\ntorch::Tensor::index_put_ (链接)
\n\xe2\x80\x99s 还需要注意的是,像 None / Ellipsis /\nSlice 这样的索引类型存在于 torch::indexing 命名空间中,并且 \xe2\x80\x99s 建议在 \n之前使用命名空间 torch::indexing用于方便使用这些索引类型的任何索引代码。
\n
为了方便起见,这里是从我刚刚给出的链接中获取的一些 Python 与 C++ 转换:
\n以下是将 Python 索引代码转换为 C++ 的一些示例:
\nGetter\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
归档时间: |
|
查看次数: |
2871 次 |
最近记录: |