In Python with PyTorch, if you have an array:
torch.linspace(0, 10, 10)
you can use e.g. only the first three elements by saying
reduced_tensor = torch.linspace(0, 10, 10)[:4]
.
Is there an analog to the [:]
array slicing in C++/libtorch? If not, how can I achieve this easily?
Yes, You can use Slice and index in libtorch. You can do:
You can read more about indexing here.
Basically as its indicated in the documentation :
For the convenience here is some of the Python vs C++ conversions taken from the link I just gave:
Here are some examples of translating Python indexing code to C++: