How to index into a tensor using a tensor?

Imagine I’m doing RL, and I have per-state values represented as a tensor:

V = torch.IntTensor(2, 3)

Then I have a state represented as another tensor:

s = torch.IntTensor([1, 1])

… and I want to increment the corresponding value in V by 1, something like:

V[s] += 1 , or += 1, or

thoughts on how to do this? (I’d prefer not to have to write V[s[0], s[1], s[2], ...] += 1 ideally). Happy to convert the index tensor to a LongTensor, if that will help?

1 Like