{"record":{"id":"5e52d28ca5000bd5","repo":"lllyasviel/style2paints","slug":"axis-cannot-be-zero","errorCode":null,"errorMessage":"Axis cannot be zero","messagePattern":"Axis cannot be zero","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"V4/s2p_v4_server/InstanceNorm.py","lineNumber":74,"sourceCode":"                 gamma_constraint=None,\n                 **kwargs):\n        super(InstanceNormalization, self).__init__(**kwargs)\n        self.supports_masking = True\n        self.axis = axis\n        self.epsilon = epsilon\n        self.center = center\n        self.scale = scale\n        self.beta_initializer = initializers.get(beta_initializer)\n        self.gamma_initializer = initializers.get(gamma_initializer)\n        self.beta_regularizer = regularizers.get(beta_regularizer)\n        self.gamma_regularizer = regularizers.get(gamma_regularizer)\n        self.beta_constraint = constraints.get(beta_constraint)\n        self.gamma_constraint = constraints.get(gamma_constraint)\n\n    def build(self, input_shape):\n        ndim = len(input_shape)\n        if self.axis == 0:\n            raise ValueError('Axis cannot be zero')\n\n        if (self.axis is not None) and (ndim == 2):\n            raise ValueError('Cannot specify axis for rank 1 tensor')\n\n        self.input_spec = InputSpec(ndim=ndim)\n\n        if self.axis is None:\n            shape = (1,)\n        else:\n            shape = (input_shape[self.axis],)\n\n        if self.scale:\n            self.gamma = self.add_weight(shape=shape,\n                                         name='gamma',\n                                         initializer=self.gamma_initializer,\n                                         regularizer=self.gamma_regularizer,\n                                         constraint=self.gamma_constraint)\n        else:","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/lllyasviel/style2paints/blob/a0d164d6a8a69fa4a87139bcd193291057f4ca39/V4/s2p_v4_server/InstanceNorm.py#L56-L92","documentation":"InstanceNorm.build() validates the layer's axis configuration before constructing weights. Axis 0 is rejected because axis 0 in Keras is the batch/sample dimension; normalizing over it would mix statistics across samples in the batch, which is meaningless for instance normalization. The library throws eagerly at build time so the misconfiguration is caught before any weights are created.","triggerScenarios":"Calling the layer (or building a model containing it) with InstanceNorm(axis=0, ...), or relying on a default/derived axis value that resolves to 0 — e.g. passing axis as an index computed from a 0-based loop or config dict.","commonSituations":"Porting models from other frameworks where the channel axis convention differs (channels-first vs channels-last) and the axis index is off by one; hand-editing layer configs; programmatically generating axis values that start at 0.","solutions":["Set axis to the channel dimension (typically 1 for channels-first or -1/last for channels-last), never 0","If axis was passed programmatically, use 1-based or negative indexing so it cannot be 0","If axis=None is acceptable, omit the axis argument entirely so normalization applies to the whole input"],"exampleFix":"# before\nnorm = InstanceNorm(axis=0)\n\n# after\nnorm = InstanceNorm(axis=1)  # or axis=-1 for channels-last","handlingStrategy":"validation","validationCode":"def check_axis(axis, ndim):\n    if axis == 0:\n        raise ValueError('InstanceNorm axis cannot be 0 (batch dimension)')\n    if axis is not None and ndim == 2:\n        raise ValueError('axis must be None for rank-1/2D inputs')\n\ncheck_axis(axis, len(input_shape))","typeGuard":"def is_valid_norm_input(x, axis):\n    return axis != 0 and (axis is None or len(x.shape) != 2)","tryCatchPattern":null,"preventionTips":["Never pass axis=0; reserve axis 0 for batch in Keras","Use axis=-1 for channels-last or axis=1 for channels-first conventions","For 2D/dense inputs use LayerNormalization instead of InstanceNorm with an axis"],"tags":["keras","layer-config","valueerror","instance-norm"],"backgroundTag":"invalid-layer-axis","analyzedSha":"a0d164d6a8a69fa4a87139bcd193291057f4ca39","analyzedAt":"2026-09-02T22:11:10.837Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}