metrax.CosineSimilarity#

class metrax.CosineSimilarity(total: Array, count: Array)#

Bases: Average

Calculates the Cosine Similarity between two arrays.

The Cosine Similarity is defined as the dot product of the vectors divided by the product of their magnitudes (norms).

\[cos_{sim}(x,y) = \frac{x \cdot y}{||x|| * ||y||}\]
__init__(total: Array, count: Array) None#

Methods

__init__(total, count)

compute()

Computes final metrics from intermediate values.

compute_value()

Wraps compute() and returns a values.Value.

empty()

Returns an empty instance (i.e. .merge(Metric.empty()) is a no-op).

from_fun(fun)

Calls cls.from_model_output with the return value from fun.

from_model_output(predictions, targets[, axis])

Creates a CosineSimilarity instance.

from_output(name)

Calls cls.from_model_output with model output named name.

merge(other)

Returns Metric that is the accumulation of self and other.

reduce()

Reduces the metric along it first axis by calling _reduce_merge().

replace(**updates)

Returns a new object replacing the specified fields with new values.

Attributes

total

count

classmethod from_model_output(predictions: Array, targets: Array, axis: int = -1) CosineSimilarity#

Creates a CosineSimilarity instance.

Parameters:
  • predictions – A floating point array of the predictions. The shape should be (batch_size,).

  • targets – A floating point array of the targets. The shape should be (batch_size,).

  • axis – The axis to compute the norm over.

Returns:

A CosineSimilarity instance.

__init__(total: Array, count: Array) None#
replace(**updates)#

Returns a new object replacing the specified fields with new values.