cos_sim¶
- paddle.fluid.layers.nn. cos_sim ( X, Y ) [source]
-
Cosine Similarity Operator
$Out = frac{X^T * Y}{(sqrt{X^T * X} * sqrt{Y^T * Y})}$
The input X and Y must have the same shape, except that the 1st dimension of input Y could be just 1 (different from input X), which will be broadcasted to match the shape of input X before computing their cosine similarity.
- Parameters
-
X (Tensor) – The 1st input of cos_sim op, Tensor with shape
[N_1, N_2, ..., N_k]
, the data type is float32.Y (Tensor) – The 2nd input of cos_sim op, Tensor with shape
[N_1 or 1, N_2, ..., N_k]
, the data type is float32.
- Returns
-
A Tensor representing the output of cosine(X, Y).
Examples
import paddle x = paddle.rand(shape=[3, 7], dtype='float32') y = paddle.rand(shape=[1, 7], dtype='float32') out = paddle.fluid.layers.cos_sim(x, y) print(out)