This is the harmonic mean of precision and recall.
Its output range is [0, 1]. It works for both multi-class
and multi-label classification.
It is defined as:
f1_score=2*(precision*recall)/(precision+recall)
Args
average
Type of averaging to be performed on data.
Acceptable values are None, "micro", "macro"
and "weighted". Default value is None.
If None, no averaging is performed and result() will return
the score for each class.
If "micro", compute metrics globally by counting the total
true positives, false negatives and false positives.
If "macro", compute metrics for each label,
and return their unweighted mean.
This does not take label imbalance into account.
If "weighted", compute metrics for each label,
and return their average weighted by support
(the number of true instances for each label).
This alters "macro" to account for label imbalance.
It can result in an score that is not between precision and recall.
threshold
Elements of y_pred greater than threshold are
converted to be 1, and the rest 0. If threshold is
None, the argmax of y_pred is converted to 1, and the rest to 0.
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:
[[["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.F1Score\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/metrics/f_score_metrics.py#L250-L323) |\n\nComputes F-1 Score.\n\nInherits From: [`FBetaScore`](../../../tf/keras/metrics/FBetaScore), [`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.F1Score`](https://www.tensorflow.org/api_docs/python/tf/keras/metrics/F1Score)\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.F1Score\\`\n\n\u003cbr /\u003e\n\n tf.keras.metrics.F1Score(\n average=None, threshold=None, name='f1_score', dtype=None\n )\n\nThis is the harmonic mean of precision and recall.\nIts output range is `[0, 1]`. It works for both multi-class\nand multi-label classification.\n\n#### It is defined as:\n\n f1_score = 2 * (precision * recall) / (precision + recall)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `average` | Type of averaging to be performed on data. Acceptable values are `None`, `\"micro\"`, `\"macro\"` and `\"weighted\"`. Default value is `None`. If `None`, no averaging is performed and `result()` will return the score for each class. If `\"micro\"`, compute metrics globally by counting the total true positives, false negatives and false positives. If `\"macro\"`, compute metrics for each label, and return their unweighted mean. This does not take label imbalance into account. If `\"weighted\"`, compute metrics for each label, and return their average weighted by support (the number of true instances for each label). This alters `\"macro\"` to account for label imbalance. It can result in an score that is not between precision and recall. |\n| `threshold` | Elements of `y_pred` greater than `threshold` are converted to be 1, and the rest 0. If `threshold` is `None`, the argmax of `y_pred` is converted to 1, and the rest to 0. |\n| `name` | Optional. String name of the metric instance. |\n| `dtype` | Optional. Data type of the metric result. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| F-1 Score: float. ||\n\n\u003cbr /\u003e\n\n#### Example:\n\n metric = tf.keras.metrics.F1Score(threshold=0.5)\n y_true = np.array([[1, 1, 1],\n [1, 0, 0],\n [1, 1, 0]], np.int32)\n y_pred = np.array([[0.2, 0.6, 0.7],\n [0.2, 0.6, 0.6],\n [0.6, 0.8, 0.0]], np.float32)\n metric.update_state(y_true, y_pred)\n result = metric.result()\n result.numpy()\n array([0.5 , 0.8 , 0.6666667], dtype=float32)\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/f_score_metrics.py#L245-L247) \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/f_score_metrics.py#L208-L231) \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/f_score_metrics.py#L175-L206) \n\n update_state(\n y_true, y_pred, sample_weight=None\n )\n\nAccumulates statistics for the metric.\n| **Note:** This function is executed as a graph function in graph mode. This means: a) Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example. b) You don't need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed. As a result, code should generally work the same way with graph or eager execution.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------|---------------------------------------|\n| `*args` | \u003cbr /\u003e \u003cbr /\u003e |\n| `**kwargs` | A mini-batch of inputs to the Metric. |\n\n\u003cbr /\u003e"]]