{"record":{"id":"030aaeaee15b7aba","repo":"keras-team/keras","slug":"invalid-permutation-argument-dims-for-permute-la","errorCode":null,"errorMessage":"Invalid permutation argument `dims` for Permute Layer. The set of indices in `dims` must be consecutive and start from 1. Received dims={dims}","messagePattern":"Invalid permutation argument `dims` for Permute Layer\\. The set of indices in `dims` must be consecutive and start from 1\\. Received dims=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/reshaping/permute.py","lineNumber":39,"sourceCode":"        Arbitrary.\n\n    Output shape:\n        Same as the input shape, but with the dimensions re-ordered according\n        to the specified pattern.\n\n    Example:\n\n    >>> x = keras.Input(shape=(10, 64))\n    >>> y = keras.layers.Permute((2, 1))(x)\n    >>> y.shape\n    (None, 64, 10)\n    \"\"\"\n\n    def __init__(self, dims, **kwargs):\n        super().__init__(**kwargs)\n        self.dims = tuple(dims)\n        if sorted(dims) != list(range(1, len(dims) + 1)):\n            raise ValueError(\n                \"Invalid permutation argument `dims` for Permute Layer. \"\n                \"The set of indices in `dims` must be consecutive and start \"\n                f\"from 1. Received dims={dims}\"\n            )\n        self.input_spec = InputSpec(ndim=len(self.dims) + 1)\n\n    def compute_output_shape(self, input_shape):\n        output_shape = [input_shape[0]]\n        for dim in self.dims:\n            output_shape.append(input_shape[dim])\n        return tuple(output_shape)\n\n    def compute_output_spec(self, inputs):\n        output_shape = self.compute_output_shape(inputs.shape)\n        return KerasTensor(\n            shape=output_shape, dtype=inputs.dtype, sparse=inputs.sparse\n        )\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/reshaping/permute.py#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rewrite dims as a shuffle of 1..N where N is the tensor rank minus the batch axis","If porting 0-based NumPy axes, add 1 to every entry: np_axes + 1","Assert sorted(dims) == list(range(1, len(dims)+1)) in config-generation code before constructing the layer"],"exampleFix":"# before\nlayer = keras.layers.Permute(dims=[2, 1, 0])  # 0-based — ValueError\n\n# after\nlayer = keras.layers.Permute(dims=[3, 2, 1])  # 1-based, excludes batch axis","handlingStrategy":"type-guard","validationCode":"def valid_permute_dims(dims):\n    return sorted(dims) == list(range(1, len(dims) + 1))\n\nassert valid_permute_dims([3, 1, 2]), 'bad Permute dims'","typeGuard":"def is_permute_dims(dims) -> bool:\n    d = tuple(dims)\n    return len(d) > 0 and sorted(d) == list(range(1, len(d) + 1))","tryCatchPattern":null,"preventionTips":["Remember Keras Permute is 1-based and excludes the batch axis","When porting NumPy/PyTorch axis orders, add 1 to every axis index","Add a unit test asserting sorted(dims) == list(range(1, len(dims)+1)) for generated dims"],"tags":["keras","permute","argument-validation","reshaping-layer"],"backgroundTag":"invalid-argument-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}