Skip to content

utils.tools.EpochMetrics

Class ยท Source

emdict = mdnc.utils.tools.EpochMetrics(
    reducer=np.mean
)

A dictionary for storing metrics. The __setitem__ and __getitem__ operators are overloaded. This tool is used for calculating the statistics of epoch metrics easily. The following codes are equivalent:

Example
1
2
3
4
5
emdict = EpochMetrics()
for i in range(10):
    emdict['loss'] = i / 10
    emdict['metric2'] = - i / 10
print(emdict['loss'], emdict['metric2'])
1
2
3
4
5
emdict = {'loss': list()}
for i in range(10):
    emdict['loss'].append(i / 10)
    emdict['metric2'].append(- i / 10)
print(np.mean(emdict['loss']), np.mean(emdict['metric2']))

Arguments

Requries

Argument Type Description
reducer object A callable object (function). The input of this function should be a sequence and the output should be a scalar.

Methods

keys, values, items

for k in emdict.keys():
    ...
for v in emdict.values():
    ...
for k, v in emdict.items():
    ...

Used as iterators returned by a python dictionary.


pop, popitem

v = emdict.pop(k, default=None)
k, v = emdict.popitem(k)

Used as pop() and popitem() of a python dictionary.


get, setdefault

v = emdict.get(k, default=None)
emdict.setdefault(k, default=None)

Used as get() and setdefault() of a python dictionary.

Operators

__getitem__

v = emdict[keyword]

Get the reduced value of a specific keyword.

Requries

Argument Type Description
keyword object A python object that could be used as the keyword. This is the name of the metric.

Returns

Argument Description
v The returned metric, this value is a scalar reduced by the reducer provided in the initialization.

__setitem__

emdict[keyword] = v

Set a new value for a specific keyword.

Requries

Argument Type Description
keyword object A python object that could be used as the keyword. This is the name of the metric.
v int or
float
A scalar value. This value would be appended in the stored metric list.

Examples

Example
1
2
3
4
5
6
7
8
import mdnc

emdict = mdnc.utils.tools.EpochMetrics()
for i in range(10):
    emdict['loss'] = i / 10
    emdict['metric2'] = - i / 10
for k, v in emdict.items():
    print(k, v)
loss 0.45
metric2 -0.45

Last update: March 14, 2021

Comments