tf.keras.layers.SpectralNormalization  |  TensorFlow v2.16.1 (original) (raw)

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
)