Search code examples
pythontheanoarray-broadcasting

Adding tensors along certain dimensions with Theano


I am trying to understand how to use dimshuffle to broadcast noise to a 4 dimensional tensor. For example the 4 dimensional tensor has shape (num_examples, 3, 32, 32) where 3 corresponds to the r,g,b components of the 32*32 image. I want to add Gaussian noise, however I want to add the same noise to all the three components (i.e r,g,b) and for each example the noise would be a random 32*32 matrix.

So I have to add the two tensors of dimensions (num_examples, 3, 32, 32) and (num_examples, 32, 32), respectively. I have not been able to figure out how to use dimshuffle on the second tensor so that its last two dimensions become broadcastable.


Solution

  • Use dimshuffle

    x = T.tensor4() # Nx3x32x32
    y = T.tensor3() # Nx32x32
    z = y.dimshuffle(0, 'x', 1, 2) + x