Skip to content

Commit ddf8cd5

Browse files
diego-urgellfacebook-github-bot
authored andcommitted
Add base AnomalyEvaluator class (#850)
Summary: Pull Request resolved: #850 ### This Stack Based on [this RFC](https://docs.google.com/document/d/1K1KQ886dynMRejR0ySH1fctOjS7gxaCS8AB1L_PHxU4/edit?usp=sharing), we are adding a new logger that warns about anomalous values in metrics, and optionally executes a callback function with potential side effects. This could be useful for users to realize sooner that something has gone wrong during training. ### This Diff To provide flexibility when detecting anomalous metric values, instead of assuming and hardcoding a predefined check (like a threshold), let's create an interface that can be overriden to implement custom checks. Reviewed By: JKSenthil Differential Revision: D58564201 fbshipit-source-id: 662ae38dd866aa979e84e76f6364ec1d57c1e090
1 parent 1d118c4 commit ddf8cd5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

torchtnt/utils/anomaly_evaluation.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# pyre-strict
9+
10+
11+
import logging
12+
from abc import ABC, abstractmethod
13+
14+
_logger: logging.Logger = logging.getLogger(__name__)
15+
16+
17+
class MetricAnomalyEvaluator(ABC):
18+
"""
19+
Abstract base class for metric anomaly evaluators. An evaluator specifies the logic to determine that
20+
a particular metric value is anomalous. To implement a custom method, create a subclass and implement
21+
the following methods:
22+
- :py:meth:`~torchtnt.utils.loggers.metric_anomaly_logger.MetricAnomalyEvaluator.update` should receive
23+
the metric value and update the internal state. This is specially useful for algorithms that require
24+
storing some previous values, moving averages, etc.
25+
- :py:meth:`~torchtnt.utils.loggers.metric_anomaly_logger.MetricAnomalyEvaluator.is_anomaly` determines
26+
whether the current metric state is anomalous.
27+
28+
Likely there are some warm-up steps before the metric is stable and can be checked against anomalies, so
29+
the separation of state update and actual detection provides this flexibility.
30+
"""
31+
32+
@abstractmethod
33+
def update(self, value: float) -> None:
34+
"""
35+
Update the internal state with the given metric value. This should not determine anomalies itself, but
36+
only aggregate the current value according to the anomaly detection algorithm.
37+
38+
Note:: If no aggregation is required, this method can store the value directly, to be used in `is_anomaly`.
39+
40+
Args:
41+
value: Metric value
42+
"""
43+
pass
44+
45+
@abstractmethod
46+
def is_anomaly(self) -> bool:
47+
"""
48+
Determine whether the current metric state is anomalous. This should be overridden with custom logic related to
49+
an anomaly detection algorithm.
50+
"""
51+
pass

0 commit comments

Comments
 (0)