PyTorch slicing

Have tensor like

tensor =  [1,2,3,4,5,6,7,8,9,10].

How I can get tensor like this:


take 2 elements with step 2(::2)

I try something like tensor[:2::2] .it’s not work


[:2::2] is not a valid indexing syntax in python.
And just like for a regular python list, you cannot do what you want with just indexing.
You can either take the slices by hand and then to get the final result.
Or generate the indices you want to keep and use torch.index_select or torch.gather to get the values.