CosineSimilarity#

class deepinv.loss.metric.CosineSimilarity(**kwargs)[source]#

Bases: Metric

Cosine similarity metric.

Computes cosine similarity between reconstruction \(\hat{x}\) and ground truth \(x\). A higher value means more similar. The metric is calculated as:

\(\text{CosineSim}(\hat{x}, x) =\dfrac{\langle \hat{x}, x \rangle}{\|\hat{x}\|_2 \, \|x\|_2}\),where \(\langle \hat{x}, x \rangle\) is the Euclidean inner product.

Note

By default, no reduction is applied over the batch dimension.

Example:

>>> import torch
>>> from deepinv.loss.metric import CosineSimilarity
>>> m = CosineSimilarity()
>>> x_net = x = torch.ones(3, 2, 8, 8) # B,C,H,W
>>> m(x_net, x)
tensor([1.0000, 1.0000, 1.0000])
Parameters:
  • complex_abs (bool) – take complex magnitude before computing similarity.

  • reduction (str) – reduction over batch (“mean”, “sum”, “none”/None).

  • norm_inputs (str) – normalization for inputs (“l2”, “min_max”, or None).

  • center_crop (int, tuple[int], None) – crop before computing metric.