Replace values in a 1D tensor with step

I am creating a 1D tensor like this:
t = torch.ones(4)
so t is:
tensor([1., 1., 1., 1.])
now I want to change the values of these tensor with a step so I can get a tensor like:
tensor([1., -1., 1., -1.])

How come this:
t[1:-1:2] = -1
produces this:
tensor([ 1., -1., 1., 1.]) ?

for your scenario,

t[1::2] = -1

because you are excluding the last index by having -1 (t[1:-1:2]).

1 Like

you’re right thanks. I wasn’t very careful with that -1!