forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack_op_test.py
More file actions
201 lines (173 loc) · 7.73 KB
/
Copy pathpack_op_test.py
File metadata and controls
201 lines (173 loc) · 7.73 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
# Copyright 2015 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.
# ==============================================================================
"""Functional tests for Pack Op."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
class PackOpTest(tf.test.TestCase):
def testSimple(self):
np.random.seed(7)
for use_gpu in False, True:
with self.test_session(use_gpu=use_gpu):
for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2):
data = np.random.randn(*shape)
# Convert [data[0], data[1], ...] separately to tensorflow
# TODO(irving): Remove list() once we handle maps correctly
xs = list(map(tf.constant, data))
# Pack back into a single tensorflow tensor
c = tf.pack(xs)
self.assertAllEqual(c.eval(), data)
def testConst(self):
np.random.seed(7)
for use_gpu in False, True:
with self.test_session(use_gpu=use_gpu):
for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2):
data = np.random.randn(*shape).astype(np.float32)
# Pack back into a single tensorflow tensor directly using np array
c = tf.pack(data)
# This is implemented via a Const:
self.assertEqual(c.op.type, "Const")
self.assertAllEqual(c.eval(), data)
# Python lists also work for 1-D case:
if len(shape) == 1:
data_list = list(data)
cl = tf.pack(data_list)
self.assertEqual(cl.op.type, "Const")
self.assertAllEqual(cl.eval(), data)
# Verify that shape induction works with shapes produced via const pack
a = tf.constant([1, 2, 3, 4, 5, 6])
b = tf.reshape(a, tf.pack([2, 3]))
self.assertAllEqual(b.get_shape(), [2, 3])
def testGradients(self):
np.random.seed(7)
for use_gpu in False, True:
for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2):
data = np.random.randn(*shape)
shapes = [shape[1:]] * shape[0]
with self.test_session(use_gpu=use_gpu):
# TODO(irving): Remove list() once we handle maps correctly
xs = list(map(tf.constant, data))
c = tf.pack(xs)
err = tf.test.compute_gradient_error(xs, shapes, c, shape)
self.assertLess(err, 1e-6)
def testZeroSize(self):
# Verify that pack doesn't crash for zero size inputs
for use_gpu in False, True:
with self.test_session(use_gpu=use_gpu):
for shape in (0,), (3,0), (0, 3):
x = np.zeros((2,) + shape)
p = tf.pack(list(x)).eval()
self.assertAllEqual(p, x)
class AutomaticPackingTest(tf.test.TestCase):
def testSimple(self):
with self.test_session():
self.assertAllEqual([1, 0, 2],
tf.convert_to_tensor([1, tf.constant(0), 2]).eval())
self.assertAllEqual(
[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
tf.convert_to_tensor([[0, 0, 0],
[0, tf.constant(1), 0],
[0, 0, 0]]).eval())
self.assertAllEqual(
[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
tf.convert_to_tensor([[0, 0, 0],
tf.constant([0, 1, 0]),
[0, 0, 0]]).eval())
self.assertAllEqual(
[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
tf.convert_to_tensor([tf.constant([0, 0, 0]),
tf.constant([0, 1, 0]),
tf.constant([0, 0, 0])]).eval())
def testWithNDArray(self):
with self.test_session():
result = tf.convert_to_tensor([[[0., 0.],
tf.constant([1., 1.])],
np.array([[2., 2.], [3., 3.]],
dtype=np.float32)])
self.assertAllEqual(
[[[0., 0.], [1., 1.]], [[2., 2.], [3., 3.]]], result.eval())
def testVariable(self):
with self.test_session():
v = tf.Variable(17)
result = tf.convert_to_tensor([[0, 0, 0],
[0, v, 0],
[0, 0, 0]])
v.initializer.run()
self.assertAllEqual([[0, 0, 0], [0, 17, 0], [0, 0, 0]], result.eval())
v.assign(38).op.run()
self.assertAllEqual([[0, 0, 0], [0, 38, 0], [0, 0, 0]], result.eval())
def testDtype(self):
t_0 = tf.convert_to_tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
self.assertEqual(tf.float32, t_0.dtype)
t_1 = tf.convert_to_tensor([[0., 0., 0.],
tf.constant([0., 0., 0.], dtype=tf.float64),
[0., 0., 0.]])
self.assertEqual(tf.float64, t_1.dtype)
t_2 = tf.convert_to_tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=tf.float64)
self.assertEqual(tf.float64, t_2.dtype)
with self.assertRaises(TypeError):
tf.convert_to_tensor([tf.constant([0., 0., 0.], dtype=tf.float32),
tf.constant([0., 0., 0.], dtype=tf.float64),
[0., 0., 0.]])
with self.assertRaises(TypeError):
tf.convert_to_tensor([[0., 0., 0.],
tf.constant([0., 0., 0.], dtype=tf.float64),
[0., 0., 0.]], dtype=tf.float32)
with self.assertRaises(TypeError):
tf.convert_to_tensor([tf.constant([0., 0., 0.], dtype=tf.float64)],
dtype=tf.float32)
def testPlaceholder(self):
with self.test_session():
# Test using placeholder with a defined shape.
ph_0 = tf.placeholder(tf.int32, shape=[])
result_0 = tf.convert_to_tensor([[0, 0, 0],
[0, ph_0, 0],
[0, 0, 0]])
self.assertAllEqual([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]],
result_0.eval(feed_dict={ph_0: 1}))
self.assertAllEqual([[0, 0, 0],
[0, 2, 0],
[0, 0, 0]],
result_0.eval(feed_dict={ph_0: 2}))
# Test using placeholder with an undefined shape.
ph_1 = tf.placeholder(tf.int32)
result_1 = tf.convert_to_tensor([[0, 0, 0],
[0, ph_1, 0],
[0, 0, 0]])
self.assertAllEqual([[0, 0, 0], [0, 1, 0], [0, 0, 0]],
result_1.eval(feed_dict={ph_1: 1}))
self.assertAllEqual([[0, 0, 0], [0, 2, 0], [0, 0, 0]],
result_1.eval(feed_dict={ph_1: 2}))
def testShapeErrors(self):
# Static shape error.
ph_0 = tf.placeholder(tf.int32, shape=[1])
with self.assertRaises(ValueError):
tf.convert_to_tensor([[0, 0, 0], [0, ph_0, 0], [0, 0, 0]])
# Dynamic shape error.
ph_1 = tf.placeholder(tf.int32)
result_1 = tf.convert_to_tensor([[0, 0, 0], [0, ph_1, 0], [0, 0, 0]])
with self.test_session():
with self.assertRaises(tf.errors.InvalidArgumentError):
result_1.eval(feed_dict={ph_1: [1]})
if __name__ == "__main__":
tf.test.main()