CosineSimilarity#
- class deepinv.loss.metric.CosineSimilarity(**kwargs)[source]#
Bases:
MetricCosine 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])