Adapt tensor layout
A model emits features and timesteps in an order expected by a subsequent layer.
Keras · Class
Reorders an input tensor's non-batch dimensions using a one-based permutation tuple. The output preserves the same dimensions but presents them in the requested order.
Permute reorders an input tensor's non-batch dimensions according to the supplied tuple. Dimension positions are one-based, so each tuple value identifies an input dimension and the output retains the same dimensions in the requested order.
keras.layers.Permute(dims, **kwargs)| Name | Type | Description |
|---|---|---|
dims | tuple of integers | Tuple of integers defining the non-batch dimension order; numbering starts at 1. |
A tensor with the same dimensions as the input, reordered according to dims.
dims is a tuple of integers specifying the permutation.(1, 3, 2), the first non-batch dimension stays in place while the next two are exchanged.import keras
x = keras.Input(shape=(10, 64))
y = keras.layers.Permute((2, 1))(x)
print(y.shape) # (None, 64, 10)This swaps the two non-batch dimensions of the input tensor.
A model emits features and timesteps in an order expected by a subsequent layer.
A recurrent block and convolutional block use different dimension conventions.
keras.layers.Permute do?Permute changes only the order of the input dimensions; it does not change their sizes or the batch axis. For an input shaped (batch, 10, 64), Permute((2, 1)) produces (batch, 64, 10).
Follow-up: How would you permute a three-dimensional non-batch shape (a, b, c) to (c, a, b)? Use Permute((3, 1, 2)).
dims tuple interpreted?The dims argument is a tuple of integers describing the output order of the input's non-batch dimensions. Its numbering starts at 1, and the batch dimension is omitted.
Follow-up: What would (1, 3, 2) do to an input with shape (batch, a, b, c)? It would output (batch, a, c, b).
The layer accepts an arbitrary input shape and returns the same shape with dimensions reordered according to dims. For example, an input (None, 10, 64) becomes (None, 64, 10) with (2, 1).
Follow-up: Why might this be useful in a model? It can adapt tensor layouts when connecting recurrent layers and convolutional networks.
keras.layers.Reshapekeras.layers.Flattenkeras.layers.RepeatVector