energy_fault_detector.core.autoencoder
- class Autoencoder(*, learning_rate=0.001, batch_size=128, epochs=10, loss_name='mean_squared_error', metrics=None, decay_rate=None, decay_steps=None, early_stopping=False, patience=5, min_delta=0.0001, noise=0.0, conditional_features=None, verbose=1, **kwargs)[source]
Bases:
ABC,SaveLoadMixinAutoencoder template. Compatible with sklearn and keras/tensorflow.
- Parameters:
learning_rate (
float) – learning rate of the adam optimizer. Default: 0.001.batch_size (
int) – number of samples per batch. Default: 128.epochs (
int) – number of epochs to run. Default: 10.loss_name (
str) – name of loss metric to use. Default: ‘mean_squared_error’.metrics (
List[str]) – list of additional metrics to track. Default: [‘mean_absolute_error’].decay_rate (
float) – learning rate decay. Optional. If not defined, a fixed learning rate is used. Default: None.decay_steps (
float) – number of steps to decay learning rate over. Optional. Default: None. If not defined, a fixed learning rate is used.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. Default: False.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. Default: 5.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). Default: 1e-4.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. Default: 0.0 (no noise).conditional_features (
Optional[List[str]]) – (optional) List of features to use as conditions for the conditional autoencoder. Default: None (no conditional features).verbose (
int) – verbosity mode - passed to KerasModel fit and predict methods. Default: 1.
- 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.
- compile_model(new_learning_rate=None, **kwargs)[source]
Compile (or recompile) model with Adam optimizer, optionally with a different learning rate.
- abstractmethod create_model(input_dimension, condition_dimension=None, **kwargs)[source]
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 (
Optional[int]) – number of features in conditional data. Only used if self.is_conditional = True.
- Return type:
Model- Returns:
A Keras model.
- encode(x, conditions=None, **kwargs)[source]
Return latent representation using autoencoder.
- Return type:
ndarray
- property epochs_completed: int
- fit(x, x_val=None, **kwargs)[source]
Fit the autoencoder model.
- Parameters:
x (
Union[ndarray,DataFrame]) – training datax_val (
Union[ndarray,DataFrame]) – validation data
- Return type:
- Returns:
Fitted model.
- get_reconstruction_error(x, reconstruction=None, **kwargs)[source]
Get the reconstruction error: output - input.
- Parameters:
x (
Union[ndarray,DataFrame]) – input datareconstruction (
Union[ndarray,DataFrame]) – pre-computed reconstruction. If None, predict() is called internally.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)[source]
Load the model object from given directory. Tries to load .keras files first, falls back to .model.
- Return type:
- predict(x, **kwargs)[source]
Predict values using fitted model.
- Parameters:
x (
Union[ndarray,DataFrame]) – input data- Return type:
Union[ndarray,DataFrame]
- save(directory, overwrite=False, **kwargs)[source]
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.
- summary(**kwargs)[source]
Print a Keras summary of the underlying model, if built.
- Return type:
None
- tune(x, learning_rate, tune_epochs, x_val=None, **kwargs)[source]
Tune full autoencoder by extending the model fitting process by tune_epochs.
- Parameters:
x (
Union[ndarray,DataFrame]) – training datax_val (
Union[ndarray,DataFrame]) – validation datalearning_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:
- Returns:
Tuned model.
- tune_decoder(x, learning_rate, tune_epochs, x_val=None, **kwargs)[source]
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 datax_val (
DataFrame) – Validation datalearning_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:
- Returns:
Tuned model.
- split_inputs(conditional_features, x, x_val=None)[source]
Prepare the input and conditional data.
- Parameters:
conditional_features (
List[str]) – List of features names that are used as condition.x (
DataFrame) – Data to split.x_val (
DataFrame) – 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