CompositeMetric

class paddle.fluid.metrics. CompositeMetric ( name=None ) [source]

This op creates a container that contains the union of all the added metrics. After the metrics added in, calling eval() method will compute all the contained metrics automatically. CAUTION: only metrics with the SAME argument list can be added in a CompositeMetric instance.

Inherit from: MetricBase

Parameters

name (str, optional) – Metric name. For details, please refer to Name. Default is None.

Examples

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/metrics.py:docstring of paddle.fluid.metrics.CompositeMetric, line 12)

Error in “code-block” directive: maximum 1 argument(s) allowed, 81 supplied.

.. code-block:: python
    import paddle.fluid as fluid
    import numpy as np
    preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
             [0.2], [0.3], [0.5], [0.8], [0.6]]
    labels = [[0], [1], [1], [1], [1],
              [0], [0], [0], [0], [0]]
    preds = np.array(preds)
    labels = np.array(labels)
    comp = fluid.metrics.CompositeMetric()
    precision = fluid.metrics.Precision()
    recall = fluid.metrics.Recall()
    comp.add_metric(precision)
    comp.add_metric(recall)
    comp.update(preds=preds, labels=labels)
    numpy_precision, numpy_recall = comp.eval()
    print("expect precision: %.2f, got %.2f" % ( 3. / 5, numpy_precision ) )
    print("expect recall: %.2f, got %.2f" % (3. / 4, numpy_recall ) )

add_metric ( metric )

add_metric

Add a new metric to container. Noted that the argument list of the added one should be consistent with existed ones.

Parameters

metric (MetricBase) – a instance of MetricBase

update ( preds, labels )

update

Update the metrics of this container.

Parameters
  • preds (numpy.array) – predicted results of current mini-batch, the shape and dtype of which should meet the requirements of the corresponded metric.

  • labels (numpy.array) – ground truth of current mini-batch, the shape and dtype of which should meet the requirements of the corresponded metric.

eval ( )

eval

Calculate the results of all metrics sequentially.

Returns

results of all added metrics. The shape and dtype of each result depend on the definition of its metric.

Return type

list

get_config ( )

get_config

Get the metric and current states. The states are the members who do not has “_” prefix.

Parameters

None

Returns

a python dict, which contains the inner states of the metric instance

Return types:

a python dict

reset ( )

reset

reset function empties the evaluation memory for previous mini-batches.

Parameters

None

Returns

None

Return types:

None