keras_utils module

class keras_utils.LoggingCallback(print_fcn=<built-in function print>)[source]

Bases: keras.callbacks.Callback

on_epoch_end(epoch, logs={})[source]

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters
  • epoch – Integer, index of epoch.

  • logs

    Dict, metric results for this training epoch, and for the

    validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the

    Model’s metrics are returned. Example`{‘loss’: 0.2, ‘accuracy’:

    0.7}`.

class keras_utils.PermanentDropout(rate, **kwargs)[source]

Bases: keras.layers.core.dropout.Dropout

call(x, mask=None)[source]

This is where the layer’s logic lives.

Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.

Parameters
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.

    • NumPy array or Python scalar values in inputs get cast as tensors.

    • Keras mask metadata is only collected from inputs.

    • Layers are built (build(input_shape) method) using shape info from inputs only.

    • input_spec compatibility is only checked against inputs.

    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

    • The SavedModel input specification is generated using inputs only.

    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.

  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.

    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

Returns

A tensor or list/tuple of tensors.

keras_utils.build_initializer(type, kerasDefaults, seed=None, constant=0.0)[source]

Set the initializer to the appropriate Keras initializer function based on the input string and learning rate. Other required values are set to the Keras default values

Parameters
  • type (string) –

    String to choose the initializer

    Options recognized: ‘constant’, ‘uniform’, ‘normal’, ‘glorot_uniform’, ‘lecun_uniform’, ‘he_normal’

    See the Keras documentation for a full description of the options

  • kerasDefaults (list) – List of default parameter values to ensure consistency between frameworks

  • seed (integer) – Random number seed

  • constant (float) – Constant value (for the constant initializer only)

Returns

The appropriate Keras initializer function

keras_utils.build_optimizer(type, lr, kerasDefaults)[source]

Set the optimizer to the appropriate Keras optimizer function based on the input string and learning rate. Other required values are set to the Keras default values

Parameters
  • type (string) –

    String to choose the optimizer

    Options recognized: ‘sgd’, ‘rmsprop’, ‘adagrad’, adadelta’, ‘adam’ See the Keras documentation for a full description of the options

  • lr (float) – Learning rate

  • kerasDefaults (list) – List of default parameter values to ensure consistency between frameworks

Returns

The appropriate Keras optimizer function

keras_utils.corr(y_true, y_pred)[source]
keras_utils.covariance(x, y)[source]
keras_utils.evaluate_autoencoder(y_pred, y_test)[source]
keras_utils.get_function(name)[source]
keras_utils.mae(y_true, y_pred)[source]
keras_utils.mse(y_true, y_pred)[source]
keras_utils.r2(y_true, y_pred)[source]
keras_utils.register_permanent_dropout()[source]
keras_utils.set_parallelism_threads()[source]

Set the number of parallel threads according to the number available on the hardware

keras_utils.set_seed(seed)[source]

Set the random number seed to the desired value

Parameters

seed (integer) – Random number seed.

keras_utils.xent(y_true, y_pred)[source]