Selecing using list indices in c++

Hi team,
I am looking for your help in replicating the list based indexing functionality of numpy in ::Tensors.

For example:

a = np.zeros((100, 100))
xs = [1, 2, 5, 6]
ys = [4, 5, 7, 3]

res = a[xs, ys].

Replicating it in torch c++:

auto a = tensor::zeros({100, 100});

int xs[] = {1, 2, 3};
int ys[] = {1, 2, 3};

auto xs = torch::from_blob(xs, {3}, torch::kInt);
auto ys = torch::from_blob(ys, {3}, torch::kInt);

/* doing the following results in a  memory access error */
auto result = a[xs][ys];

My apologies if it is a cause of something silly.

Best,
Kaunil D.

oh that c++ codes,
Why you are using c++ instead of python? Any reason for using this.
Thanks for advice.

1 Like

Yeah: The routines preceding this are built to utilize CUDA for massaging high dimensional satellite imagery/ point clouds.

1 Like

oh thank you .Is it not possible to make in python pytorch :confused:

saving time in context switches. manageblity. 1 Cmake does it all. ITS FUN.

1 Like

Oh thanks for explaining my doubts :slight_smile: .

try
auto result = torch::index(a, { xs,ys });
or creating TensorList for second argument.

Starting from currently nightly build and 1.5 release, the best way to do it in C++ is:
Python:

a = np.zeros((100, 100))
xs = [1, 2, 5, 6]
ys = [4, 5, 7, 3]

res = a[xs, ys]

C++:

auto a = torch::zeros({100, 100});
auto xs = torch::tensor({1, 2, 5, 6});
auto ys = torch::tensor({4, 5, 7, 3});

auto res = a.index({xs, ys});
2 Likes

It works! Thank you for the quick turnaround @yf225