-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathpaddle.py
More file actions
63 lines (49 loc) · 2.06 KB
/
paddle.py
File metadata and controls
63 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import TYPE_CHECKING
import paddle
if TYPE_CHECKING: # pragma: no cover
from paddle import tensor
import numpy
def cosine(
x_mat: 'tensor', y_mat: 'tensor', eps: float = 1e-7, device: str = 'cpu'
) -> 'numpy.ndarray':
"""Cosine distance between each row in x_mat and each row in y_mat.
:param x_mat: np.ndarray with ndim=2
:param y_mat: np.ndarray with ndim=2
:param eps: a small jitter to avoid divde by zero
:param device: the computational device for `embed_model`, can be either `cpu` or `cuda`.
:return: np.ndarray with ndim=2
"""
paddle.set_device(device)
a_n, b_n = x_mat.norm(axis=1)[:, None], y_mat.norm(axis=1)[:, None]
a_norm = x_mat / paddle.clip(a_n, min=eps)
b_norm = y_mat / paddle.clip(b_n, min=eps)
sim_mt = 1 - paddle.mm(a_norm, b_norm.transpose(perm=[1, 0]))
return sim_mt.numpy()
def sqeuclidean(
x_mat: 'tensor', y_mat: 'tensor', device: str = 'cpu'
) -> 'numpy.ndarray':
"""Squared euclidean distance between each row in x_mat and each row in y_mat.
:param x_mat: paddle array with ndim=2
:param y_mat: paddle array with ndim=2
:param device: the computational device for `embed_model`, can be either `cpu` or `cuda`.
:return: np.ndarray with ndim=2
"""
paddle.set_device(device)
return (
paddle.sum(y_mat**2, axis=1)
+ paddle.sum(x_mat**2, axis=1)[:, None]
- 2 * paddle.mm(x_mat, y_mat.transpose(perm=[1, 0]))
).numpy()
def euclidean(x_mat: 'tensor', y_mat: 'tensor', device: str = 'cpu') -> 'numpy.ndarray':
"""Euclidean distance between each row in x_mat and each row in y_mat.
:param x_mat: paddle array with ndim=2
:param y_mat: paddle array with ndim=2
:param device: the computational device for `embed_model`, can be either `cpu` or `cuda`.
:return: np.ndarray with ndim=2
"""
paddle.set_device(device)
return paddle.sqrt(
paddle.sum(y_mat**2, axis=1)
+ paddle.sum(x_mat**2, axis=1)[:, None]
- 2 * paddle.mm(x_mat, y_mat.transpose(perm=[1, 0]))
).numpy()