Repository navigation
Expand file tree
/
Copy pathragged_util_test.py
More file actions
228 lines (205 loc) · 8.07 KB
/
Copy pathragged_util_test.py
File metadata and controls
228 lines (205 loc) · 8.07 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
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
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for ragged_util."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl.testing import parameterized
import numpy as np
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops.ragged import ragged_util
from tensorflow.python.platform import googletest
# Example 3d tensor for test cases. Has shape [4, 2, 3].
TENSOR_3D = [[[('%d%d%d' % (i, j, k)).encode('utf-8')
for k in range(3)]
for j in range(2)]
for i in range(4)]
# Example 4d tensor for test cases. Has shape [4, 2, 3, 5].
TENSOR_4D = [[[[('%d%d%d%d' % (i, j, k, l)).encode('utf-8')
for l in range(5)]
for k in range(3)]
for j in range(2)]
for i in range(4)]
@test_util.run_all_in_graph_and_eager_modes
class RaggedUtilTest(test_util.TensorFlowTestCase,
parameterized.TestCase):
@parameterized.parameters([
# Docstring examples
dict(
data=['a', 'b', 'c'],
repeats=[3, 0, 2],
axis=0,
expected=[b'a', b'a', b'a', b'c', b'c']),
dict(
data=[[1, 2], [3, 4]],
repeats=[2, 3],
axis=0,
expected=[[1, 2], [1, 2], [3, 4], [3, 4], [3, 4]]),
dict(
data=[[1, 2], [3, 4]],
repeats=[2, 3],
axis=1,
expected=[[1, 1, 2, 2, 2], [3, 3, 4, 4, 4]]),
# Scalar repeats value
dict(
data=['a', 'b', 'c'],
repeats=2,
axis=0,
expected=[b'a', b'a', b'b', b'b', b'c', b'c']),
dict(
data=[[1, 2], [3, 4]],
repeats=2,
axis=0,
expected=[[1, 2], [1, 2], [3, 4], [3, 4]]),
dict(
data=[[1, 2], [3, 4]],
repeats=2,
axis=1,
expected=[[1, 1, 2, 2], [3, 3, 4, 4]]),
# data & repeats are broadcast to have at least one dimension,
# so these are all equivalent:
dict(data=3, repeats=4, axis=0, expected=[3, 3, 3, 3]),
dict(data=[3], repeats=4, axis=0, expected=[3, 3, 3, 3]),
dict(data=3, repeats=[4], axis=0, expected=[3, 3, 3, 3]),
dict(data=[3], repeats=[4], axis=0, expected=[3, 3, 3, 3]),
# Empty tensor
dict(data=[], repeats=[], axis=0, expected=[]),
])
def testRepeat(self, data, repeats, expected, axis=None):
result = ragged_util.repeat(data, repeats, axis)
self.assertAllEqual(result, expected)
@parameterized.parameters([
dict(mode=mode, **args)
for mode in ['constant', 'dynamic', 'unknown_shape']
for args in [
# data & repeats are broadcast to have at least one dimension,
# so these are all equivalent:
dict(data=3, repeats=4, axis=0),
dict(data=[3], repeats=4, axis=0),
dict(data=3, repeats=[4], axis=0),
dict(data=[3], repeats=[4], axis=0),
# 1-dimensional data tensor.
dict(data=[], repeats=5, axis=0),
dict(data=[1, 2, 3], repeats=5, axis=0),
dict(data=[1, 2, 3], repeats=[3, 0, 2], axis=0),
dict(data=[1, 2, 3], repeats=[3, 0, 2], axis=-1),
dict(data=[b'a', b'b', b'c'], repeats=[3, 0, 2], axis=0),
# 2-dimensional data tensor.
dict(data=[[1, 2, 3], [4, 5, 6]], repeats=3, axis=0),
dict(data=[[1, 2, 3], [4, 5, 6]], repeats=3, axis=1),
dict(data=[[1, 2, 3], [4, 5, 6]], repeats=[3, 5], axis=0),
dict(data=[[1, 2, 3], [4, 5, 6]], repeats=[3, 5, 7], axis=1),
# 3-dimensional data tensor: shape=[4, 2, 3].
dict(data=TENSOR_3D, repeats=2, axis=0),
dict(data=TENSOR_3D, repeats=2, axis=1),
dict(data=TENSOR_3D, repeats=2, axis=2),
dict(data=TENSOR_3D, repeats=[2, 0, 4, 1], axis=0),
dict(data=TENSOR_3D, repeats=[3, 2], axis=1),
dict(data=TENSOR_3D, repeats=[1, 3, 1], axis=2),
# 4-dimensional data tensor: shape=[4, 2, 3, 5].
dict(data=TENSOR_4D, repeats=2, axis=0),
dict(data=TENSOR_4D, repeats=2, axis=1),
dict(data=TENSOR_4D, repeats=2, axis=2),
dict(data=TENSOR_4D, repeats=2, axis=3),
dict(data=TENSOR_4D, repeats=[2, 0, 4, 1], axis=0),
dict(data=TENSOR_4D, repeats=[3, 2], axis=1),
dict(data=TENSOR_4D, repeats=[1, 3, 1], axis=2),
dict(data=TENSOR_4D, repeats=[1, 3, 0, 0, 2], axis=3),
]
])
def testValuesMatchesNumpy(self, mode, data, repeats, axis):
# Exception: we can't handle negative axis if data.ndims is unknown.
if axis < 0 and mode == 'unknown_shape':
return
expected = np.repeat(data, repeats, axis)
if mode == 'constant':
data = constant_op.constant(data)
repeats = constant_op.constant(repeats)
elif mode == 'dynamic':
data = constant_op.constant(data)
repeats = constant_op.constant(repeats)
data = array_ops.placeholder_with_default(data, data.shape)
repeats = array_ops.placeholder_with_default(repeats, repeats.shape)
elif mode == 'unknown_shape':
data = array_ops.placeholder_with_default(data, None)
repeats = array_ops.placeholder_with_default(repeats, None)
result = ragged_util.repeat(data, repeats, axis)
self.assertAllEqual(result, expected)
@parameterized.parameters([
dict(
descr='axis >= rank(data)',
mode='dynamic',
data=[1, 2, 3],
repeats=[3, 0, 2],
axis=1,
error='axis=1 out of bounds: expected -1<=axis<1'),
dict(
descr='axis < -rank(data)',
mode='dynamic',
data=[1, 2, 3],
repeats=[3, 0, 2],
axis=-2,
error='axis=-2 out of bounds: expected -1<=axis<1'),
dict(
descr='len(repeats) != data.shape[axis]',
mode='dynamic',
data=[[1, 2, 3], [4, 5, 6]],
repeats=[2, 3],
axis=1,
error='Dimensions 3 and 2 are not compatible'),
dict(
descr='rank(repeats) > 1',
mode='dynamic',
data=[[1, 2, 3], [4, 5, 6]],
repeats=[[3], [5]],
axis=1,
error=r'Shape \(2, 1\) must have rank at most 1'),
dict(
descr='non-integer axis',
mode='constant',
data=[1, 2, 3],
repeats=2,
axis='foo',
exception=TypeError,
error='axis must be an int'),
])
def testError(self,
descr,
mode,
data,
repeats,
axis,
exception=ValueError,
error=None):
# Make sure that this is also an error case for numpy.
with self.assertRaises(exception):
np.repeat(data, repeats, axis)
if mode == 'constant':
data = constant_op.constant(data)
repeats = constant_op.constant(repeats)
elif mode == 'dynamic':
data = constant_op.constant(data)
repeats = constant_op.constant(repeats)
data = array_ops.placeholder_with_default(data, data.shape)
repeats = array_ops.placeholder_with_default(repeats, repeats.shape)
elif mode == 'unknown_shape':
data = array_ops.placeholder_with_default(data, None)
repeats = array_ops.placeholder_with_default(repeats, None)
with self.assertRaisesRegexp(exception, error):
ragged_util.repeat(data, repeats, axis)
if __name__ == '__main__':
googletest.main()