Assigning value at given target indices


Sorry if this question has been asked already. I couldn’t find a solution anywhere.

I want to assign a value to a tensor at particular column indices.

For instance, given a tensor t1 = [[0.3,0.4,0.5], [0.1,0.2, 0.8]] , indices = [1,2] and value = 1. I want to compute t2 such that t2 = [[0.3,1, 0.5], [0.1, 0.2, 1]].

Basically, I would like to do below with pytorch tensor indexing

for i in range(t1.size(0)):
t2[i][indices[i]] = value


P.S. I tried scatter and index_select but couldn’t figure out how to use them for above problem.

You can look up how they convert class label to 1 hot coding vector, then use it as a mask, for example t2 = mask * value + (1 - mask) * t1