tf.keras.losses.CosineSimilarity
Stay organized with collections
Save and categorize content based on your preferences.
Computes the cosine similarity between y_true
& y_pred
.
Inherits From: Loss
tf.keras.losses.CosineSimilarity(
axis=-1,
reduction='sum_over_batch_size',
name='cosine_similarity'
)
Note that it is a number between -1 and 1. When it is a negative number
between -1 and 0, 0 indicates orthogonality and values closer to -1
indicate greater similarity. This makes it usable as a loss function in a
setting where you try to maximize the proximity between predictions and
targets. If either y_true
or y_pred
is a zero vector, cosine similarity
will be 0 regardless of the proximity between predictions and targets.
loss = -sum(l2_norm(y_true) * l2_norm(y_pred))
Args |
axis
|
The axis along which the cosine similarity is computed
(the features axis). Defaults to -1 .
|
reduction
|
Type of reduction to apply to the loss. In almost all cases
this should be "sum_over_batch_size" .
Supported options are "sum" , "sum_over_batch_size" or None .
|
name
|
Optional name for the loss instance.
|
Methods
call
View source
call(
y_true, y_pred
)
from_config
View source
@classmethod
from_config(
config
)
get_config
View source
get_config()
__call__
View source
__call__(
y_true, y_pred, sample_weight=None
)
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.losses.CosineSimilarity\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L149-L186) |\n\nComputes the cosine similarity between `y_true` \\& `y_pred`.\n\nInherits From: [`Loss`](../../../tf/keras/Loss) \n\n tf.keras.losses.CosineSimilarity(\n axis=-1,\n reduction='sum_over_batch_size',\n name='cosine_similarity'\n )\n\nNote that it is a number between -1 and 1. When it is a negative number\nbetween -1 and 0, 0 indicates orthogonality and values closer to -1\nindicate greater similarity. This makes it usable as a loss function in a\nsetting where you try to maximize the proximity between predictions and\ntargets. If either `y_true` or `y_pred` is a zero vector, cosine similarity\nwill be 0 regardless of the proximity between predictions and targets.\n\n#### Formula:\n\n loss = -sum(l2_norm(y_true) * l2_norm(y_pred))\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `axis` | The axis along which the cosine similarity is computed (the features axis). Defaults to `-1`. |\n| `reduction` | Type of reduction to apply to the loss. In almost all cases this should be `\"sum_over_batch_size\"`. Supported options are `\"sum\"`, `\"sum_over_batch_size\"` or `None`. |\n| `name` | Optional name for the loss instance. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `call`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L20-L22) \n\n call(\n y_true, y_pred\n )\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L30-L34) \n\n @classmethod\n from_config(\n config\n )\n\n### `get_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L185-L186) \n\n get_config()\n\n### `__call__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/loss.py#L32-L61) \n\n __call__(\n y_true, y_pred, sample_weight=None\n )\n\nCall self as a function."]]