tf.keras.metrics.CategoricalCrossentropy
Stay organized with collections
Save and categorize content based on your preferences.
Computes the crossentropy metric between the labels and predictions.
Inherits From: MeanMetricWrapper
, Mean
, Metric
, Layer
, Module
tf.keras.metrics.CategoricalCrossentropy(
name='categorical_crossentropy',
dtype=None,
from_logits=False,
label_smoothing=0,
axis=-1
)
This is the crossentropy metric class to be used when there are multiple
label classes (2 or more). Here we assume that labels are given as a
one_hot
representation. eg., When labels values are [2, 0, 1],
y_true
= [[0, 0, 1], [1, 0, 0], [0, 1, 0]].
Args |
name
|
(Optional) string name of the metric instance.
|
dtype
|
(Optional) data type of the metric result.
|
from_logits
|
(Optional) Whether output is expected to be a logits tensor.
By default, we consider that output encodes a probability distribution.
|
label_smoothing
|
(Optional) Float in [0, 1]. When > 0, label values are
smoothed, meaning the confidence on label values are relaxed. e.g.
label_smoothing=0.2 means that we will use a value of 0.1 for label
0 and 0.9 for label 1 "
|
axis
|
(Optional) Defaults to -1. The dimension along which entropy is
computed.
|
Standalone usage:
# EPSILON = 1e-7, y = y_true, y` = y_pred
# y` = clip_ops.clip_by_value(output, EPSILON, 1. - EPSILON)
# y` = [[0.05, 0.95, EPSILON], [0.1, 0.8, 0.1]]
# xent = -sum(y * log(y'), axis = -1)
# = -((log 0.95), (log 0.1))
# = [0.051, 2.302]
# Reduced xent = (0.051 + 2.302) / 2
m = tf.keras.metrics.CategoricalCrossentropy()
m.update_state([[0, 1, 0], [0, 0, 1]],
[[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
m.result().numpy()
1.1769392
m.reset_state()
m.update_state([[0, 1, 0], [0, 0, 1]],
[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
sample_weight=tf.constant([0.3, 0.7]))
m.result().numpy()
1.6271976
Usage with compile()
API:
model.compile(
optimizer='sgd',
loss='mse',
metrics=[tf.keras.metrics.CategoricalCrossentropy()])
Methods
merge_state
View source
merge_state(
metrics
)
Merges the state from one or more metrics.
This method can be used by distributed systems to merge the state
computed by different metric instances. Typically the state will be
stored in the form of the metric's weights. For example, a
tf.keras.metrics.Mean metric contains a list of two weight values: a
total and a count. If there were two instances of a
tf.keras.metrics.Accuracy that each independently aggregated partial
state for an overall accuracy calculation, these two metric's states
could be combined as follows:
m1 = tf.keras.metrics.Accuracy()
_ = m1.update_state([[1], [2]], [[0], [2]])
m2 = tf.keras.metrics.Accuracy()
_ = m2.update_state([[3], [4]], [[3], [4]])
m2.merge_state([m1])
m2.result().numpy()
0.75
Args |
metrics
|
an iterable of metrics. The metrics must have compatible
state.
|
Raises |
ValueError
|
If the provided iterable does not contain metrics matching
the metric's required specifications.
|
reset_state
View source
reset_state()
Resets all of the metric state variables.
This function is called between epochs/steps,
when a metric is evaluated during training.
result
View source
result()
Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the
metric value using the state variables.
Returns |
A scalar tensor, or a dictionary of scalar tensors.
|
update_state
View source
update_state(
y_true, y_pred, sample_weight=None
)
Accumulates metric statistics.
For sparse categorical metrics, the shapes of y_true
and y_pred
are
different.
Args |
y_true
|
Ground truth label values. shape = [batch_size, d0, .. dN-1] or
shape = [batch_size, d0, .. dN-1, 1] .
|
y_pred
|
The predicted probability values. shape = [batch_size, d0, .. dN] .
|
sample_weight
|
Optional sample_weight acts as a
coefficient for the metric. If a scalar is provided, then the metric is
simply scaled by the given value. If sample_weight is a tensor of size
[batch_size] , then the metric for each sample of the batch is rescaled
by the corresponding element in the sample_weight vector. If the shape
of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted
to this shape), then each metric element of y_pred is scaled by the
corresponding value of sample_weight . (Note on dN-1 : all metric
functions reduce by 1 dimension, usually the last axis (-1)).
|
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 2023-10-06 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],[],null,["# tf.keras.metrics.CategoricalCrossentropy\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/probabilistic_metrics.py#L168-L237) |\n\nComputes the crossentropy metric between the labels and predictions.\n\nInherits From: [`MeanMetricWrapper`](../../../tf/keras/metrics/MeanMetricWrapper), [`Mean`](../../../tf/keras/metrics/Mean), [`Metric`](../../../tf/keras/metrics/Metric), [`Layer`](../../../tf/keras/layers/Layer), [`Module`](../../../tf/Module)\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.metrics.CategoricalCrossentropy`](https://www.tensorflow.org/api_docs/python/tf/keras/metrics/CategoricalCrossentropy)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n\\`tf.compat.v1.keras.metrics.CategoricalCrossentropy\\`\n\n\u003cbr /\u003e\n\n tf.keras.metrics.CategoricalCrossentropy(\n name='categorical_crossentropy',\n dtype=None,\n from_logits=False,\n label_smoothing=0,\n axis=-1\n )\n\nThis is the crossentropy metric class to be used when there are multiple\nlabel classes (2 or more). Here we assume that labels are given as a\n`one_hot` representation. eg., When labels values are \\[2, 0, 1\\],\n`y_true` = \\[\\[0, 0, 1\\], \\[1, 0, 0\\], \\[0, 1, 0\\]\\].\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| `from_logits` | (Optional) Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution. |\n| `label_smoothing` | (Optional) Float in \\[0, 1\\]. When \\\u003e 0, label values are smoothed, meaning the confidence on label values are relaxed. e.g. `label_smoothing=0.2` means that we will use a value of `0.1` for label `0` and `0.9` for label `1`\" |\n| `axis` | (Optional) Defaults to -1. The dimension along which entropy is computed. |\n\n\u003cbr /\u003e\n\n#### Standalone usage:\n\n # EPSILON = 1e-7, y = y_true, y` = y_pred\n # y` = clip_ops.clip_by_value(output, EPSILON, 1. - EPSILON)\n # y` = [[0.05, 0.95, EPSILON], [0.1, 0.8, 0.1]]\n # xent = -sum(y * log(y'), axis = -1)\n # = -((log 0.95), (log 0.1))\n # = [0.051, 2.302]\n # Reduced xent = (0.051 + 2.302) / 2\n m = tf.keras.metrics.CategoricalCrossentropy()\n m.update_state([[0, 1, 0], [0, 0, 1]],\n [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])\n m.result().numpy()\n 1.1769392\n\n m.reset_state()\n m.update_state([[0, 1, 0], [0, 0, 1]],\n [[0.05, 0.95, 0], [0.1, 0.8, 0.1]],\n sample_weight=tf.constant([0.3, 0.7]))\n m.result().numpy()\n 1.6271976\n\nUsage with `compile()` API: \n\n model.compile(\n optimizer='sgd',\n loss='mse',\n metrics=[tf.keras.metrics.CategoricalCrossentropy()])\n\nMethods\n-------\n\n### `merge_state`\n\n[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/base_metric.py#L288-L326) \n\n merge_state(\n metrics\n )\n\nMerges the state from one or more metrics.\n\nThis method can be used by distributed systems to merge the state\ncomputed by different metric instances. Typically the state will be\nstored in the form of the metric's weights. For example, a\ntf.keras.metrics.Mean metric contains a list of two weight values: a\ntotal and a count. If there were two instances of a\ntf.keras.metrics.Accuracy that each independently aggregated partial\nstate for an overall accuracy calculation, these two metric's states\ncould be combined as follows: \n\n m1 = tf.keras.metrics.Accuracy()\n _ = m1.update_state([[1], [2]], [[0], [2]])\n\n m2 = tf.keras.metrics.Accuracy()\n _ = m2.update_state([[3], [4]], [[3], [4]])\n\n m2.merge_state([m1])\n m2.result().numpy()\n 0.75\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|-----------|-----------------------------------------------------------------|\n| `metrics` | an iterable of metrics. The metrics must have compatible state. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|--------------------------------------------------------------------------------------------------|\n| `ValueError` | If the provided iterable does not contain metrics matching the metric's required specifications. |\n\n\u003cbr /\u003e\n\n### `reset_state`\n\n[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/base_metric.py#L249-L265) \n\n reset_state()\n\nResets all of the metric state variables.\n\nThis function is called between epochs/steps,\nwhen a metric is evaluated during training.\n\n### `result`\n\n[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/base_metric.py#L551-L563) \n\n result()\n\nComputes and returns the scalar metric value tensor or a dict of scalars.\n\nResult computation is an idempotent operation that simply calculates the\nmetric value using the state variables.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A scalar tensor, or a dictionary of scalar tensors. ||\n\n\u003cbr /\u003e\n\n### `update_state`\n\n[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/base_metric.py#L686-L728) \n\n update_state(\n y_true, y_pred, sample_weight=None\n )\n\nAccumulates metric statistics.\n\nFor sparse categorical metrics, the shapes of `y_true` and `y_pred` are\ndifferent.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `y_true` | Ground truth label values. shape = `[batch_size, d0, .. dN-1]` or shape = `[batch_size, d0, .. dN-1, 1]`. |\n| `y_pred` | The predicted probability values. shape = `[batch_size, d0, .. dN]`. |\n| `sample_weight` | Optional `sample_weight` acts as a coefficient for the metric. If a scalar is provided, then the metric is simply scaled by the given value. If `sample_weight` is a tensor of size `[batch_size]`, then the metric for each sample of the batch is rescaled by the corresponding element in the `sample_weight` vector. If the shape of `sample_weight` is `[batch_size, d0, .. dN-1]` (or can be broadcasted to this shape), then each metric element of `y_pred` is scaled by the corresponding value of `sample_weight`. (Note on `dN-1`: all metric functions reduce by 1 dimension, usually the last axis (-1)). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| Update op. ||\n\n\u003cbr /\u003e"]]