metrics¶
To allow logging of model performance during each epoch of training we provide an interface to compute metrics in an online (streaming) fashion. This is not only more memory/time efficient than loading all generated files and calculating performance after an epoch, but it allows for performance to be reported at each batch.
See StatefulMetric
for details on how to define a new streaming metric.
The following are metrics provided by Morgana. See F0Model for example usage.
StatefulMetric¶
-
class
morgana.metrics.
StatefulMetric
[source]¶ Bases:
object
Abstract class for accumulating information and calculating a result using current stored data.
- Three abstract methods must be implemented so that a metric can be calculated in an online fashion.
-
accumulate
(self, *args, **kwargs)[source]¶ Accumulates a batch of values into the stateful variables.
-
result_as_json
(self, *args)[source]¶ If the result is a
torch.Tensor
then it must be converted tonp.ndarray
to be saved as JSON.
Handler¶
This metric is used to maintain multiple collections of metrics, it is created automatically by
morgana.experiment_builder.ExperimentBuilder
and added to your model instance as an attribute called metrics
.
-
class
morgana.metrics.
Handler
(**metrics)[source]¶ Bases:
morgana.metrics.StatefulMetric
Container for running a set of metrics.
- Parameters
metrics (dict[str, StatefulMetric]) – Name of metrics and StatefulMetric instances that will be assigned to all metric collections.
-
collections
¶ Multiple collections, each containing a map of metrics. Metrics can be overlapping between collections. It is possible to have multiple collections as different training modes (train/valid) may involve different metrics.
- Type
dict[str, dict[str, StatefulMetric]]
-
add_metrics
(self, collections=('all', ), **kwargs)[source]¶ Updates the given collections with all names and metrics in
kwargs
.The new metrics will also be added to
self.metrics
, even if no collections are specified.- Parameters
collections (str or list[str]) – Name(s) of collections to update with the given metrics.
kwargs (dict[str, StatefulMetric]) – Name of metrics and
StatefulMetric
instances to be added.
-
add_collection
(self, collection, from_collections=())[source]¶ Creates a new collection, and copies the metrics of other collection(s).
- Parameters
collection (str) – Name of new collection.
from_collections – Name(s) of collections from which to copy existing metrics.
-
accumulate
(self, collection, **kwargs)[source]¶ Accumulates to all metrics in kwargs.
- Parameters
collection (str) – Metrics in this collection will be updated.
kwargs (dict[str, tuple]) – Names of metrics, and inputs to each metric’s accumulate function, e.g. a list of
torch.Tensor
.
-
result
(self, collection='all', *args)[source]¶ Gets the result for all metrics in the given collection.
Print¶
-
class
morgana.metrics.
Print
[source]¶ Bases:
morgana.metrics.StatefulMetric
Class for printing the last reported value.
-
value
¶ Most recent accumulated input.
-
History¶
-
class
morgana.metrics.
History
(max_len=None)[source]¶ Bases:
morgana.metrics.StatefulMetric
Class for storing the history of any object.
- Parameters
max_len (int) – Maximum length of the history being stored.
-
history
¶ Tensor of (up to
max_len
) previous inputs.
TensorHistory¶
-
class
morgana.metrics.
TensorHistory
(feat_dim, max_len=None, device=None)[source]¶ Bases:
morgana.metrics.StatefulMetric
Class for storing the history of a tensor.
- Parameters
-
history
¶ Tensor of (up to
max_len
) previous inputs.
Mean¶
RMSE¶
-
class
morgana.metrics.
RMSE
[source]¶ Bases:
morgana.metrics.Mean
Class for computing RMSE in an online fashion.
-
sum
¶ Sum of squared difference of
target
andpred
inputs.
-
count
¶ Number of valid frames for all inputs.
-