deel.lip.model module

This module contains equivalents for Model and Sequential. These classes add support for condensation and vanilla exportation.

class deel.lip.model.Model(*args, **kwargs)

Bases: Model

Equivalent of keras.Model but support condensation and vanilla exportation.

Warning

As lipschitz constant are multiplicative along layer, the Model class cannot set a global Lipschitz constant (problem with branching inside a model).

condense()
vanilla_export() Model

Export this model to a “Vanilla” model, i.e. a model without Condensable layers.

Returns:

A Keras model, identical to this model, but where condensable layers have been replaced with their vanilla equivalent (e.g. SpectralConv2D with Conv2D).

class deel.lip.model.Sequential(*args, **kwargs)

Bases: Sequential, LipschitzLayer, Condensable

Equivalent of keras.Sequential but allow to set k-lip factor globally. Also support condensation and vanilla exportation. For now constant repartition is implemented (each layer get n_sqrt(k_lip_factor), where n is the number of layers) But in the future other repartition function may be implemented.

Parameters:
  • layers – list of layers to add to the model.

  • name – name of the model, can be None

  • k_coef_lip – the Lipschitz coefficient to ensure globally on the model.

build(input_shape=None)

Builds the model based on input shapes received.

This is to be used for subclassed models, which do not know at instantiation time what their inputs look like.

This method only exists for users who want to call model.build() in a standalone way (as a substitute for calling the model on real data to build it). It will never be called by the framework (and thus it will never throw unexpected errors in an unrelated workflow).

Parameters:

input_shape – Single tuple, TensorShape instance, or list/dict of shapes, where shapes are tuples, integers, or TensorShape instances.

Raises:
  • ValueError

    1. In case of invalid user-provided data (not of type tuple, list, TensorShape, or dict). 2. If the model requires call arguments that are agnostic to the input shapes (positional or keyword arg in call signature). 3. If not all layers were properly built. 4. If float type inputs are not supported within the layers.

  • In each of these cases, the user should build their model by calling

  • it on real tensor data.

condense()

The condense operation allows to overwrite the kernel and ensure that other variables are still consistent. :returns: None

get_config()

Returns the config of the Model.

Config is a Python dictionary (serializable) containing the configuration of an object, which in this case is a Model. This allows the Model to be be reinstantiated later (without its trained weights) from this configuration.

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Developers of subclassed Model are advised to override this method, and continue to update the dict from super(MyModel, self).get_config() to provide the proper configuration of this Model. The default config is an empty dict. Optionally, raise NotImplementedError to allow Keras to attempt a default serialization.

Returns:

Python dictionary containing the configuration of this Model.

set_klip_factor(klip_factor)

Allow to set the Lipschitz factor of a layer. :param klip_factor: the Lipschitz factor the user want to ensure.

Returns:

None

vanilla_export()

This operation allows to turn this Layer to its super type, easing storage and serving. :returns: self as super type

deel.lip.model.vanillaModel(model)

Transform a model to its equivalent “vanilla” model, i.e. a model where Condensable layers are replaced with their vanilla equivalent. For example, SpectralConv2D layers are converted to tf.keras Conv2D layers.

The input model can be a tf.keras Sequential/Model or a deel.lip Sequential/Model.

Parameters:

model – a tf.keras or deel.lip model with Condensable layers.

Returns:

A Keras model, identical to the input model where Condensable layers are replaced with their vanilla counterparts.