| Files | Last commit | Last update |
|---|---|---|
| 4 months ago | ||
| 3 months ago | ||
| 1 month ago | ||
| 2 months ago | ||
| 2 months ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 16 days ago |
CosineSimilarity
English | 简体中文
Product Support Status
| Product | Supported |
|---|---|
| Ascend 950PR&950DT products | √ |
| Atlas A3 products | √ |
| Atlas A2 products | √ |
| Atlas 200I/500 A2 inference products | × |
| Atlas inference products | × |
| Atlas training products | × |
Function Description
-
Operator function: computes the cosine similarity of two input tensors along the specified dimension. The cosine similarity measures the cosine of the angle between two vectors, falls within the range [-1, 1], and is commonly used to measure how similar two vectors are.
-
Computation formula:
cosine_similarity(x1,x2,dim,eps)=∑(x1⋅x2,dim)max(∑(x12,dim),eps)⋅max(∑(x22,dim),eps)\text{cosine\_similarity}(x_1, x_2, \text{dim}, \text{eps}) = \frac{\sum(x_1 \cdot x_2, \text{dim})}{\max(\sqrt{\sum(x_1^2, \text{dim})}, \text{eps}) \cdot \max(\sqrt{\sum(x_2^2, \text{dim})}, \text{eps})}
Parameter Description
| Parameter Name | Input/Output/Attribute | Description | Data Type | Data Format |
|---|---|---|---|---|
| input_x1 | Input | The first input tensor, whose rank ranges from 1 to 8. | FLOAT | ND |
| input_x2 | Input | The second input tensor, which must have exactly the same shape as input_x1. | FLOAT | ND |
| output_y | Output | The output tensor after reduction along the dim dimension. Its shape is the input shape with the dimension corresponding to dim removed. | FLOAT | ND |
| dim | Attribute | The dimension along which the cosine similarity is computed. The value range is [-rank, rank-1], and the default value is 1. | INT32 | - |
| eps | Attribute | A non-negative numerical stability parameter that prevents division by zero. The default value is 1e-8. | FLOAT | - |
Constraint Description
- Only the FLOAT (float32) data type is supported for the inputs and output.
- The two inputs must have the same rank, and the rank ranges from 1 to 8. 0D tensors are not supported.
- Each dimension of the two inputs must have the same length, that is,
input_x1.shape == input_x2.shape. - No form of broadcasting is supported, including expanding dimensions of length 1 and left-padding 1s for inputs of different ranks.
- A shape that contains a dimension of length 0 is not supported.
dimmust be within the[-rank, rank-1]range for the input rank. A negative index is processed as the corresponding positive dimension, for example,dim=-1indicates the last dimension.- The output shape is the common shape of the two inputs with the dimension corresponding to
dimremoved. epsmust be a non-negative value.- If both inputs have a rank of 1,
dimmust be explicitly specified within the[-1, 0]range.
Invocation Description
| Invocation Method | Sample Code | Description |
|---|---|---|
| Graph-mode invocation | test_geir_cosine_similarity | Invokes the CosineSimilarity operator by building a graph through the operator IR |