How to scatter with variable length of index

I want to use create multiple-hot vector. I tried to use scatter_ function of Tensor. It works fine for the fixed length of indices. batch_size = 4 dim = 7 idx = torch.LongTensor([[0,1],[2,3],[0,4],[0,5]]) hot_vec = hot_v = torch.zeros(batch_size, dim) hot_vec.scatter_(1, idx, 1.0) result …