Python covert to c++ torch copy operation

Hi all

How does the code below convert to c++?
I need your help.

thanks all

 // batched_imgs ,pad_img tensor list 
batch_shape = [len(images)] + max_size
batched_imgs = images[0].new_full(batch_shape, 0)
    for img, pad_img in zip(images, batched_imgs):
         pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)

I solved it.
do it like this

    torch::Tensor batched_imgs = tensors[0].new_full({ (int64_t)tensors.size() ,tensors[0].size(0),max_size_first, max_size_second },0);
    std::vector<torch::Tensor> image_sizes;
    for (int i = 0; i < tensors.size(); ++i) 
        batched_imgs[i].narrow(/*dim=*/1, /*start=*/0, /*length=*/tensors[i].size(1))
            .narrow(/*dim=*/2, /*start=*/0, /*length=*/tensors[i].size(2)).copy_(tensors[i]);

Starting from the current nightly build (and PyTorch 1.5 soon), for

pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)

we can write

using namespace torch::indexing;
pad_img.index({Slice(None, img.size(0)), Slice(None, img.size(1)), Slice(None, img.size(2))}).copy_(img)

Here is the general translation for Tensor::index and Tensor::index_put_ functions:

Python             C++ (assuming `using namespace at::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}})
1 Like

Oh Good, I Look forward
Thank You