CosineSimilarity reward
CosineSimilarity
Bases: RewardFunctionWrapper
Computes the cosine similarity between y_true
and y_pred
.
Formula:
The formula is similar to the classic cosine similarity used in deep learning, but scaled to [0.0, 1.0] and adjusted to have a reward that tend towards 1.0 if the two objects are similar (and 0.0 otherwise).
Example:
program.compile(
reward=synalinks.rewards.CosineSimilarity(
embedding_model=embedding_model
)
optimizer=synalinks.optimizers.RandomFewShot(),
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embedding_model
|
EmbeddingModel
|
The embedding model to use to compute the cosine similarity. |
None
|
axis
|
int
|
(Optional) Defaults to |
-1
|
name
|
str
|
(Optional) string name of the reward instance. |
'cosine_similarity'
|
in_mask
|
list
|
(Optional) list of keys to keep to compute the reward. |
None
|
out_mask
|
list
|
(Optional) list of keys to remove to compute the reward. |
None
|
Source code in synalinks/src/rewards/cosine_similarity.py
cosine_similarity(y_true, y_pred, embedding_model=None, axis=-1)
async
Computes the cosine similarity between y_true
and y_pred
.
Formula:
The formula is similar to the classic cosine similarity used in deep learning, but scaled to [0.0, 1.0] and adjusted to have a reward that tend towards 1.0 if the two objects are similar (and 0.0 otherwise).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
JsonDataModel
|
The ground truth JSON data_model. |
required |
y_pred
|
JsonDataModel
|
The predicted JSON data_model. |
required |
embedding_model
|
EmbeddingModel
|
The embedding model to use to compute the cosine similarity. |
None
|
axis
|
int
|
(Optional) Defaults to |
-1
|
Returns:
Type | Description |
---|---|
float
|
The reward value, which tend to 1.0 if the values are similar, and towards 0.0 otherwise. |