tf.keras.metrics.Mean

Compute the (weighted) mean of the given values.

Inherits From: Metric

Used in the notebooks

Used in the guide Used in the tutorials

For example, if values is [1, 3, 5, 7] then the mean is 4. If sample_weight was specified as [1, 1, 0, 0] then the mean would be 2.

This metric creates two variables, total and count. The mean value returned is simply total divided by count.

name (Optional) string name of the metric instance.
dtype (Optional) data type of the metric result.

Example:

m = Mean()
m.update_state([1, 3, 5, 7])
m.result()
4.0
m.reset_state()
m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])
m.result()
2.0

!-<- Tabular view --
 ta<ble class=re"sponsive fixed orange
c">o<lgroupco><l width=21"4pxco"><l/c><olgroup
t>r<th>< colspan=2h"2">< class=ad"d-linkAt">tributes/h<2/t><h/t><r

tr<
t>d<
`>dtype`a <id=dt"ype/a"><
/>t<d
t>d<

>/t<d
/>t<rtr><
t>d<
`>variables`a <id=va"riables/a"><
/>t<d
t>d<

>/t<d
/>t<r
/>t<able



## Methods

h3< id=ad"d_variableco"><dead>d_variable/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L186-L202Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dead>d_variable(
    shape, initializer, dtype=None, aggregation=#x&27;sum#x&27;, name=None
)
/c<ode/p><re




h3< id=ad"d_weightco"><dead>d_weight/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L204-L208Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dead>d_weight(
    shape=(), initializer=None, dtype=None, name=None
)
/c<ode/p><re




h3< id=fr"om_configco"><defr>om_config/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L226-L228Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<de@c>lassmethod/c<ode
c>o<defr>om_config(
    config
)
/c<ode/p><re




h3< id=ge"t_configco"><dege>t_config/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L222-L224Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dege>t_config()
/c<ode/p><re

Return the serializable config of the metric.


h3< id=re"set_stateco"><dere>set_state/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L150-L152Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dere>set_state()
/c<ode/p><re

Reset all of the metric state variables.

This function is called between epochs/steps,
when a metric is evaluated during training.

h3< id=re"sultco"><dere>sult/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L154-L157Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dere>sult()
/c<ode/p><re

Compute the current metric value.


!-<- Tabular view --
 ta<ble class=re"sponsive fixed orange
c">o<lgroupco><l width=21"4pxco"><l/c><olgroup
t>r<th>< colspan=2R"e">turns/t<h/t><r
t>r< class=al"t
t">d< colspan=2
"A"> scalar tensor, or a dictionary of scalar tensors.
/t<d
/>t<r

/t<able



h3< id=st"ateless_reset_stateco"><dest>ateless_reset_state/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L164-L177Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dest>ateless_reset_state()
/c<ode/p><re




h3< id=st"ateless_resultco"><dest>ateless_result/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L148-L162Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dest>ateless_result(
    metric_variables
)
/c<ode/p><re




h3< id=st"ateless_update_stateco"><dest>ateless_update_state/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L115-L138Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<dest>ateless_update_state(
    metric_variables, *args, **kwargs
)
/c<ode/p><re




h3< id=up"date_stateco"><deup>date_state/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L137-L148Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<deup>date_state(
    values, sample_weight=None
)
/c<ode/p><re

Accumulate statistics for the metric.


h3< id=__"call__co"><de__>call__/c<ode/h><3

a <target=_b"lank c"lass=ex"ternal h"ref=ht"tps://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L217-L220Vi">ew source/a<

>pr<e class=de"vsite-click-to-copy prettyprint lang-py tfo-signature-link
c">o<de__>call__(
    *args, **kwargs
)
/c<ode/p><re

Call self as a function.