Inplace Fill Starting From a Specific Index

I have a 1D tensor of a fixed dimension 11. I want to fill each position with a certain value starting from an index. For example,

x = Tensor([1,2,3,4,5,6,7,8,9,10,11])

I want to replace each value with -1 starting from index 7 to get a result,

x = Tensor([1,2,3,4,5,6,7,-1,-1,-1,-1])

How can I attain this easily?

This way:

import torch
x = torch.Tensor([1,2,3,4,5,6,7,8,9,10,11])
x[7:].fill_(-1)
print(x)
1 Like

Thank you so much for the help!

1 Like