keras-team/keras · error · ValueError

object has no attribute 'kernel' nor 'embeddings

Error message

{type(self.layer).__name__} object has no attribute 'kernel' nor 'embeddings'

What it means

SpectralNormalization.build looked for a weight to normalize on the wrapped layer and found neither a 'kernel' nor an 'embeddings' attribute. The wrapper can only spectral-normalize layers that expose one of those standard weight attributes (Dense, Conv2D, Embedding, etc.); wrapper layers or layers with differently-named weights fail this hasattr probe at build time.

Solutions

  1. Wrap a layer that has a kernel or embeddings attribute, e.g. SpectralNormalization(keras.layers.Dense(10))
  2. For custom layers, expose the weight to normalize as an attribute named 'kernel'
  3. Normalize the relevant weight manually instead of using this wrapper
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/normalization/spectral_normalization.py:62 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/layers/normalization/spectral_normalization.py:62

    def __init__(self, layer, power_iterations=1, **kwargs):
        super().__init__(layer, **kwargs)
        if power_iterations <= 0:
            raise ValueError(
                "`power_iterations` should be greater than zero. Received: "
                f"`power_iterations={power_iterations}`"
            )
        self.power_iterations = power_iterations

    def build(self, input_shape):
        super().build(input_shape)
        self.input_spec = InputSpec(min_ndim=1, axes={-1: input_shape[-1]})

        if hasattr(self.layer, "kernel"):
            self.kernel = self.layer.kernel
        elif hasattr(self.layer, "embeddings"):
            self.kernel = self.layer.embeddings
        else:
            raise ValueError(
                f"{type(self.layer).__name__} object has no attribute 'kernel' "
                "nor 'embeddings'"
            )

        self.kernel_shape = self.kernel.shape

        self.vector_u = self.add_weight(
            shape=(1, self.kernel_shape[-1]),
            initializer=initializers.TruncatedNormal(stddev=0.02),
            trainable=False,
            name="vector_u",
            dtype=self.kernel.dtype,
        )

    def call(self, inputs, training=False):
        if training:
            new_vector_u, new_kernel = ops.cond(
                ops.all(ops.equal(self.kernel.value, 0)),

View on GitHub (pinned to 7a34a03db6)