<br />

<button value="stable" default="">See Stable</button> <button value="nightly">See Nightly</button>

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/normalization/spectral_normalization.py#L9-L121) |

Performs spectral normalization on the weights of a target layer.

Inherits From: [`Wrapper`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Wrapper.md.txt), [`Layer`](https://www.tensorflow.org/api_docs/python/tf/keras/Layer.md.txt), [`Operation`](https://www.tensorflow.org/api_docs/python/tf/keras/Operation.md.txt)

```python
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/api_docs/python/tf/keras/Layer.md.txt) 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. |
| `**kwargs` | Base wrapper keyword arguments. |

<br />

#### Examples:

Wrap [`keras.layers.Conv2D`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D.md.txt):

```python
>>> x = np.random.rand(1, 10, 10, 1)
>>> conv2d = SpectralNormalization(keras.layers.Conv2D(2, 2))
>>> y = conv2d(x)
>>> y.shape
(1, 9, 9, 2)
```

Wrap [`keras.layers.Dense`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense.md.txt):

```python
>>> x = np.random.rand(1, 10, 10, 1)
>>> dense = SpectralNormalization(keras.layers.Dense(10))
>>> y = dense(x)
>>> y.shape
(1, 10, 10, 10)
```

#### Reference:

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

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `input` | Retrieves the input tensor(s) of a symbolic operation. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. |
| `output` | Retrieves the output tensor(s) of a layer. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. |

<br />

## Methods

### `from_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/core/wrapper.py#L38-L44)

```python
@classmethod
from_config(
    config, custom_objects=None
)
```

Creates an operation from its config.

This method is the reverse of `get_config`, capable of instantiating the
same operation from the config dictionary.

> [!NOTE]
> **Note:** If you override this method, you might receive a serialized dtype config, which is a `dict`. You can deserialize it as follows:

```python
if "dtype" in config and isinstance(config["dtype"], dict):
    policy = dtype_policies.deserialize(config["dtype"])
```

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | A Python dictionary, typically the output of `get_config`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An operation instance. ||

<br />

### `normalized_weights`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/normalization/spectral_normalization.py#L93-L116)

```python
normalized_weights()
```

Generate spectral normalized weights.

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