lllyasviel/style2paints · error · ValueError

Cannot specify axis for rank 1 tensor

Error message

Cannot specify axis for rank 1 tensor

What it means

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.

Source

Thrown at V4/s2p_v4_server/InstanceNorm.py:77

        self.supports_masking = True
        self.axis = axis
        self.epsilon = epsilon
        self.center = center
        self.scale = scale
        self.beta_initializer = initializers.get(beta_initializer)
        self.gamma_initializer = initializers.get(gamma_initializer)
        self.beta_regularizer = regularizers.get(beta_regularizer)
        self.gamma_regularizer = regularizers.get(gamma_regularizer)
        self.beta_constraint = constraints.get(beta_constraint)
        self.gamma_constraint = constraints.get(gamma_constraint)

    def build(self, input_shape):
        ndim = len(input_shape)
        if self.axis == 0:
            raise ValueError('Axis cannot be zero')

        if (self.axis is not None) and (ndim == 2):
            raise ValueError('Cannot specify axis for rank 1 tensor')

        self.input_spec = InputSpec(ndim=ndim)

        if self.axis is None:
            shape = (1,)
        else:
            shape = (input_shape[self.axis],)

        if self.scale:
            self.gamma = self.add_weight(shape=shape,
                                         name='gamma',
                                         initializer=self.gamma_initializer,
                                         regularizer=self.gamma_regularizer,
                                         constraint=self.gamma_constraint)
        else:
            self.gamma = None
        if self.center:
            self.beta = self.add_weight(shape=shape,

View on GitHub (pinned to a0d164d6a8)

Solutions

  1. Remove the axis argument (use InstanceNorm() with axis=None) for rank-1/2D inputs
  2. Keep the input 4D (N,H,W,C) if per-channel spatial normalization was intended — insert the norm before flattening
  3. Replace InstanceNorm with LayerNormalization or BatchNormalization for dense/2D inputs

Example fix

# before
x = Dense(256)(x)
x = InstanceNorm(axis=1)(x)

# after
x = Dense(256)(x)
x = LayerNormalization(axis=-1)(x)  # or use InstanceNorm() without axis on 4D input
Defensive patterns

Strategy: validation

Validate before calling

def can_apply_instance_norm(x, axis):
    ndim = len(x.shape)
    if axis is not None and ndim == 2:
        return False
    return axis != 0

assert can_apply_instance_norm(x, axis)

Type guard

def supports_axis(x, axis):
    return axis is None or len(x.shape) > 2

Try / catch

try:
    out = InstanceNorm(axis=axis)(x)
except ValueError as e:
    if 'rank 1 tensor' in str(e):
        out = LayerNormalization(axis=-1)(x)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of lllyasviel/style2paints@a0d164d6a8 (2026-09-02). Data as JSON: /api/errors/f0bce2c499efaab1. Report an issue: GitHub.