Emulating slices on multidimensional Tensors

I have to add a subset of one tensor to another. What is an efficient way of doing this without slices?


  • tensor A where A.size() = [10, 256, 23, 39]
  • tensor B where B.size() = [10, 256, 16, 32]

How do I emulate this statement in PyTorch?

A[:,:,3:19, 3:35] += B

Doing what you suggest would be an inplace op that pytorch can’t differentiate, but I suppose you want to do it anyway.

The naive solution would be to slice and dice, then put everything back together again. Something like this, which I think should be pretty performant.

bits1 = A[:,:,:3]   # hopefully pytorch just creates a new view onto 
bits2 = A[:,:,3:19] # the same data in memory
bits3 = A[:,:,19:]
bits21 = bits2[:,:,:,:3]
bits22 = bits2[:,:,:,3:35] + B
bits23 = bits2[:,:,:,35:]

bits2 = torch.cat((bits21, bits22, bits23), dim=3)
C = torch.cat((bits1, bits2, bits3), dim=2)