11

I often use theano.tensor.dimshuffle. Is there an equivalent function for Numpy?

I guess I could do the same via several numpy.swapaxes and numpy.newaxis (for broadcast dimensions), numpy.reshape but is there some simpler or more direct way, just like dimshuffle?

ali_m
  • 71,714
  • 23
  • 223
  • 298
Albert
  • 65,406
  • 61
  • 242
  • 386

1 Answers1

11

The function numpy.transpose permits any permutation of the axes of an array.

The variety array.T is a special case of this, corresponding to array.transpose() without arguments, which defaults to array.transpose(range(array.ndim)[::-1]).

numpy.swapaxes is numpy.transpose restricted to permutations of two axes.

theano.tensor.dimshuffle essentially corresponds to numpy.transpose, but in addition, it permits the creation of new axes of length 1 for broadcasting, by adding 'x' wherever an axis should be created. In numpy, this can be achieved using a combination of transpose and reshape.

Note that in numpy, care is taken to make transpose return a view on the data whenever possible. In theano this is probably the case, too, but may depend on how the code is optimized.

eickenberg
  • 14,152
  • 1
  • 48
  • 52