gradgraph.optimization.tf.constraints module

class InBetween(lower_bound=None, upper_bound=None)[source]

Bases: Constraint

Constrains the weights to be within a specified range.

This constraint clips the weights to lie within the specified lower and upper bounds.

Parameters:
  • lower_bound (float or None, optional) – The lower bound for the weights. If None, defaults to the smallest positive representable number for the current floating-point type (tf.keras.backend.epsilon()).

  • upper_bound (float or None, optional) – The upper bound for the weights. If None, defaults to the largest representable number for the current floating-point type.

__call__(w)[source]

Clips the weights w to be within the specified bounds.

get_config()[source]

Returns the configuration of the constraint as a dictionary.

from_config(config)

Instantiates a constraint from a configuration dictionary.

Examples

>>> constraint = InBetween(lower_bound=0.0, upper_bound=1.0)
>>> model.add(Dense(64, kernel_constraint=constraint))

Notes

This constraint is useful when you want to ensure that the weights of a layer remain within a certain range during training.

classmethod from_config(config)

Instantiates a weight constraint from a configuration dictionary.

Example:

`python constraint = UnitNorm() config = constraint.get_config() constraint = UnitNorm.from_config(config) `

Args:

config: A Python dictionary, the output of get_config().

Returns:

A keras.constraints.Constraint instance.

get_config()[source]

Get the configuration of the bounds.

Returns:

A dictionary containing the configuration of the bounds with the following keys: - ‘lower_bound’: The lower bound value. - ‘upper_bound’: The upper bound value.

Return type:

dict