encodermap.callbacks package#
Submodules#
encodermap.callbacks.callbacks module#
Callbacks to strew into the Autoencoder classes.
- class CheckpointSaver(parameters=None)[source]#
Bases:
EncoderMapBaseCallback
Callback that saves an encodermap.models model.
- Parameters:
parameters (Optional['AnyParameters'])
- class EarlyStop(patience=0)[source]#
Bases:
Callback
Stop training when the loss is at its min, i.e. the loss stops decreasing.
- Parameters:
patience (int) – Number of epochs to wait after min has been hit. After this number of no improvement, training stops.
- on_train_batch_end(batch, logs=None)[source]#
Gets the current loss at the end of the batch compares it to previous batches.
- class EncoderMapBaseCallback(parameters=None)[source]#
Bases:
Callback
Base class for callbacks in EncoderMap.
The Parameters class in EncoderMap has a summary_step variable that dictates when variables and other tensors are logged to TensorBoard. No matter what property is logged there will always be a code section executing a if train_step % summary_step == 0 code snippet. This is handled centrally in this class. This class is instantiated inside the user-facing AutoEncoderClass classes and is provided with the appropriate parameters (Parameters for EncoderMap and ADCParameters for AngleDihedralCartesianEncoderMap). Thus, subclassing this class does not need to implement a new __init__ method. Only the on_summary_step and the on_checkpoint_step methods need to be implemented for sub-classes if this class with code that should happen when these events happen.
Examples:
In this example, the on_summary_step method causes an exception.
>>> from typing import Optional >>> import encodermap as em ... >>> class MyCallback(em.callbacks.EncoderMapBaseCallback): ... def on_summary_step(self, step: int, logs: Optional[dict] = None) -> None: ... raise Exception(f"Summary step {self.steps_counter} has been reached.") ... >>> emap = em.EncoderMap() Output... >>> emap.add_callback(MyCallback) >>> emap.train() Traceback (most recent call last): ... Exception: Summary step 10 has been reached.
- Parameters:
parameters (Optional['AnyParameters'])
- p (Union[encodermap.parameters.Parameters, encodermap.parameters.ADCParameters]
The parameters for this callback. Based on the summary_step and checkpoint_step of the encodermap.parameters.Parameters class different class-methods are called.
- on_checkpoint_step(step, logs=None)[source]#
Executed, when the currently finished batch matches encodermap.Parameters.checkpoint_step
- on_summary_step(step, logs=None)[source]#
Executed, when the currently finished batch matches encodermap.Parameters.summary_step
- on_train_batch_end(batch, logs=None)[source]#
Called after a batch ends. The number of batch is provided by keras.
This method is the backbone of all of EncoderMap’s callbacks. After every batch is method is called by keras. When the number of that batch matches either encodermap.Parameters.summary_step or encodermap.Parameters.checkpoint_step the code on self.on_summary_step, or self.on_checkpoint_step is executed. These methods should be overwritten by child classes.
- class ImageCallback(parameters, highd_data, image_step, backend='matplotlib', mpl_scatter_kws=None, mpl_hist_kws=None, plotly_scatter_kws=None, plotly_hist_kws=None, additional_fns=None, when='batch', save_dir=None)[source]#
Bases:
Callback
Writes images to tensorboard.
- Parameters:
parameters (AnyParameters)
highd_data (np.ndarray)
image_step (int)
backend (Literal['matplotlib', 'plotly'])
mpl_scatter_kws (Optional[dict])
mpl_hist_kws (Optional[dict])
plotly_scatter_kws (Optional[dict])
plotly_hist_kws (Optional[dict])
additional_fns (Optional[list[Callable]])
when (Literal['batch', 'epoch'])
save_dir (Optional[Union[str, Path]])
- class IncreaseCartesianCost(parameters=None)[source]#
Bases:
Callback
Callback for the enocdermap.autoencoder.AngleDihedralCarteisanEncoderMap.
This callback implements the soft-start of the cartesian cost.
- Parameters:
parameters (Optional[ADCParameters])
- class NoneInterruptCallback[source]#
Bases:
Callback
A callback that interrupts training, when NaN is encountered in weights.
- class ProgressBar(parameters=None)[source]#
Bases:
EncoderMapBaseCallback
Progressbar Callback. Mix in with model.fit() and make sure to set verbosity to zero.
- Parameters:
parameters (Optional['AnyParameters'])
- on_summary_step(epoch, logs=None)[source]#
Update the progress bar after an epoch with the current loss.
- on_train_batch_end(batch, logs=None)[source]#
Overwrites the parent class’ on_train_batch_end and adds a progress-bar update.
encodermap.callbacks.metrics module#
Metrics are meta-variables that can be computed to observe the training but are not directly linked to loss/cost and gradients.
- class ADCClashMetric(*args, **kwargs)[source]#
Bases:
AngleDihedralCartesianEncoderMapBaseMetric
- Parameters:
distance_unit (Literal['nm', 'ang'])
name (str)
parameters (Optional[ADCParameters])
- custom_update_state = True#
Metric that computes clashes between atoms in the reconstructed backbone.
Please choose the correct distance unit.
- classmethod from_config(config, custom_objects=None)[source]#
Reconstructs this keras serializable from a dict.
- Parameters:
custom_objects (Optional[dict[str, Any]]) – Not needed here, but see https://keras.io/guides/serialization_and_saving/ for yourself.
- class ADCRMSDMetric(*args, **kwargs)[source]#
Bases:
AngleDihedralCartesianEncoderMapBaseMetric
- Parameters:
name (str)
parameters (Optional[Union[Parameters, ADCParameters]])
kwargs (Any)
- custom_update_state = True#
- result()[source]#
Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
- Returns:
A scalar tensor, or a dictionary of scalar tensors.
- update_state(y_true, y_pred, sample_weight=None)[source]#
Accumulates statistics for the metric.
Note: This function is executed as a graph function in graph mode. This means:
Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example.
You don’t need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed.
As a result, code should generally work the same way with graph or eager execution.
- Parameters:
*args
**kwargs – A mini-batch of inputs to the Metric.
- class AngleDihedralCartesianEncoderMapBaseMetric(*args, **kwargs)[source]#
Bases:
Metric
- Parameters:
parameters (Optional[ADCParameters])
name (str | None)
current_training_step (Optional[int])
- custom_update_state = False#
- classmethod from_config(config, custom_objects=None)[source]#
Reconstructs this keras serializable from a dict.
- Parameters:
custom_objects (Optional[dict[str, Any]]) – Not needed here, but see https://keras.io/guides/serialization_and_saving/ for yourself.
- class EncoderMapBaseMetric(*args, **kwargs)[source]#
Bases:
Metric
Base class for metrics in EncoderMap.
Metrics are used to judge the performance of ML models. They are similar to loss functions as they can (but don’t have to) be computed at every iteration of the training. Oftentimes, metrics implement more expensive calculations. Metrics are also automatically added to a model’s training history, accessible via history = emap.train().
Examples:
In this example, the update method always returns zero.
>>> import encodermap as em >>> import numpy as np ... >>> class MyMetric(em.callbacks.EncoderMapBaseMetric): ... def update(self, y_true, y_pred, sample_weight=None): ... return 0.0 ... >>> emap = em.EncoderMap() Output... >>> emap.add_metric(MyMetric) >>> history = emap.train() Saving... >>> np.mean(history["MyMetric Metric"]) 0.0 >>> len(history["MyMetric Metric"]) == emap.p.n_steps True
- Parameters:
parameters (Optional[ADCParameters])
name (str | None)
current_training_step (Optional[int])
- custom_update_state = True#
- classmethod from_config(config, custom_objects=None)[source]#
Reconstructs this keras serializable from a dict.
- Parameters:
custom_objects (Optional[dict[str, Any]]) – Not needed here, but see https://keras.io/guides/serialization_and_saving/ for yourself.
- class OmegaAngleBaseMetric(*args, **kwargs)[source]#
Bases:
AngleDihedralCartesianEncoderMapBaseMetric
- custom_update_state = True#
- class SidechainVsBackboneFrequencyBaseMetric(*args, **kwargs)[source]#
Bases:
AngleDihedralCartesianEncoderMapBaseMetric
- custom_update_state = True#
- kabsch_weighted(P, Q, W=None)[source]#
Taken from Jimmy C. Kromann’s RMSD (charnley/rmsd) Using the Kabsch algorithm with two sets of paired point P and Q. Each vector set is represented as an NxD matrix, where D is the dimension of the space. An optional vector of weights W may be provided.
Note that this algorithm does not require that P and Q have already been overlayed by a centroid translation.
The function returns the rotation matrix U, translation vector V, and RMS deviation between Q and P’, where P’ is:
P’ = P * U + V
For more info see http://en.wikipedia.org/wiki/Kabsch_algorithm
- Parameters:
P (np.ndarray) – Points A with shape (n_points, 3).
Q (np.ndarray) – Points B with shape (n_points, 3).
W (np.ndarray) – Weights with shape (n_points, ).
- Returns:
The RMSD value in the same units, as the input points.
- Return type:
- rmsd_numpy(a, b, translate=True)[source]#
Implements Kabsch-Umeyama algorithm.
References
- @article{kabsch1976solution,
title={A solution for the best rotation to relate two sets of vectors}, author={Kabsch, Wolfgang}, journal={Acta Crystallographica Section A: Crystal Physics, Diffraction, Theoretical and General Crystallography}, volume={32}, number={5}, pages={922–923}, year={1976}, publisher={International Union of Crystallography}
}
- rmsd_tf(a, b, p)[source]#
- Parameters:
a (tf.Tensor)
b (tf.Tensor)
p (ADCParameters)
- Return type:
tf.Tensor