Accuracy

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

This interface is used to calculate the mean accuracy over multiple batches. Accuracy object has two state: value and weight. The definition of Accuracy is available at https://en.wikipedia.org/wiki/Accuracy_and_precision

Parameters

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

Examples

import paddle.fluid as fluid
#suppose we have batch_size = 128
batch_size=128
accuracy_manager = fluid.metrics.Accuracy()

#suppose the accuracy is 0.9 for the 1st batch
batch1_acc = 0.9
accuracy_manager.update(value = batch1_acc, weight = batch_size)
print("expect accuracy: %.2f, get accuracy: %.2f" % (batch1_acc, accuracy_manager.eval()))

#suppose the accuracy is 0.8 for the 2nd batch
batch2_acc = 0.8

accuracy_manager.update(value = batch2_acc, weight = batch_size)
#the joint acc for batch1 and batch2 is (batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2
print("expect accuracy: %.2f, get accuracy: %.2f" % ((batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2, accuracy_manager.eval()))

#reset the accuracy_manager
accuracy_manager.reset()
#suppose the accuracy is 0.8 for the 3rd batch
batch3_acc = 0.8
accuracy_manager.update(value = batch3_acc, weight = batch_size)
print("expect accuracy: %.2f, get accuracy: %.2f" % (batch3_acc, accuracy_manager.eval()))
update(value, weight)

This function takes the minibatch states (value, weight) as input, to accumulate and update the corresponding status of the Accuracy object. The update method is as follows:

\[\begin{split}\\ \begin{array}{l}{\text { self. value }+=\text { value } * \text { weight }} \\ {\text { self. weight }+=\text { weight }}\end{array} \\\end{split}\]
Parameters
  • value (float|numpy.array) – accuracy of one minibatch.

  • weight (int|float) – minibatch size.

eval()

This function returns the mean accuracy (float or numpy.array) for all accumulated minibatches.

Returns

mean accuracy for all accumulated minibatches.

Return type

float or numpy.array

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 costains the inner states of the metric instance

Return types:

a python dict

reset()

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

Parameters

None

Returns

None

Return types:

None