tf.keras.layers.SpectralNormalization
Stay organized with collections
Save and categorize content based on your preferences.
Performs spectral normalization on the weights of a target layer.
Inherits From: Wrapper
, Layer
, Operation
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.
Args |
layer
|
A 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.
|
**kwargs
|
Base wrapper keyword arguments.
|
Examples:
Wrap keras.layers.Conv2D
:
>>> 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
:
>>> x = np.random.rand(1, 10, 10, 1)
>>> dense = SpectralNormalization(keras.layers.Dense(10))
>>> y = dense(x)
>>> y.shape
(1, 10, 10, 10)
Reference:
Attributes |
input
|
Retrieves the input tensor(s) of a symbolic operation.
Only returns the tensor(s) corresponding to the first time
the operation was called.
|
output
|
Retrieves the output tensor(s) of a layer.
Only returns the tensor(s) corresponding to the first time
the operation was called.
|
Methods
from_config
View source
@classmethod
from_config(
config, custom_objects=None
)
Creates a layer from its config.
This method is the reverse of get_config
,
capable of instantiating the same layer from the config
dictionary. It does not handle layer connectivity
(handled by Network), nor weights (handled by set_weights
).
Args |
config
|
A Python dictionary, typically the
output of get_config.
|
Returns |
A layer instance.
|
normalized_weights
View source
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()
.
symbolic_call
View source
symbolic_call(
*args, **kwargs
)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.layers.SpectralNormalization\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/layers/normalization/spectral_normalization.py#L9-L121) |\n\nPerforms spectral normalization on the weights of a target layer.\n\nInherits From: [`Wrapper`](../../../tf/keras/layers/Wrapper), [`Layer`](../../../tf/keras/Layer), [`Operation`](../../../tf/keras/Operation) \n\n tf.keras.layers.SpectralNormalization(\n layer, power_iterations=1, **kwargs\n )\n\nThis wrapper controls the Lipschitz constant of the weights of a layer by\nconstraining their spectral norm, which can stabilize the training of GANs.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `layer` | A [`keras.layers.Layer`](../../../tf/keras/Layer) instance that has either a `kernel` (e.g. `Conv2D`, `Dense`...) or an `embeddings` attribute (`Embedding` layer). |\n| `power_iterations` | int, the number of iterations during normalization. |\n| `**kwargs` | Base wrapper keyword arguments. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\nWrap [`keras.layers.Conv2D`](../../../tf/keras/layers/Conv2D): \n\n \u003e\u003e\u003e x = np.random.rand(1, 10, 10, 1)\n \u003e\u003e\u003e conv2d = SpectralNormalization(keras.layers.Conv2D(2, 2))\n \u003e\u003e\u003e y = conv2d(x)\n \u003e\u003e\u003e y.shape\n (1, 9, 9, 2)\n\nWrap [`keras.layers.Dense`](../../../tf/keras/layers/Dense): \n\n \u003e\u003e\u003e x = np.random.rand(1, 10, 10, 1)\n \u003e\u003e\u003e dense = SpectralNormalization(keras.layers.Dense(10))\n \u003e\u003e\u003e y = dense(x)\n \u003e\u003e\u003e y.shape\n (1, 10, 10, 10)\n\n#### Reference:\n\n- [Spectral Normalization for GAN](https://arxiv.org/abs/1802.05957).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|----------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | Retrieves the input tensor(s) of a symbolic operation. \u003cbr /\u003e Only returns the tensor(s) corresponding to the *first time* the operation was called. |\n| `output` | Retrieves the output tensor(s) of a layer. \u003cbr /\u003e Only returns the tensor(s) corresponding to the *first time* the operation was called. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/layers/core/wrapper.py#L41-L47) \n\n @classmethod\n from_config(\n config, custom_objects=None\n )\n\nCreates a layer from its config.\n\nThis method is the reverse of `get_config`,\ncapable of instantiating the same layer from the config\ndictionary. It does not handle layer connectivity\n(handled by Network), nor weights (handled by `set_weights`).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|----------------------------------------------------------|\n| `config` | A Python dictionary, typically the output of get_config. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A layer instance. ||\n\n\u003cbr /\u003e\n\n### `normalized_weights`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/layers/normalization/spectral_normalization.py#L93-L116) \n\n normalized_weights()\n\nGenerate spectral normalized weights.\n\nThis method returns the updated value for `self.kernel` with the\nspectral normalized value, so that the layer is ready for `call()`.\n\n### `symbolic_call`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/ops/operation.py#L58-L70) \n\n symbolic_call(\n *args, **kwargs\n )"]]