energy_fault_detector.core.autoencoder

class Autoencoder(learning_rate, batch_size, epochs, loss_name, metrics, decay_rate, decay_steps, early_stopping, patience, min_delta, noise, conditional_features=None, **kwargs)

Bases: ABC, SaveLoadMixin

Autoencoder template. Compatible with sklearn and keras/tensorflow.

Parameters:
  • learning_rate (float) – learning rate of the adam optimizer.

  • batch_size (int) – number of samples per batch.

  • epochs (int) – number of epochs to run.

  • loss_name (str) – name of loss metric to use.

  • metrics (List[str]) – list of additional metrics to track.

  • decay_rate (float) – learning rate decay. Optional. If not defined, a fixed learning rate is used.

  • decay_steps (float) – number of steps to decay learning rate over. Optional.

  • early_stopping (bool) – If True the early stopping callback will be used in the fit method. Early stopping will interrupt the training procedure before the last epoch is reached if the loss is not improving. The exact time of the interruption is based on the patience parameter.

  • patience (int) – parameter for early stopping. If early stopping is used the training will end if more than patience epochs in a row have not shown an improved loss.

  • min_delta (float) – parameter of the early stopping callback. If the losses of an epoch and the next epoch differ by less than min_delta, they are considered equal (i.e. no improvement).

  • noise (float) – float value that determines the influence of the noise term on the training input. High values mean highly noisy input. 0 means no noise at all. If noise >0 is used validation metrics will not be affected by it. Thus training loss and validation loss can differ depending on the magnitude of noise.

  • conditional_features (Optional[List[str]]) – (optional) List of features to use as conditions for the conditional autoencoder.

is_conditional

Indicates whether the autoencoder is a conditional autoencoder or not.

model

Keras Model created by the self.create_model() method.

encoder

Keras Model created by the self.create_model() method.

history

Dictionary with the loss, val_loss and metrics, if the model was fitted.

callbacks

List of callbacks applied every epoch when fitting the model. If early stopping is enabled, contains at least the keras.callbacks.early_stopping callback.

__call__(x, conditions=None)

Calls the model on new inputs.

Return type:

Tensor

compile_model(new_learning_rate=None, **kwargs)

Compile (or recompile) model with Adam optimizer, optionally with a different learning rate.

abstractmethod create_model(input_dimension, **kwargs)

Create a keras model, sets the model and (optionally) encoder attributes.

Parameters:
  • input_dimension (Union[int, Tuple]) – number of features in input data.

  • condition_dimension – number of features in conditional data. Only used if self.is_conditional = True.

Return type:

Model

Returns:

A Keras model.

encode(x, conditions=None)

Return latent representation using autoencoder.

Return type:

ndarray

fit(x, x_val=None, **kwargs)

Fit the autoencoder model.

Parameters:
  • x (Union[ndarray, DataFrame]) – training data

  • x_val (Union[ndarray, DataFrame]) – validation data

Return type:

Autoencoder

Returns:

Fitted model.

get_reconstruction_error(x, **kwargs)

Get the reconstruction error: output - input.

Parameters:
  • x (Union[ndarray, DataFrame]) – input data

  • kwargs – other keyword args for the keras Model.predict method.

Return type:

Union[ndarray, DataFrame]

Returns:

AE reconstruction error of the input data.

load(directory, **kwargs)

Load the model object from given directory.

Return type:

Autoencoder

predict(x, **kwargs)

Predict values using fitted model.

Parameters:

x (Union[ndarray, DataFrame]) – input data

Return type:

Union[ndarray, DataFrame]

save(directory, overwrite=False, **kwargs)

Save the model object in given directory, filename is the class name.

Parameters:
  • directory (str) – directory to save the object in.

  • overwrite (bool) – whether to overwrite existing data, default False.

tune(x, learning_rate, tune_epochs, x_val=None, **kwargs)

Tune full autoencoder by extending the model fitting process by tune_epochs.

Parameters:
  • x (Union[ndarray, DataFrame]) – training data

  • x_val (Union[ndarray, DataFrame]) – validation data

  • learning_rate (float) – learning rate to use during tuning.

  • tune_epochs (int) – number of epochs to tune.

  • kwargs – other keyword args for the keras Model.fit method.

Return type:

Autoencoder

Returns:

Tuned model.

tune_decoder(x, learning_rate, tune_epochs, x_val=None, **kwargs)

Tune decoder only - weights of the encoder are unchanged. Weight tuning is done by extending the model fitting process by tune_epochs.

Parameters:
  • x (DataFrame) – Training data

  • x_val (DataFrame) – Validation data

  • learning_rate (float) – Learning rate to use during tuning. Default original learning rate.

  • tune_epochs (int) – Number of epochs to tune.

  • kwargs – Other keyword args for the keras Model.fit method.

Return type:

Autoencoder

Returns:

Tuned model.

split_inputs(conditional_features, x, x_val=None)

Prepare the input and conditional data.

Parameters:
  • conditional_features (List[str]) – List of features names that are used as condition.

  • x (pd.DataFrame) – Data to split.

  • x_val (pd.DataFrame, optional) – Validation data to split. Defaults to None.

Returns:

Tuple of input and conditional data.

input_data, conditions, val_input_data, val_conditions

Return type:

Tuple