tf.keras.metrics.Mean
Stay organized with collections
Save and categorize content based on your preferences.
Compute the (weighted) mean of the given values.
Inherits From: Metric
tf.keras.metrics.Mean(
name='mean', dtype=None
)
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
.
Args |
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 -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2"><h2 class="add-link">Attributes</h2></th></tr>
<tr>
<td>
`dtype`<a id="dtype"></a>
</td>
<td>
</td>
</tr><tr>
<td>
`variables`<a id="variables"></a>
</td>
<td>
</td>
</tr>
</table>
## Methods
<h3 id="add_variable"><code>add_variable</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L186-L202">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>add_variable(
shape, initializer, dtype=None, aggregation='sum', name=None
)
</code></pre>
<h3 id="add_weight"><code>add_weight</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L204-L208">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>add_weight(
shape=(), initializer=None, dtype=None, name=None
)
</code></pre>
<h3 id="from_config"><code>from_config</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L226-L228">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>@classmethod</code>
<code>from_config(
config
)
</code></pre>
<h3 id="get_config"><code>get_config</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L222-L224">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>get_config()
</code></pre>
Return the serializable config of the metric.
<h3 id="reset_state"><code>reset_state</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L150-L152">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>reset_state()
</code></pre>
Reset all of the metric state variables.
This function is called between epochs/steps,
when a metric is evaluated during training.
<h3 id="result"><code>result</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L154-L157">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>result()
</code></pre>
Compute the current metric value.
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
A scalar tensor, or a dictionary of scalar tensors.
</td>
</tr>
</table>
<h3 id="stateless_reset_state"><code>stateless_reset_state</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L164-L177">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>stateless_reset_state()
</code></pre>
<h3 id="stateless_result"><code>stateless_result</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L148-L162">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>stateless_result(
metric_variables
)
</code></pre>
<h3 id="stateless_update_state"><code>stateless_update_state</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L115-L138">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>stateless_update_state(
metric_variables, *args, **kwargs
)
</code></pre>
<h3 id="update_state"><code>update_state</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L137-L148">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>update_state(
values, sample_weight=None
)
</code></pre>
Accumulate statistics for the metric.
<h3 id="__call__"><code>__call__</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L217-L220">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>__call__(
*args, **kwargs
)
</code></pre>
Call self as a function.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.metrics.Mean\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L94-L157) |\n\nCompute the (weighted) mean of the given values.\n\nInherits From: [`Metric`](../../../tf/keras/Metric) \n\n tf.keras.metrics.Mean(\n name='mean', dtype=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Effective Tensorflow 2](https://www.tensorflow.org/guide/effective_tf2) - [Mixed precision](https://www.tensorflow.org/guide/mixed_precision) - [Use TPUs](https://www.tensorflow.org/guide/tpu) | - [TensorFlow 2 quickstart for experts](https://www.tensorflow.org/tutorials/quickstart/advanced) - [Custom training: walkthrough](https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough) - [Custom training with tf.distribute.Strategy](https://www.tensorflow.org/tutorials/distribute/custom_training) - [Convolutional Variational Autoencoder](https://www.tensorflow.org/tutorials/generative/cvae) - [Training with Orbit](https://www.tensorflow.org/tfmodels/orbit/index) |\n\nFor example, if values is `[1, 3, 5, 7]` then the mean is 4.\nIf `sample_weight` was specified as `[1, 1, 0, 0]` then the mean would be 2.\n\nThis metric creates two variables, `total` and `count`.\nThe mean value returned is simply `total` divided by `count`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|------------------------------------------------|\n| `name` | (Optional) string name of the metric instance. |\n| `dtype` | (Optional) data type of the metric result. |\n\n\u003cbr /\u003e\n\n#### Example:\n\n m = Mean()\n m.update_state([1, 3, 5, 7])\n m.result()\n 4.0\n\n m.reset_state()\n m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])\n m.result()\n 2.0\n\n\n\n\n \u003c!-- Tabular view --\u003e\n \u003ctable class=\"responsive fixed orange\"\u003e\n \u003ccolgroup\u003e\u003ccol width=\"214px\"\u003e\u003ccol\u003e\u003c/colgroup\u003e\n \u003ctr\u003e\u003cth colspan=\"2\"\u003e\u003ch2 class=\"add-link\"\u003eAttributes\u003c/h2\u003e\u003c/th\u003e\u003c/tr\u003e\n\n \u003ctr\u003e\n \u003ctd\u003e\n `dtype`\u003ca id=\"dtype\"\u003e\u003c/a\u003e\n \u003c/td\u003e\n \u003ctd\u003e\n\n \u003c/td\u003e\n \u003c/tr\u003e\u003ctr\u003e\n \u003ctd\u003e\n `variables`\u003ca id=\"variables\"\u003e\u003c/a\u003e\n \u003c/td\u003e\n \u003ctd\u003e\n\n \u003c/td\u003e\n \u003c/tr\u003e\n \u003c/table\u003e\n\n\n\n ## Methods\n\n \u003ch3 id=\"add_variable\"\u003e\u003ccode\u003eadd_variable\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L186-L202\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eadd_variable(\n shape, initializer, dtype=None, aggregation='sum', name=None\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"add_weight\"\u003e\u003ccode\u003eadd_weight\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L204-L208\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eadd_weight(\n shape=(), initializer=None, dtype=None, name=None\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"from_config\"\u003e\u003ccode\u003efrom_config\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L226-L228\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003e@classmethod\u003c/code\u003e\n \u003ccode\u003efrom_config(\n config\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"get_config\"\u003e\u003ccode\u003eget_config\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L222-L224\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eget_config()\n \u003c/code\u003e\u003c/pre\u003e\n\n Return the serializable config of the metric.\n\n\n \u003ch3 id=\"reset_state\"\u003e\u003ccode\u003ereset_state\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L150-L152\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003ereset_state()\n \u003c/code\u003e\u003c/pre\u003e\n\n Reset all of the metric state variables.\n\n This function is called between epochs/steps,\n when a metric is evaluated during training.\n\n \u003ch3 id=\"result\"\u003e\u003ccode\u003eresult\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L154-L157\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eresult()\n \u003c/code\u003e\u003c/pre\u003e\n\n Compute the current metric value.\n\n\n \u003c!-- Tabular view --\u003e\n \u003ctable class=\"responsive fixed orange\"\u003e\n \u003ccolgroup\u003e\u003ccol width=\"214px\"\u003e\u003ccol\u003e\u003c/colgroup\u003e\n \u003ctr\u003e\u003cth colspan=\"2\"\u003eReturns\u003c/th\u003e\u003c/tr\u003e\n \u003ctr class=\"alt\"\u003e\n \u003ctd colspan=\"2\"\u003e\n A scalar tensor, or a dictionary of scalar tensors.\n \u003c/td\u003e\n \u003c/tr\u003e\n\n \u003c/table\u003e\n\n\n\n \u003ch3 id=\"stateless_reset_state\"\u003e\u003ccode\u003estateless_reset_state\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L164-L177\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003estateless_reset_state()\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"stateless_result\"\u003e\u003ccode\u003estateless_result\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L148-L162\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003estateless_result(\n metric_variables\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"stateless_update_state\"\u003e\u003ccode\u003estateless_update_state\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L115-L138\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003estateless_update_state(\n metric_variables, *args, **kwargs\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"update_state\"\u003e\u003ccode\u003eupdate_state\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/reduction_metrics.py#L137-L148\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eupdate_state(\n values, sample_weight=None\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n Accumulate statistics for the metric.\n\n\n \u003ch3 id=\"__call__\"\u003e\u003ccode\u003e__call__\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v3.3.3/keras/src/metrics/metric.py#L217-L220\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003e__call__(\n *args, **kwargs\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n Call self as a function."]]