Table of Contents

Class CosineSimilarityLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Cosine Similarity Loss between two vectors.

public class CosineSimilarityLoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations (e.g., float, double).

Inheritance
CosineSimilarityLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Cosine Similarity measures how similar two vectors are in terms of their orientation, regardless of their magnitude (size).

The formula for cosine similarity is: cos(θ) = (A·B)/(||A||×||B||) Where:

  • A·B is the dot product of vectors A and B
  • ||A|| and ||B|| are the magnitudes (lengths) of vectors A and B
  • θ is the angle between vectors A and B

The loss is calculated as 1 - cosine similarity, so:

  • A value of 0 means the vectors are perfectly aligned (very similar)
  • A value of 1 means they are perpendicular (no similarity)
  • A value of 2 means they point in exactly opposite directions

Cosine similarity loss is particularly useful for:

  • Text similarity tasks (comparing document vectors)
  • Recommendation systems
  • Image retrieval
  • Any task where the direction of vectors matters more than their magnitude

It's often preferred over Euclidean distance when working with high-dimensional sparse vectors.

Constructors

CosineSimilarityLoss()

Initializes a new instance of the CosineSimilarityLoss class.

public CosineSimilarityLoss()

Methods

CalculateDerivative(Vector<T>, Vector<T>)

Calculates the derivative of the Cosine Similarity Loss with respect to the predicted values.

public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted vector from the model.

actual Vector<T>

The actual (target) vector.

Returns

Vector<T>

A vector containing the gradient of the loss with respect to each prediction.

CalculateLoss(Vector<T>, Vector<T>)

Calculates the Cosine Similarity Loss between two vectors.

public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted vector from the model.

actual Vector<T>

The actual (target) vector.

Returns

T

A scalar value representing the cosine similarity loss.