Passed
Push — master ( ee1e78...515b92 )
by Konstantinos
01:14
created

NSTAlgorithmProgress.iterations()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import attr
2
3
4
@attr.s
5
class NSTAlgorithmProgress:
6
    tracked_metrics: dict = attr.ib(converter=lambda x: {k: [v] for k, v in dict(x).items()})
7
    _callbacks = attr.ib(default=attr.Factory(lambda self: {
8
        True: self._append,
9
        False: self._set,
10
    }, takes_self=True))
11
12
    def update(self, *args, **kwargs):
13
        metrics = args[0].state.metrics
14
        for metric_key, value in metrics.items():
15
            self._callbacks[metric_key in self.tracked_metrics](metric_key, value)
16
17
    def _set(self, key, value):
18
        self.tracked_metrics[key] = [value]
19
20
    def _append(self, key, value):
21
        self.tracked_metrics[key].append(value)
22
23
    # Properties
24
25
    @property
26
    def iterations(self):
27
        """Iterations completed."""
28
        return self.tracked_metrics.get('iterations', [None])[-1]
29
30
    @property
31
    def duration(self):
32
        """Time in seconds the iterative algorithm has been running."""
33
        return self.tracked_metrics.get('duration', [None])[-1]
34
35
    @property
36
    def cost_improvement(self):
37
        """Difference of loss function between the last 2 measurements.
38
39
        Positive value indicates that the loss went down and that the learning
40
        process moved towards the (local) minimum (in terms of minimizing the
41
        loss/cost function).
42
43
        So roughly, positive values indicate improvement [moving towards (local)
44
        minimum] and negative indicate moving away from minimum.
45
46
        Moving refers to the learning parameters.
47
        """
48
        if 1 < len(self.tracked_metrics.get('cost', [])):
49
            return self.tracked_metrics['cost'][-2] - self.tracked_metrics['cost'][-1]
50