-
Notifications
You must be signed in to change notification settings - Fork 75.3k
Expand file tree
/
Copy pathvariable_utils_test.py
More file actions
116 lines (96 loc) · 4.19 KB
/
variable_utils_test.py
File metadata and controls
116 lines (96 loc) · 4.19 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
# Copyright 2022 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 variable_utils."""
from tensorflow.python.eager import context
from tensorflow.python.framework import composite_tensor
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor
from tensorflow.python.framework import test_util
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.util import nest
from tensorflow.python.util import variable_utils
class CT(composite_tensor.CompositeTensor):
"""A generic CompositeTensor, used for constructing tests."""
@property
def _type_spec(self):
pass
class CT2(composite_tensor.CompositeTensor):
"""Another CompositeTensor, used for constructing tests."""
def __init__(self, component):
self.component = component
@property
def _type_spec(self):
pass
def _convert_variables_to_tensors(self):
return CT2(ops.convert_to_tensor(self.component))
@test_util.run_all_in_graph_and_eager_modes
class VariableUtilsTest(test.TestCase):
def test_convert_variables_to_tensors(self):
ct = CT()
data = [resource_variable_ops.ResourceVariable(1),
resource_variable_ops.ResourceVariable(2),
constant_op.constant(3),
[4],
5,
ct]
if not context.executing_eagerly():
self.evaluate(variables.global_variables_initializer())
results = variable_utils.convert_variables_to_tensors(data)
expected_results = [1, 2, 3, [4], 5, ct]
# Only ResourceVariables are converted to Tensors.
self.assertIsInstance(results[0], tensor.Tensor)
self.assertIsInstance(results[1], tensor.Tensor)
self.assertIsInstance(results[2], tensor.Tensor)
self.assertIsInstance(results[3], list)
self.assertIsInstance(results[4], int)
self.assertIs(results[5], ct)
results[:3] = self.evaluate(results[:3])
self.assertAllEqual(results, expected_results)
def test_convert_variables_in_composite_tensor(self):
ct2 = CT2(resource_variable_ops.ResourceVariable(1))
if not context.executing_eagerly():
self.evaluate(variables.global_variables_initializer())
self.assertIsInstance(ct2.component,
resource_variable_ops.ResourceVariable)
result = variable_utils.convert_variables_to_tensors(ct2)
self.assertIsInstance(result.component, tensor.Tensor)
self.assertAllEqual(result.component, 1)
def test_replace_variables_with_atoms(self):
data = [resource_variable_ops.ResourceVariable(1),
resource_variable_ops.ResourceVariable(2),
constant_op.constant(3),
[4],
5]
if not context.executing_eagerly():
self.evaluate(variables.global_variables_initializer())
results = variable_utils.replace_variables_with_atoms(data)
expected_results = [0, 0, 3, [4], 5]
# Only ResourceVariables are replaced with int 0s.
self.assertIsInstance(results[0], int)
self.assertIsInstance(results[1], int)
self.assertIsInstance(results[2], tensor.Tensor)
self.assertIsInstance(results[3], list)
self.assertIsInstance(results[4], int)
results[2] = self.evaluate(results[2])
self.assertAllEqual(results, expected_results)
# Make sure 0 is a tf.nest atom with expand_composites=True.
flat_results = nest.flatten(results, expand_composites=True)
expected_flat_results = [0, 0, 3, 4, 5]
self.assertAllEqual(flat_results, expected_flat_results)
if __name__ == "__main__":
test.main()