Metric status: why batch mean value cannot be directly averaged

Keras Metric Object normally saves a batch status, such as the sum of values and the sum of weights.

Thematic shorts • IntroductionCollate in 2 self-measured2 First-hand sources
Start Body
Learning objectives and first steps2 objectives 2 first fixes

Try to do it after reading

  • Distinguishing the cumulative status of the indicator from the current batch return
  • There's not a lot of them by real denominators

Suggested first

  • Python Basic Syntax:
  • Counting and training closed

The training log doesn't show the last one

Keras Metric Object normally saves a batch status, such as the sum of values and the sum of weights. update_state update statistics, result read results, reset_state empty cumulative. It differs from the term “overlay variable after one number per batch” and from the term “unconditional average average of all batches”. The molecular denominator of the indicator is determined before the correct state can be judged.

The first two samples loss is [1,3] and the average value of the batch 2; Article 1 of the second instalment loss is [10] and the average value of the batch is 10. If the two groups of mean values are equalized by an average of six; The average total sample value was 14/3, approximately 4.667. The difference arises from the fact that the second batch was expanded to the same weight as the first two aggregates and was not a question of calculating accuracy.

If Metric receives a sample by loss, it can accumulate sum14 and count3. If the average batch value has been attributed to the standard, the number of batch true samples needs to be clearly used as weights, otherwise the count may become “many batches”. Sequence tasks may also be used as valid token denominators and batch sizes are not sufficient weights.

Resetting boundaries is also a definition of indicators

The replacement of each epoch, training phase and validation phase shall be determined by the call process. Hand-written loops forget reset_state, which mixes the results of round 2 into round 1; Training and validation are shared and not replaced, thus contaminating the validation indicators with training data. Even if the model does not update the parameters at the time of validation, the statistical status may be updated.

The accuracy rate may be divided by the aggregate, but indicators such as AUC and F1 have their own status and approximate rules, and it cannot be assumed that they can be easily averaged by the batch of indicators. In particular, when the distribution of positive and negative samples is very uneven, the average of the batches of AUC is not equal to the AUC in the order of all samples. The overall cumulative programme of the corresponding indicators should be used and thresholds set should be checked.

When miscalculated, a data set containing only three samples was taken, calculated using batch sizes 1, 2 and 3, respectively. If the goal is a full sample equivalent, the result should be consistent; Check end weights, duplicate data and repositioning in cases of inconsistency. This examination is more direct than the fractional differences in large data.

Version scope is Keras3 the current Metric interface, the TensorFlow backend. 4.667 by hand; This example of mean value is not likely to be used as an example to summarize the internal dimensions of the different indicators.

Value recovery

losses=[1,3,10]
assert sum(losses)/len(losses) == 14/3
assert ((1+3)/2+10)/2 == 6
Two batches of 2 and 1, with average value of 2 and 10, and what is the average value of the whole sample?

(2x2+1x10)/3 = 14/3, about 4.667 instead of 6.

Can Metric state be emptied before authentication only training = False?

Can't. The forward operating mode is independent of the cumulative status of the indicator and requires a clear process-based call for reset_state or the use of stand-alone target.

Sources and reconciliations

Definitions, methods and interfaces are available.

  1. Keras 3 — Customizing what happens in fit with TensorFlow
  2. Keras 3 — Metrics

Figures and calculations are reorganized according to this chapter. Mathematical measurements, framework operations and real business effects are described separately in the body of the text.

← Back to the knowledge directory
Ask Max Ask about this site
Ask Max ↗

Opening the assistant…

Paper figure

Photo comments

LEAVE A NOTE

About this photograph

Connecting to the discussion board…

    Comments become public after moderation. You can delete your own comments from this browser.
    No email is required. Clearing cookies prevents this browser from claiming earlier comments.

    MAX WANG / INDEX

    Find something

    Enter a keyword, or explore the directory below.