cosine_distance

Contents

cosine_distance#

class braintools.metric.cosine_distance(predictions, targets, epsilon=1e-08)#

Compute the cosine distance between targets and predictions.

The cosine distance, implemented here, measures the dissimilarity of two vectors as the opposite of cosine similarity: \(1 - \cos(\theta)\).

Parameters:
  • predictions (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – The predicted vectors, with shape [..., dim].

  • targets (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Ground truth target vectors, with shape [..., dim].

  • epsilon (float) – Minimum norm used as a floor in the denominator of the cosine similarity. The default of 1e-8 (rather than 0) ensures zero-vector safety actually takes effect.

Returns:

Cosine distances, with shape [...].

Return type:

Array | ndarray | bool | number | bool | int | float | complex | Quantity

References

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> pred = jnp.array([1.0, 0.0])
>>> target = jnp.array([0.0, 1.0])
>>> braintools.metric.cosine_distance(pred, target)
Array(1., dtype=float32)