keras-team/keras · error · ValueError

Invalid permutation argument `dims` for Permute Layer. The s

Error message

Invalid permutation argument `dims` for Permute Layer. The set of indices in `dims` must be consecutive and start from 1. Received dims={dims}

What it means

Permute.__init__ validates that dims is a permutation of the consecutive integers 1..len(dims), excluding the batch axis. Anything else — duplicates, zeros, gaps, non-consecutive values — is rejected immediately at layer construction because no valid transpose exists for it.

Source

Thrown at keras/src/layers/reshaping/permute.py:39

        Arbitrary.

    Output shape:
        Same as the input shape, but with the dimensions re-ordered according
        to the specified pattern.

    Example:

    >>> x = keras.Input(shape=(10, 64))
    >>> y = keras.layers.Permute((2, 1))(x)
    >>> y.shape
    (None, 64, 10)
    """

    def __init__(self, dims, **kwargs):
        super().__init__(**kwargs)
        self.dims = tuple(dims)
        if sorted(dims) != list(range(1, len(dims) + 1)):
            raise ValueError(
                "Invalid permutation argument `dims` for Permute Layer. "
                "The set of indices in `dims` must be consecutive and start "
                f"from 1. Received dims={dims}"
            )
        self.input_spec = InputSpec(ndim=len(self.dims) + 1)

    def compute_output_shape(self, input_shape):
        output_shape = [input_shape[0]]
        for dim in self.dims:
            output_shape.append(input_shape[dim])
        return tuple(output_shape)

    def compute_output_spec(self, inputs):
        output_shape = self.compute_output_shape(inputs.shape)
        return KerasTensor(
            shape=output_shape, dtype=inputs.dtype, sparse=inputs.sparse
        )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Rewrite dims as a shuffle of 1..N where N is the tensor rank minus the batch axis
  2. If porting 0-based NumPy axes, add 1 to every entry: np_axes + 1
  3. Assert sorted(dims) == list(range(1, len(dims)+1)) in config-generation code before constructing the layer

Example fix

# before
layer = keras.layers.Permute(dims=[2, 1, 0])  # 0-based — ValueError

# after
layer = keras.layers.Permute(dims=[3, 2, 1])  # 1-based, excludes batch axis
Defensive patterns

Strategy: type-guard

Validate before calling

def valid_permute_dims(dims):
    return sorted(dims) == list(range(1, len(dims) + 1))

assert valid_permute_dims([3, 1, 2]), 'bad Permute dims'

Type guard

def is_permute_dims(dims) -> bool:
    d = tuple(dims)
    return len(d) > 0 and sorted(d) == list(range(1, len(d) + 1))

Prevention

When it happens

Trigger: Permute(dims=[0,1,2]) (contains 0), Permute(dims=[1,2,4]) (gap), Permute(dims=[1,1,2]) (duplicate), or 0-based indexing like Permute(dims=[2,3,4]) for a 4D tensor. All raise at construction time.

Common situations: Coming from NumPy/PyTorch where axes are 0-based — writing [2,1,0] instead of [3,2,1]; forgetting that Keras Permute ignores the batch axis (use 1..N, not 0..N-1); generating dims programmatically and emitting out-of-range indices.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/030aaeaee15b7aba. Report an issue: GitHub.