TFGENZOO.layers.spectral_normalization module

class TFGENZOO.layers.spectral_normalization.SpectralNormalization(layer: <module 'tensorflow_core.keras.layers' from '/home/meguru/.pyenv/versions/3.7.7/lib/python3.7/site-packages/tensorflow_core/python/keras/api/_v2/keras/layers/__init__.py'>, power_iterations: int = 1, **kwargs)[source]

Bases: tensorflow.python.keras.layers.wrappers.Wrapper

This wrapper controls the Lipschitz constant of the layer by constraining its spectral norm.

Note

This stabilizes the training of GANs. Spectral Normalization for Generative Adversarial Networks: https://arxiv.org/abs/1802.05957 Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida (2018) SpectralNormalization wrapper works for keras and tf layers.

Examples

>>> net = SpectralNormalization(
>>>   tf.keras.layers.Conv2D(2, 2, activation="relu"),
>>>     input_shape=(32, 32, 3))(x)
>>> net = SpectralNormalization(
>>>   tf.keras.layers.Conv2D(16, 5, activation="relu"))(net)
>>> net = SpectralNormalization(
>>>   tf.keras.layers.Dense(120, activation="relu"))(net)
>>> net = SpectralNormalization(
>>>   tf.keras.layers.Dense(n_classes))(net)
Parameters

layer (tf.keras.layersLayer) – a layer instance.

Returns

Wrapped Layer

Return type

tf.keras.layers.Layer

Raises
  • AssertionError – If not initialized with a Layer instance.

  • ValueError – If initialized with negative power_iterations

  • AttributeError – If Layer does not contain a kernel or embeddings of weights

build(input_shape)[source]

Build Layer

call(inputs, training=None)[source]

Call Layer

compute_output_shape(input_shape)[source]

Computes the output shape of the layer.

If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.

Parameters

input_shape – Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.

Returns

An input shape tuple.

get_config()[source]

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Returns

Python dictionary.

normalize_weights()[source]

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