Pytorch 1.5 使用Tensor::index
和Tensor::index_put_
using namespace torch::indexing;
auto myints = torch::Tensor({10, 20, 30, 40, 50, 60, 70});
auto myvector = torch::ones({18});
myvector.index_put_({3, 7}, myints.index({0, 3}));
Tensor::index
和的一般翻译Tensor::index_put_
Python C++ (assuming `using namespace torch::indexing`)
-------------------------------------------------------------------
0 0
None None
... "..." or Ellipsis
: Slice()
start:stop:step Slice(start, stop, step)
True / False true / false
[[1, 2]] torch::tensor({{1, 2}})
Pytorch 1.4 替代功能
Tensor Tensor::narrow(int64_t dim, int64_t start, int64_t length)
Tensor & Tensor::copy_(const Tensor & src, bool non_blocking=false)
narrow
几乎完全一样slice
并copy_
用于分配
auto myints = torch::Tensor({10, 20, 30, 40, 50, 60, 70});
auto myvector = torch::ones({18});
myvector.narrow(0, 3, 4).copy_(myvector.narrow(0, 0, 3));