sparseml.keras.utils package

Submodules

sparseml.keras.utils.callbacks module

Built-in callbacks for Keras

class sparseml.keras.utils.callbacks. LoggerSettingCallback ( loggers : Union [ sparseml.keras.utils.logger.KerasLogger , List [ sparseml.keras.utils.logger.KerasLogger ] ] ) [source]

Bases: tensorflow.python.keras.callbacks.Callback

Class to help correctly set logging modes for callbacks that rely on KerasLogger. All callbacks using KerasLogger should derive from this class.

Parameters

loggers – logger or list of loggers

on_epoch_begin ( epoch , logs = None ) [source]

Called at the begin of a training epoch

Parameters
  • epoch – epoch index

  • logs – dictionary of logs (see Keras Callback doc)

on_epoch_end ( epoch , logs = None ) [source]

Called at the end of a training epoch

Parameters
  • epoch – epoch index

  • logs – dictionary of logs (see Keras Callback doc)

on_predict_batch_begin ( batch , logs = None ) [source]

Called at the begin of a batch in prediction

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_predict_batch_end ( batch , logs = None ) [source]

Called at the end of a batch in prediction

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_predict_begin ( logs = None ) [source]

Called at the begin of prediction

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_predict_end ( logs = None ) [source]

Called at the end of prediction

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_test_batch_begin ( batch , logs = None ) [source]

Called at the begin of a batch in evaluation

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_test_batch_end ( batch , logs = None ) [source]

Called at the end of a batch in evaluation

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_test_begin ( logs = None ) [source]

Called at the begin of evaluation

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_test_end ( logs = None ) [source]

Called at the end of evaluation

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_train_batch_begin ( batch , logs = None ) [source]

Called at the begin of a batch in training

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_train_batch_end ( batch , logs = None ) [source]

Called at the end of a batch in training

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_train_begin ( logs = None ) [source]

Called at the begin of training

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_train_end ( logs = None ) [source]

Called at the end of training

Parameters

logs – dictionary of logs (see Keras Callback doc)

class sparseml.keras.utils.callbacks. LossesAndMetricsLoggingCallback ( loggers : Union [ sparseml.keras.utils.logger.KerasLogger , List [ sparseml.keras.utils.logger.KerasLogger ] ] , start_step : Union [ tensorflow.python.framework.ops.Tensor , int ] = 0 ) [source]

Bases: sparseml.keras.utils.callbacks.LoggerSettingCallback

Callback to log all losses and metrics

Parameters
  • loggers – logger or list of loggers

  • start_step – a start step tensor when this callback starts to take effect

on_epoch_end ( epoch , logs = None ) [source]

Called at the end of a training epoch

Parameters
  • epoch – epoch index

  • logs – dictionary of logs (see Keras Callback doc)

on_test_end ( logs = None ) [source]

Called at the end of evaluation

Parameters

logs – dictionary of logs (see Keras Callback doc)

on_train_batch_end ( batch , logs = None ) [source]

Called at the end of a batch in training

Parameters
  • batch – batch index in current epoch

  • logs – dictionary of logs (see Keras Callback doc)

on_train_begin ( logs = None ) [source]

Called at the begin of training

Parameters

logs – dictionary of logs (see Keras Callback doc)

sparseml.keras.utils.compat module

sparseml.keras.utils.compat. assign ( lhs , rhs , name = None ) [source]

sparseml.keras.utils.exporter module

Export Keras models to the local device.

class sparseml.keras.utils.exporter. ModelExporter ( model : tensorflow.python.keras.engine.training.Model , output_dir : str ) [source]

Bases: object

An exporter for exporting Keras models into ONNX format as well as numpy arrays for the input and output tensors.

Parameters
  • model – the module to export

  • output_dir – the directory to export the module and extras to

export_h5 ( name : str = 'model.h5' ) [source]

Export the Keras model as a single HDF5 file in the output_dir/keras directory

Parameters

name – name to export file with. default is model.h5

export_keras ( ) [source]

Export the model Keras files to the output_dir/keras directory

export_onnx ( name : str = 'model.onnx' , opset : int = 11 , doc_string : str = '' , debug_mode : bool = True , raise_on_tf_support : bool = True , ** kwargs ) [source]

Export an ONNX file for the current model.

Parameters
  • name – name of the onnx file to save

  • opset – onnx opset to use for exported model. Default is 11

  • doc_string – optional doc string for exported ONNX model

  • debug_mode – debug mode, default to True, passed into convert_keras

  • kwargs – additional parameters passed into convert_keras

export_samples ( sample_batches : List [ Any ] , sample_labels : Optional [ List [ Any ] ] = None , exp_counter : int = 0 ) [source]

Export a set list of sample batches as inputs and outputs through the model.

Parameters
  • sample_batches – a list of the sample batches to feed through the module for saving inputs and outputs

  • sample_labels – an optional list of sample labels that correspond to the the batches for saving

  • exp_counter – the counter to start exporting the tensor files at

sparseml.keras.utils.logger module

Contains code for loggers that help visualize the information from each modifier

class sparseml.keras.utils.logger. KerasLogger ( name : str , update_freq : Union [ str , int ] = 'epoch' , ** kwargs ) [source]

Bases: abc.ABC

Base class that all modifier loggers shall implement.

Parameters
  • name – name given to the logger, used for identification

  • update_freq – define when logging should happen - “epoch”: log at the end of each epoch - “batch”: log at the end of each training batch - an integer value: the number of batches before the next logging should be

abstract log_scalar ( tag : str , value : float , step : Optional [ int ] = None , ** kwargs ) [source]
Parameters
  • tag – identifying tag to log the value with

  • value – value to save

  • step – global step for when the value was taken

property mode

Mode the current logger is at a current time

Returns

a LoggingMode

property name

name given to the logger, used for identification

Type

return

property update_freq
class sparseml.keras.utils.logger. LoggingMode ( value ) [source]

Bases: enum.Enum

Some logger changes its logging behavior (e.g. the destination it logs to) based on whether it’s running in train, validation or predict mode

This enum defines the mode a logger could be in during its lifetime

PREDICT = 'predict'
TEST = 'validation'
TRAIN = 'train'
class sparseml.keras.utils.logger. PythonLogger ( name : str = 'python' , update_freq : Union [ str , int ] = 'epoch' , logger : Optional [ logging.Logger ] = None ) [source]

Bases: sparseml.keras.utils.logger.KerasLogger

Modifier logger that handles printing values into a python logger instance.

Parameters
  • name – name given to the logger, used for identification;

  • update_freq – update frequency (see attribute info in the base class)

  • logger – a logger instance to log to, if None then will create it’s own defaults to python

log_scalar ( tag : str , value : float , step : Optional [ int ] = None , ** kwargs ) [source]
Parameters
  • tag – identifying tag to log the value with

  • value – value to save

  • step – global step for when the value was taken

  • wall_time – global wall time for when the value was taken, defaults to time.time()

class sparseml.keras.utils.logger. TensorBoardLogger ( name : str = 'tensorboard' , update_freq : Union [ str , int ] = 'epoch' , log_dir : str = 'logs' ) [source]

Bases: sparseml.keras.utils.logger.KerasLogger

Modifier logger that handles outputting values into a TensorBoard log directory for viewing in TensorBoard.

Parameters
  • name – name given to the logger, used for identification; defaults to tensorboard

  • update_freq – update frequency (see attribute info in the base class)

  • log_dir – the path to create a SummaryWriter at. writer must be None to use if not supplied (and writer is None), will create a TensorBoard dir in cwd

log_scalar ( tag : str , value : float , step : Optional [ int ] = None , ** kwargs ) [source]
Parameters
  • tag – identifying tag to log the value with

  • value – value to save

  • step – global step for when the value was taken

sparseml.keras.utils.model module

Utils for Keras model

sparseml.keras.utils.model. sparsity ( model : tensorflow.python.keras.engine.training.Model ) [source]

Retrieve sparsity of a Keras model

Parameters

model – a Keras model

Returns

  1. model sparsity, (2) dictionary of layer sparsity

Module contents

Generic code used as utilities and helpers for Keras