forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_grad_test.py
More file actions
131 lines (98 loc) · 3.82 KB
/
array_grad_test.py
File metadata and controls
131 lines (98 loc) · 3.82 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
# Copyright 2021 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 array_grad."""
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gradient_checker_v2
from tensorflow.python.platform import test
@test_util.with_eager_op_as_function
@test_util.run_all_in_graph_and_eager_modes
class ArrayGradTest(test.TestCase):
def _testGrad(self, f, x):
max_error = gradient_checker_v2.max_error(
*gradient_checker_v2.compute_gradient(f, [x]))
self.assertLess(max_error, 1e-4)
def test_gather_v2_simple(self):
x = constant_op.constant([1., 2., 3., 4., 5.], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x, constant_op.constant([2, 0, 2, 4], dtype=dtypes.int32))
self._testGrad(f, x)
def test_gather_v2_more_index_dims(self):
x = constant_op.constant([1., 2., 3., 4., 5.], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x, constant_op.constant([[2, 0], [2, 4]], dtype=dtypes.int32))
self._testGrad(f, x)
def test_gather_v2_more_param_dims(self):
x = constant_op.constant([[1., 2.], [3., 4.]], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x, constant_op.constant([1, 0], dtype=dtypes.int32))
self._testGrad(f, x)
def test_gather_v2_axis(self):
x = constant_op.constant([[1., 2.], [3., 4.]], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x, constant_op.constant([1, 0], dtype=dtypes.int32), axis=1)
self._testGrad(f, x)
def test_gather_v2_batch_dims(self):
x = constant_op.constant([[1., 2.], [3., 4.]], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x,
constant_op.constant([[1, 0], [0, 0]], dtype=dtypes.int32),
axis=1,
batch_dims=1)
self._testGrad(f, x)
def test_gather_v2_2batch_dims(self):
x = constant_op.constant([[[1., 2.], [3., 4.]]], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x,
constant_op.constant([[[1, 0], [0, 0]]], dtype=dtypes.int32),
axis=2,
batch_dims=2)
self._testGrad(f, x)
def test_gather_v2_batch_dims_with_axis(self):
x = constant_op.constant([[[1., 2.]], [[3., 4.]]], dtype=dtypes.float64)
def f(x):
return array_ops.gather_v2(
x,
constant_op.constant([[0], [0]], dtype=dtypes.int32),
axis=2,
batch_dims=1)
self._testGrad(f, x)
def test_broadcast_to(self):
x = constant_op.constant([1., 2., 3.], dtype=dtypes.float64)
y = constant_op.constant([2, 3], dtype=dtypes.int32)
def f(x):
return array_ops.broadcast_to(
x,
y)
self._testGrad(f, x)
@test_util.disable_xla("b/206689921") # XLA does not support DT_INT64
def test_broadcast_to_int64(self):
x = constant_op.constant([1., 2., 3.], dtype=dtypes.float64)
y = constant_op.constant([2, 3], dtype=dtypes.int64)
def f(x):
return array_ops.broadcast_to(
x,
y)
self._testGrad(f, x)
if __name__ == "__main__":
test.main()