swap tensor axis in keras
You can use K.permute_dimensions()
which is exactly similar to np.transpose()
.
Example:
import numpy as np
from keras import backend as K
A = np.random.random((1000,32,64,3))
# B = np.moveaxis( A, 3, 1)
C = np.transpose( A, (0,3,1,2))
print A.shape
print C.shape
A_t = K.variable(A)
C_t = K.permute_dimensions(A_t, (0,3,1,2))
print K.eval(A_t).shape
print K.eval(C_t).shape
Use keras.layers.Permute(dims)
where dims
does not include the samples dimension
model.add(Permute((2, 1), input_shape=(10, 64)))