{"record":{"id":"f0bce2c499efaab1","repo":"lllyasviel/style2paints","slug":"cannot-specify-axis-for-rank-1-tensor","errorCode":null,"errorMessage":"Cannot specify axis for rank 1 tensor","messagePattern":"Cannot specify axis for rank 1 tensor","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"V4/s2p_v4_server/InstanceNorm.py","lineNumber":77,"sourceCode":"        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:\n            self.gamma = None\n        if self.center:\n            self.beta = self.add_weight(shape=shape,","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/lllyasviel/style2paints/blob/a0d164d6a8a69fa4a87139bcd193291057f4ca39/V4/s2p_v4_server/InstanceNorm.py#L59-L95","documentation":"InstanceNorm.build() rejects an explicitly specified axis when the incoming tensor has ndim == 2 (rank 1 per-sample input, e.g. shape (batch, features)). For such low-rank inputs there is no meaningful per-instance spatial/channel axis to normalize over, so specifying one is treated as a configuration error. The check exists because the layer's axis semantics only apply to inputs with spatial dimensions.","triggerScenarios":"Feeding a 2D tensor (batch_size, features) — e.g. a Dense output or flattened input — into InstanceNorm while axis is not None, such as InstanceNorm(axis=1) applied directly to a dense/fully-connected output.","commonSituations":"Chaining InstanceNorm after a Dense layer in an MLP; flattening a conv feature map before normalization; reusing an InstanceNorm layer config that worked on 4D conv tensors on a 2D tensor.","solutions":["Remove the axis argument (use InstanceNorm() with axis=None) for rank-1/2D inputs","Keep the input 4D (N,H,W,C) if per-channel spatial normalization was intended — insert the norm before flattening","Replace InstanceNorm with LayerNormalization or BatchNormalization for dense/2D inputs"],"exampleFix":"# before\nx = Dense(256)(x)\nx = InstanceNorm(axis=1)(x)\n\n# after\nx = Dense(256)(x)\nx = LayerNormalization(axis=-1)(x)  # or use InstanceNorm() without axis on 4D input","handlingStrategy":"validation","validationCode":"def can_apply_instance_norm(x, axis):\n    ndim = len(x.shape)\n    if axis is not None and ndim == 2:\n        return False\n    return axis != 0\n\nassert can_apply_instance_norm(x, axis)","typeGuard":"def supports_axis(x, axis):\n    return axis is None or len(x.shape) > 2","tryCatchPattern":"try:\n    out = InstanceNorm(axis=axis)(x)\nexcept ValueError as e:\n    if 'rank 1 tensor' in str(e):\n        out = LayerNormalization(axis=-1)(x)\n    else:\n        raise","preventionTips":["Only specify axis for 4D+ (spatial) inputs","Omit axis for 2D inputs, or switch to LayerNormalization/BatchNormalization","Check tensor rank with len(x.shape) before composing the layer"],"tags":["keras","layer-config","valueerror","rank-mismatch"],"backgroundTag":"invalid-layer-axis","analyzedSha":"a0d164d6a8a69fa4a87139bcd193291057f4ca39","analyzedAt":"2026-09-02T22:11:10.837Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}