deel.lip.utils module

Contains utility functions.

deel.lip.utils.evaluate_lip_const(model: Model, x, eps=0.0001, seed=None)

Evaluate the Lipschitz constant of a model, with the naive method. Please note that the estimation of the lipschitz constant is done locally around input sample. This may not correctly estimate the behaviour in the whole domain.

Parameters:
  • model – built keras model used to make predictions

  • x – inputs used to compute the lipschitz constant

  • eps – magnitude of noise to add to input in order to compute the constant

  • seed – seed used when generating the noise ( can be set to None )

Returns:

the empirically evaluated lipschitz constant. The computation might also be inaccurate in high dimensional space.

deel.lip.utils.evaluate_lip_const_gen(model: Model, generator: Generator[Tuple[ndarray, ndarray], Any, None], eps=0.0001, seed=None)

Evaluate the Lipschitz constant of a model, with the naive method. Please note that the estimation of the lipschitz constant is done locally around input sample. This may not correctly estimate the behaviour in the whole domain. The computation might also be inaccurate in high dimensional space.

This is the generator version of evaluate_lip_const.

Parameters:
  • model – built keras model used to make predictions

  • generator – used to select datapoints where to compute the lipschitz constant

  • eps – magnitude of noise to add to input in order to compute the constant

  • seed – seed used when generating the noise ( can be set to None )

Returns:

the empirically evaluated lipschitz constant.

deel.lip.utils.process_labels_for_multi_gpu(labels)

Process labels to be fed to any loss based on KR estimation with a multi-GPU/TPU strategy.

When using a multi-GPU/TPU strategy, the flag multi_gpu in KR-based losses must be set to True and the labels have to be pre-processed with this function.

For binary classification, the labels should be of shape [batch_size, 1]. For multiclass problems, the labels must be one-hot encoded (1 or 0) with shape [batch_size, number of classes].

Parameters:

labels – tf.Tensor containing the labels

Returns:

labels processed for KR-based losses with multi-GPU/TPU strategy.