TensorPrimitives.CosineSimilarity Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
The first tensor, represented as a span.
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>)
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
The first tensor, represented as a span.
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) / (
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.