Share via


TensorPrimitives.CosineSimilarity Method

Definition

Overloads

CosineSimilarity(ReadOnlySpan<Single>, ReadOnlySpan<Single>)

Computes the cosine similarity between the two specified non-empty, equal-length tensors of single-precision floating-point numbers.

CosineSimilarity<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Computes the cosine similarity between the two specified non-empty, equal-length tensors of numbers.

CosineSimilarity(ReadOnlySpan<Single>, ReadOnlySpan<Single>)

Source:
TensorPrimitives.cs
Source:
TensorPrimitives.Single.cs
Source:
TensorPrimitives.Single.cs

Computes the cosine similarity between the two specified non-empty, equal-length tensors of single-precision floating-point numbers.

public:
 static float CosineSimilarity(ReadOnlySpan<float> x, ReadOnlySpan<float> y);
public static float CosineSimilarity (ReadOnlySpan<float> x, ReadOnlySpan<float> y);
static member CosineSimilarity : ReadOnlySpan<single> * ReadOnlySpan<single> -> single
Public Shared Function CosineSimilarity (x As ReadOnlySpan(Of Single), y As ReadOnlySpan(Of Single)) As Single

Parameters

x
ReadOnlySpan<Single>

The first tensor, represented as a span.

y
ReadOnlySpan<Single>

The second tensor, represented as a span.

Returns

The cosine similarity of the two tensors.

Exceptions

x and y must not be empty.

Remarks

This method effectively computes TensorPrimitives.Dot(x, y) / (MathF.Sqrt(TensorPrimitives.SumOfSquares(x)) * MathF.Sqrt(TensorPrimitives.SumOfSquares(y)).

If any element in either input tensor is equal to NegativeInfinity, PositiveInfinity, or NaN, NaN is returned.

This method may call into the underlying C runtime or employ instructions specific to the current architecture. Exact results may differ between different operating systems or architectures.

Applies to

CosineSimilarity<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Source:
TensorPrimitives.CosineSimilarity.cs
Source:
TensorPrimitives.CosineSimilarity.cs

Computes the cosine similarity between the two specified non-empty, equal-length tensors of numbers.

public:
generic <typename T>
 where T : System::Numerics::IRootFunctions<T> static T CosineSimilarity(ReadOnlySpan<T> x, ReadOnlySpan<T> y);
public static T CosineSimilarity<T> (ReadOnlySpan<T> x, ReadOnlySpan<T> y) where T : System.Numerics.IRootFunctions<T>;
static member CosineSimilarity : ReadOnlySpan<'T (requires 'T :> System.Numerics.IRootFunctions<'T>)> * ReadOnlySpan<'T (requires 'T :> System.Numerics.IRootFunctions<'T>)> -> 'T (requires 'T :> System.Numerics.IRootFunctions<'T>)
Public Shared Function CosineSimilarity(Of T As IRootFunctions(Of T)) (x As ReadOnlySpan(Of T), y As ReadOnlySpan(Of T)) As T

Type Parameters

T

Parameters

x
ReadOnlySpan<T>

The first tensor, represented as a span.

y
ReadOnlySpan<T>

The second tensor, represented as a span.

Returns

T

The cosine similarity of the two tensors.

Exceptions

x and y must not be empty.

Remarks

This method effectively computes TensorPrimitives.Dot(x, y) / (T.Sqrt(TensorPrimitives.SumOfSquares(x)) * T.Sqrt(TensorPrimitives.SumOfSquares(y)).

If any element in either input tensor is equal to NegativeInfinity, PositiveInfinity, or NaN, NaN is returned.

This method may call into the underlying C runtime or employ instructions specific to the current architecture. Exact results may differ between different operating systems or architectures.

Applies to