# tf.keras.layers.SpectralNormalization

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/normalization/spectral_normalization.py#L26-L141) |

Performs spectral normalization on the weights of a target layer.

Inherits From: [`Wrapper`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Wrapper), [`Layer`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Layer), [`Module`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Module)

    tf.keras.layers.SpectralNormalization(
        layer, power_iterations=1, **kwargs
    )

This wrapper controls the Lipschitz constant of the weights of a layer by
constraining their spectral norm, which can stabilize the training of GANs.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `layer` | A [`keras.layers.Layer`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Layer) instance that has either a `kernel` (e.g. `Conv2D`, `Dense`...) or an `embeddings` attribute (`Embedding` layer). |
| `power_iterations` | int, the number of iterations during normalization. |

<br />

#### Examples:

Wrap [`keras.layers.Conv2D`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Conv2D):

    >>> x = np.random.rand(1, 10, 10, 1)
    >>> conv2d = SpectralNormalization(tf.keras.layers.Conv2D(2, 2))
    >>> y = conv2d(x)
    >>> y.shape
    TensorShape([1, 9, 9, 2])

Wrap [`keras.layers.Dense`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Dense):

    >>> x = np.random.rand(1, 10, 10, 1)
    >>> dense = SpectralNormalization(tf.keras.layers.Dense(10))
    >>> y = dense(x)
    >>> y.shape
    TensorShape([1, 10, 10, 10])

#### Reference:

- [Spectral Normalization for GAN](https://arxiv.org/abs/1802.05957).

## Methods

### `normalize_weights`

[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/normalization/spectral_normalization.py#L108-L136)

    normalize_weights()

Generate spectral normalized weights.

This method will update the value of `self.kernel` with the
spectral normalized value, so that the layer is ready for `call()`.