FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docarray/docarray/computation/tensorflow_backend.py at fix-num-docs · docarray/docarray · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
docarray
/
docarray
Public
Notifications
You must be signed in to change notification settings
Fork
243
Star
3.1k
Code
Issues
68
Pull requests
43
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
docarray
/
docarray
/
computation
/
tensorflow_backend.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
303 lines (249 loc) · 11.7 KB
Breadcrumbs
docarray
/
docarray
/
computation
/
tensorflow_backend.py
Copy path
File metadata and controls
303 lines (249 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import
typing
from
typing
import
TYPE_CHECKING
,
Callable
,
List
,
Optional
,
Tuple
import
numpy
as
np
from
docarray
.
computation
import
AbstractComputationalBackend
from
docarray
.
computation
.
abstract_numpy_based_backend
import
AbstractNumpyBasedBackend
from
docarray
.
typing
import
TensorFlowTensor
from
docarray
.
utils
.
_internal
.
misc
import
import_library
if
TYPE_CHECKING
:
import
tensorflow
as
tf
# type: ignore
import
tensorflow
.
_api
.
v2
.
experimental
.
numpy
as
tnp
# type: ignore
else
:
tf
=
import_library
(
'tensorflow'
,
raise_error
=
True
)
tnp
=
tf
.
_api
.
v2
.
experimental
.
numpy
def
_unsqueeze_if_single_axis
(
*
matrices
:
tf
.
Tensor
)
->
List
[
tf
.
Tensor
]:
"""
Unsqueezes tensors that only have one axis, at dim 0.
This ensures that all outputs can be treated as matrices, not vectors.
:param matrices: Matrices to be unsqueezed
:return: List of the input matrices,
where single axis matrices are unsqueezed at dim 0.
"""
unsqueezed
=
[]
for
m
in
matrices
:
if
len
(
m
.
shape
)
==
1
:
unsqueezed
.
append
(
tf
.
expand_dims
(
m
,
axis
=
0
))
else
:
unsqueezed
.
append
(
m
)
return
unsqueezed
def
_unsqueeze_if_scalar
(
t
:
tf
.
Tensor
)
->
tf
.
Tensor
:
"""
Unsqueezes tensor of a scalar, from shape () to shape (1,).
:param t: tensor to unsqueeze.
:return: unsqueezed tf.Tensor
"""
if
len
(
t
.
shape
)
==
0
:
# avoid scalar output
t
=
tf
.
expand_dims
(
t
,
0
)
return
t
def
norm_left
(
t
:
tf
.
Tensor
)
->
TensorFlowTensor
:
return
TensorFlowTensor
(
tensor
=
t
)
def
norm_right
(
t
:
TensorFlowTensor
)
->
tf
.
Tensor
:
return
t
.
tensor
class
TensorFlowCompBackend
(
AbstractNumpyBasedBackend
[
TensorFlowTensor
]):
"""
Computational backend for TensorFlow.
"""
_module
=
tnp
_cast_output
:
Callable
=
norm_left
_get_tensor
:
Callable
=
norm_right
@
classmethod
def
to_numpy
(
cls
,
array
:
'TensorFlowTensor'
)
->
'np.ndarray'
:
return
cls
.
_get_tensor
(
array
).
numpy
()
@
classmethod
def
none_value
(
cls
)
->
typing
.
Any
:
"""Provide a compatible value that represents None in numpy."""
return
tf
.
constant
(
float
(
'nan'
))
@
classmethod
def
to_device
(
cls
,
tensor
:
'TensorFlowTensor'
,
device
:
str
)
->
'TensorFlowTensor'
:
"""Move the tensor to the specified device."""
if
cls
.
device
(
tensor
)
==
device
:
return
tensor
else
:
with
tf
.
device
(
device
):
return
cls
.
_cast_output
(
tf
.
identity
(
cls
.
_get_tensor
(
tensor
)))
@
classmethod
def
device
(
cls
,
tensor
:
'TensorFlowTensor'
)
->
Optional
[
str
]:
"""Return device on which the tensor is allocated."""
return
cls
.
_get_tensor
(
tensor
).
device
@
classmethod
def
detach
(
cls
,
tensor
:
'TensorFlowTensor'
)
->
'TensorFlowTensor'
:
"""
Returns the tensor detached from its current graph.
:param tensor: tensor to be detached
:return: a detached tensor with the same data.
"""
return
cls
.
_cast_output
(
tf
.
stop_gradient
(
cls
.
_get_tensor
(
tensor
)))
@
classmethod
def
dtype
(
cls
,
tensor
:
'TensorFlowTensor'
)
->
tf
.
dtypes
:
"""Get the data type of the tensor."""
d_type
=
cls
.
_get_tensor
(
tensor
).
dtype
return
d_type
.
name
@
classmethod
def
minmax_normalize
(
cls
,
tensor
:
'TensorFlowTensor'
,
t_range
:
Tuple
=
(
0.0
,
1.0
),
x_range
:
Optional
[
Tuple
]
=
None
,
eps
:
float
=
1e-7
,
)
->
'TensorFlowTensor'
:
a
,
b
=
t_range
t
=
tf
.
cast
(
cls
.
_get_tensor
(
tensor
),
tf
.
float32
)
min_d
=
x_range
[
0
]
if
x_range
else
tnp
.
min
(
t
,
axis
=
-
1
,
keepdims
=
True
)
max_d
=
x_range
[
1
]
if
x_range
else
tnp
.
max
(
t
,
axis
=
-
1
,
keepdims
=
True
)
i
=
(
b
-
a
)
*
(
t
-
min_d
)
/
(
max_d
-
min_d
+
tf
.
constant
(
eps
)
+
a
)
normalized
=
tnp
.
clip
(
i
,
*
((
a
,
b
)
if
a
<
b
else
(
b
,
a
)))
return
cls
.
_cast_output
(
tf
.
cast
(
normalized
,
tensor
.
tensor
.
dtype
))
@
classmethod
def
equal
(
cls
,
tensor1
:
'TensorFlowTensor'
,
tensor2
:
'TensorFlowTensor'
)
->
bool
:
"""
Check if two tensors are equal.
:param tensor1: the first tensor
:param tensor2: the second tensor
:return: True if two tensors are equal, False otherwise.
If one or more of the inputs is not a TensorFlowTensor, return False.
"""
t1
,
t2
=
getattr
(
tensor1
,
'tensor'
,
None
),
getattr
(
tensor2
,
'tensor'
,
None
)
if
tf
.
is_tensor
(
t1
)
and
tf
.
is_tensor
(
t2
):
# mypy doesn't know that tf.is_tensor implies that t1, t2 are not None
return
t1
.
shape
==
t2
.
shape
and
tf
.
math
.
reduce_all
(
tf
.
equal
(
t1
,
t1
))
# type: ignore
return
False
class
Retrieval
(
AbstractComputationalBackend
.
Retrieval
[
TensorFlowTensor
]):
"""
Abstract class for retrieval and ranking functionalities
"""
@
staticmethod
def
top_k
(
values
:
'TensorFlowTensor'
,
k
:
int
,
descending
:
bool
=
False
,
device
:
Optional
[
str
]
=
None
,
)
->
Tuple
[
'TensorFlowTensor'
,
'TensorFlowTensor'
]:
"""
Retrieves the top k smallest values in `values`,
and returns them alongside their indices in the input `values`.
Can also be used to retrieve the top k largest values,
by setting the `descending` flag.
:param values: TensorFlowTensor of values to rank.
Should be of shape (n_queries, n_values_per_query).
Inputs of shape (n_values_per_query,) will be expanded
to (1, n_values_per_query).
:param k: number of values to retrieve
:param descending: retrieve largest values instead of smallest values
:param device: the computational device to use.
:return: Tuple of TensorFlowTensors containing the retrieved values, and
their indices. Both are of shape (n_queries, k)
"""
comp_be
=
TensorFlowCompBackend
if
device
is
not
None
:
values
=
comp_be
.
to_device
(
values
,
device
)
tf_values
:
tf
.
Tensor
=
comp_be
.
_get_tensor
(
values
)
if
len
(
tf_values
.
shape
)
<=
1
:
tf_values
=
tf
.
expand_dims
(
tf_values
,
axis
=
0
)
len_tf_values
=
(
tf_values
.
shape
[
-
1
]
if
len
(
tf_values
.
shape
)
>
1
else
len
(
tf_values
)
)
k
=
min
(
k
,
len_tf_values
)
if
not
descending
:
tf_values
=
-
tf_values
result
=
tf
.
math
.
top_k
(
input
=
tf_values
,
k
=
k
,
sorted
=
True
)
res_values
=
result
.
values
res_indices
=
result
.
indices
if
not
descending
:
res_values
=
-
result
.
values
return
comp_be
.
_cast_output
(
res_values
),
comp_be
.
_cast_output
(
res_indices
)
class
Metrics
(
AbstractComputationalBackend
.
Metrics
[
TensorFlowTensor
]):
"""
Abstract base class for metrics (distances and similarities).
"""
@
staticmethod
def
cosine_sim
(
x_mat
:
'TensorFlowTensor'
,
y_mat
:
'TensorFlowTensor'
,
eps
:
float
=
1e-7
,
device
:
Optional
[
str
]
=
None
,
)
->
'TensorFlowTensor'
:
"""Pairwise cosine similarities between all vectors in x_mat and y_mat.
:param x_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each example.
:param y_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each example.
:param eps: a small jitter to avoid divde by zero
:param device: the device to use for computations.
If not provided, the devices of x_mat and y_mat are used.
:return: Tensor of shape (n_vectors, n_vectors) containing all pairwise
cosine distances.
The index [i_x, i_y] contains the cosine distance between
x_mat[i_x] and y_mat[i_y].
"""
comp_be
=
TensorFlowCompBackend
x_mat_tf
:
tf
.
Tensor
=
comp_be
.
_get_tensor
(
x_mat
)
y_mat_tf
:
tf
.
Tensor
=
comp_be
.
_get_tensor
(
y_mat
)
with
tf
.
device
(
device
):
x_mat_tf
=
tf
.
identity
(
x_mat_tf
)
y_mat_tf
=
tf
.
identity
(
y_mat_tf
)
x_mat_tf
,
y_mat_tf
=
_unsqueeze_if_single_axis
(
x_mat_tf
,
y_mat_tf
)
a_n
=
tf
.
linalg
.
normalize
(
x_mat_tf
,
axis
=
1
)[
1
]
b_n
=
tf
.
linalg
.
normalize
(
y_mat_tf
,
axis
=
1
)[
1
]
a_norm
=
x_mat_tf
/
tf
.
clip_by_value
(
a_n
,
clip_value_min
=
eps
,
clip_value_max
=
tf
.
float32
.
max
)
b_norm
=
y_mat_tf
/
tf
.
clip_by_value
(
b_n
,
clip_value_min
=
eps
,
clip_value_max
=
tf
.
float32
.
max
)
sims
=
tf
.
squeeze
(
tf
.
linalg
.
matmul
(
a_norm
,
tf
.
transpose
(
b_norm
)))
sims
=
_unsqueeze_if_scalar
(
sims
)
return
comp_be
.
_cast_output
(
sims
)
@
staticmethod
def
euclidean_dist
(
x_mat
:
'TensorFlowTensor'
,
y_mat
:
'TensorFlowTensor'
,
device
:
Optional
[
str
]
=
None
,
)
->
'TensorFlowTensor'
:
"""Pairwise Euclidian distances between all vectors in x_mat and y_mat.
:param x_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each example.
:param y_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each example.
:param device: the device to use for pytorch computations.
If not provided, the devices of x_mat and y_mat are used.
:return: Tensor of shape (n_vectors, n_vectors) containing all pairwise
euclidian distances.
The index [i_x, i_y] contains the euclidian distance between
x_mat[i_x] and y_mat[i_y].
"""
comp_be
=
TensorFlowCompBackend
x_mat_tf
:
tf
.
Tensor
=
comp_be
.
_get_tensor
(
x_mat
)
y_mat_tf
:
tf
.
Tensor
=
comp_be
.
_get_tensor
(
y_mat
)
with
tf
.
device
(
device
):
x_mat_tf
=
tf
.
identity
(
x_mat_tf
)
y_mat_tf
=
tf
.
identity
(
y_mat_tf
)
x_mat_tf
,
y_mat_tf
=
_unsqueeze_if_single_axis
(
x_mat_tf
,
y_mat_tf
)
dists
=
tf
.
squeeze
(
tf
.
norm
(
tf
.
subtract
(
x_mat_tf
,
y_mat_tf
),
axis
=
-
1
))
dists
=
_unsqueeze_if_scalar
(
dists
)
return
comp_be
.
_cast_output
(
dists
)
@
staticmethod
def
sqeuclidean_dist
(
x_mat
:
'TensorFlowTensor'
,
y_mat
:
'TensorFlowTensor'
,
device
:
Optional
[
str
]
=
None
,
)
->
'TensorFlowTensor'
:
"""Pairwise Squared Euclidian distances between all vectors
in x_mat and y_mat.
:param x_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each
example.
:param y_mat: tensor of shape (n_vectors, n_dim), where n_vectors is the
number of vectors and n_dim is the number of dimensions of each
example.
:param device: the device to use for pytorch computations.
If not provided, the devices of x_mat and y_mat are used.
:return: Tensor of shape (n_vectors, n_vectors) containing all pairwise
euclidian distances.
The index [i_x, i_y] contains the euclidian distance between
x_mat[i_x] and y_mat[i_y].
"""
dists
=
TensorFlowCompBackend
.
Metrics
.
euclidean_dist
(
x_mat
,
y_mat
)
squared
:
tf
.
Tensor
=
tf
.
math
.
square
(
TensorFlowCompBackend
.
_get_tensor
(
dists
)
)
return
TensorFlowCompBackend
.
_cast_output
(
squared
)
Back
|
FazBrowse Home
|
New Git URL