How to implement this code without loop

#batch is a tensor with shape HW

  for i in range(H):
      batch[i,:]=i*batch[i,:]

T T please

This should work:

batch = torch.ones(10, 4)
batch = torch.arange(batch.size(0)).view(-1, 1) * batch

If you are dealing with more dimensions, you might need to change the view call.

1 Like