aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/constant_op_test.py
blob: 92f9b5fe4a4b80993d8afb3606e0df80a3cdb628 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"""Tests for ConstantOp."""
import tensorflow.python.platform

import numpy as np
import tensorflow as tf

from tensorflow.python.ops import gen_array_ops


class ConstantTest(tf.test.TestCase):

  def _testCpu(self, x):
    np_ans = np.array(x)
    with self.test_session(use_gpu=False):
      tf_ans = tf.convert_to_tensor(x).eval()
    if np_ans.dtype in [np.float32, np.float64, np.complex64]:
      self.assertAllClose(np_ans, tf_ans)
    else:
      self.assertAllEqual(np_ans, tf_ans)

  def _testGpu(self, x):
    np_ans = np.array(x)
    with self.test_session(use_gpu=True):
      tf_ans = tf.convert_to_tensor(x).eval()
    if np_ans.dtype in [np.float32, np.float64, np.complex64]:
      self.assertAllClose(np_ans, tf_ans)
    else:
      self.assertAllEqual(np_ans, tf_ans)

  def _testAll(self, x):
    self._testCpu(x)
    self._testGpu(x)

  def testFloat(self):
    self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32))
    self._testAll(
        np.random.normal(size=30).reshape([2, 3, 5]).astype(np.float32))
    self._testAll(np.empty((2, 0, 5)).astype(np.float32))

  def testDouble(self):
    self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float64))
    self._testAll(
        np.random.normal(size=30).reshape([2, 3, 5]).astype(np.float64))
    self._testAll(np.empty((2, 0, 5)).astype(np.float64))

  def testInt32(self):
    self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.int32))
    self._testAll(
        (100 * np.random.normal(size=30)).reshape([2, 3, 5]).astype(np.int32))
    self._testAll(np.empty((2, 0, 5)).astype(np.int32))

  def testInt64(self):
    self._testAll(np.arange(-15, 15).reshape([2, 3, 5]).astype(np.int64))
    self._testAll(
        (100 * np.random.normal(size=30)).reshape([2, 3, 5]).astype(np.int64))
    self._testAll(np.empty((2, 0, 5)).astype(np.int64))

  def testSComplex(self):
    self._testAll(
        np.complex(1, 2) * np.arange(-15, 15).reshape([2, 3, 5]).astype(
            np.complex64))
    self._testAll(np.complex(
        1, 2) * np.random.normal(size=30).reshape([2, 3, 5]).astype(
            np.complex64))
    self._testAll(np.empty((2, 0, 5)).astype(np.complex64))

  def testString(self):
    self._testCpu(np.array([str(x) for x in np.arange(-15, 15)]).reshape(
        [2, 3, 5]))
    self._testCpu(np.empty((2, 0, 5)).astype(np.str_))

  def testStringWithNulls(self):
    with self.test_session():
      val = tf.convert_to_tensor("\0\0\0\0").eval()
    self.assertEqual(len(val), 4)
    self.assertEqual(val, "\0\0\0\0")

    with self.test_session():
      val = tf.convert_to_tensor("xx\0xx").eval()
    self.assertEqual(len(val), 5)
    self.assertAllEqual(val, "xx\0xx")
    nested = [["\0\0\0\0", "xx\0xx"], ["\0_\0_\0_\0", "\0"]]

    with self.test_session():
      val = tf.convert_to_tensor(nested).eval()
    # NOTE(mrry): Do not use assertAllEqual, because it converts nested to a
    #   numpy array, which loses the null terminators.
    self.assertEqual(val.tolist(), nested)

  def testExplicitShapeNumPy(self):
    with tf.Graph().as_default():
      c = tf.constant(
          np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32),
          shape=[2, 3, 5])
    self.assertEqual(c.get_shape(), [2, 3, 5])

  def testImplicitShapeNumPy(self):
    with tf.Graph().as_default():
      c = tf.constant(
          np.arange(-15, 15).reshape([2, 3, 5]).astype(np.float32))
    self.assertEqual(c.get_shape(), [2, 3, 5])

  def testExplicitShapeList(self):
    with tf.Graph().as_default():
      c = tf.constant([1, 2, 3, 4, 5, 6, 7], shape=[7])
    self.assertEqual(c.get_shape(), [7])

  def testImplicitShapeList(self):
    with tf.Graph().as_default():
      c = tf.constant([1, 2, 3, 4, 5, 6, 7])
    self.assertEqual(c.get_shape(), [7])

  def testExplicitShapeNumber(self):
    with tf.Graph().as_default():
      c = tf.constant(1, shape=[1])
    self.assertEqual(c.get_shape(), [1])

  def testImplicitShapeNumber(self):
    with tf.Graph().as_default():
      c = tf.constant(1)
    self.assertEqual(c.get_shape(), [])

  def testShapeInconsistent(self):
    with tf.Graph().as_default():
      c = tf.constant([1, 2, 3, 4, 5, 6, 7], shape=[10])
    self.assertEqual(c.get_shape(), [10])

  # pylint: disable=g-long-lambda
  def testShapeWrong(self):
    with tf.Graph().as_default():
      with self.assertRaisesWithPredicateMatch(
          ValueError,
          lambda e: ("Too many elements provided. Needed at most 5, "
                     "but received 7" == str(e))):
        tf.constant([1, 2, 3, 4, 5, 6, 7], shape=[5])
  # pylint: enable=g-long-lambda

  def testTooLargeConstant(self):
    with tf.Graph().as_default():
      large_array = np.zeros((512, 1024, 1024), dtype=np.float32)
      with self.assertRaisesRegexp(
          ValueError,
          "Cannot create an Operation with a NodeDef larger than 2GB."):
        c = tf.constant(large_array)

  def testTooLargeGraph(self):
    with tf.Graph().as_default() as g:
      large_array = np.zeros((256, 1024, 1024), dtype=np.float32)
      c = tf.constant(large_array)
      d = tf.constant(large_array)
      with self.assertRaisesRegexp(
          ValueError, "GraphDef cannot be larger than 2GB."):
        g.as_graph_def()

  def testSparseValuesRaiseErrors(self):
    with self.assertRaisesRegexp(ValueError,
                                 "setting an array element with a sequence"):
      c = tf.constant([[1, 2], [3]], dtype=tf.int32)

    with self.assertRaisesRegexp(ValueError, "must be a dense"):
      c = tf.constant([[1, 2], [3]])

    with self.assertRaisesRegexp(ValueError, "must be a dense"):
      c = tf.constant([[1, 2], [3], [4, 5]])


class AsTensorTest(tf.test.TestCase):

  def testAsTensorForTensorInput(self):
    with tf.Graph().as_default():
      t = tf.constant(10.0)
      x = tf.convert_to_tensor(t)
    self.assertIs(t, x)

  def testAsTensorForNonTensorInput(self):
    with tf.Graph().as_default():
      x = tf.convert_to_tensor(10.0)
    self.assertTrue(isinstance(x, tf.Tensor))

  def testAsTensorForShapeInput(self):
    with self.test_session():
      x = tf.convert_to_tensor(tf.TensorShape([]))
      self.assertEqual(tf.int32, x.dtype)
      self.assertAllEqual([], x.eval())

      x = tf.convert_to_tensor(tf.TensorShape([1, 2, 3]))
      self.assertEqual(tf.int32, x.dtype)
      self.assertAllEqual([1, 2, 3], x.eval())

      x = tf.convert_to_tensor(tf.TensorShape([1, 2, 3]), dtype=tf.int64)
      self.assertEqual(tf.int64, x.dtype)
      self.assertAllEqual([1, 2, 3], x.eval())

      x = tf.reshape(tf.zeros([6]), tf.TensorShape([2, 3]))
      self.assertAllEqual([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], x.eval())

    with self.assertRaisesRegexp(ValueError, "partially known"):
      tf.convert_to_tensor(tf.TensorShape(None))

    with self.assertRaisesRegexp(ValueError, "partially known"):
      tf.convert_to_tensor(tf.TensorShape([1, None, 64]))

    with self.assertRaises(TypeError):
      tf.convert_to_tensor(tf.TensorShape([1, 2, 3]), dtype=tf.float32)

  def testAsTensorForDimensionInput(self):
    with self.test_session():
      x = tf.convert_to_tensor(tf.TensorShape([1, 2, 3])[1])
      self.assertEqual(tf.int32, x.dtype)
      self.assertAllEqual(2, x.eval())

      x = tf.convert_to_tensor(tf.TensorShape([1, 2, 3])[1], dtype=tf.int64)
      self.assertEqual(tf.int64, x.dtype)
      self.assertAllEqual(2, x.eval())

    with self.assertRaisesRegexp(ValueError, "unknown Dimension"):
      tf.convert_to_tensor(tf.TensorShape(None)[1])

    with self.assertRaisesRegexp(ValueError, "unknown Dimension"):
      tf.convert_to_tensor(tf.TensorShape([1, None, 64])[1])

    with self.assertRaises(TypeError):
      tf.convert_to_tensor(tf.TensorShape([1, 2, 3])[1], dtype=tf.float32)


class IdentityOpTest(tf.test.TestCase):

  def testIdTensor(self):
    with tf.Graph().as_default():
      x = tf.constant(2.0, shape=[6], name="input")
      id_op = tf.identity(x, name="id")
    self.assertTrue(isinstance(id_op.op.inputs[0], tf.Tensor))
    self.assertProtoEquals(
        "name: 'id' op: 'Identity' input: 'input' "
        "attr { key: 'T' value { type: DT_FLOAT } }", id_op.op.node_def)


class ZerosTest(tf.test.TestCase):

  def _Zeros(self, shape):
    with self.test_session():
      ret = tf.zeros(shape)
      self.assertEqual(shape, ret.get_shape())
      return ret.eval()

  def testConst(self):
    self.assertTrue(np.array_equal(self._Zeros([2, 3]), np.array([[0] * 3] *
                                                                 2)))

  def testDynamicSizes(self):
    np_ans = np.array([[0] * 3] * 2)
    with self.test_session():
      # Creates a tensor of 2 x 3.
      d = tf.fill([2, 3], 12., name="fill")
      # Constructs a tensor of zeros of the same dimensions as "d".
      z = tf.zeros(tf.shape(d))
      out = z.eval()
    self.assertAllEqual(np_ans, out)
    self.assertShapeEqual(np_ans, d)
    self.assertShapeEqual(np_ans, z)

  def testDtype(self):
    with self.test_session():
      d = tf.fill([2, 3], 12., name="fill")
      self.assertEqual(d.get_shape(), [2, 3])
      # Test default type for both constant size and dynamic size
      z = tf.zeros([2, 3])
      self.assertEquals(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      z = tf.zeros(tf.shape(d))
      self.assertEquals(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      # Test explicit type control
      for dtype in [tf.float32, tf.float64, tf.int32,
                    tf.uint8, tf.int16, tf.int8,
                    tf.complex64, tf.int64]:
        z = tf.zeros([2, 3], dtype=dtype)
        self.assertEquals(z.dtype, dtype)
        self.assertEquals([2, 3], z.get_shape())
        z = tf.zeros(tf.shape(d), dtype=dtype)
        self.assertEquals(z.dtype, dtype)
        self.assertEquals([2, 3], z.get_shape())


class ZerosLikeTest(tf.test.TestCase):

  def testZerosLike(self):
    for dtype in [tf.float32, tf.float64, tf.int32,
                  tf.uint8, tf.int16, tf.int8,
                  tf.complex64, tf.int64]:
      numpy_dtype = dtype.as_numpy_dtype
      with self.test_session():
        # Creates a tensor of non-zero values with shape 2 x 3.
        d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
        # Constructs a tensor of zeros of the same dimensions and type as "d".
        z_var = tf.zeros_like(d)
        # Test that the type is correct
        self.assertEquals(z_var.dtype, dtype)
        z_value = z_var.eval()

      # Test that the value is correct
      self.assertTrue(np.array_equal(z_value, np.array([[0] * 3] * 2)))
      self.assertEqual([2, 3], z_var.get_shape())

  def testGenZerosLike(self):
    for dtype in [tf.float32, tf.float64, tf.int32,
                  tf.uint8, tf.int16, tf.int8,
                  tf.complex64, tf.int64]:
      numpy_dtype = dtype.as_numpy_dtype
      with self.test_session():
        # Creates a tensor of non-zero values with shape 2 x 3.
        d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
        # Constructs a tensor of zeros of the same dimensions and type as "d".
        z_var = gen_array_ops._zeros_like(d)
        # Test that the type is correct
        self.assertEquals(z_var.dtype, dtype)
        z_value = z_var.eval()

      # Test that the value is correct
      self.assertTrue(np.array_equal(z_value, np.array([[0] * 3] * 2)))
      self.assertEqual([2, 3], z_var.get_shape())


class OnesTest(tf.test.TestCase):

  def _Ones(self, shape):
    with self.test_session():
      ret = tf.ones(shape)
      self.assertEqual(shape, ret.get_shape())
      return ret.eval()

  def testConst(self):
    self.assertTrue(np.array_equal(self._Ones([2, 3]), np.array([[1] * 3] * 2)))

  def testDynamicSizes(self):
    np_ans = np.array([[1] * 3] * 2)
    with self.test_session():
      # Creates a tensor of 2 x 3.
      d = tf.fill([2, 3], 12., name="fill")
      # Constructs a tensor of ones of the same dimensions as "d".
      z = tf.ones(tf.shape(d))
      out = z.eval()
    self.assertAllEqual(np_ans, out)
    self.assertShapeEqual(np_ans, d)
    self.assertShapeEqual(np_ans, z)

  def testDtype(self):
    with self.test_session():
      d = tf.fill([2, 3], 12., name="fill")
      self.assertEqual(d.get_shape(), [2, 3])
      # Test default type for both constant size and dynamic size
      z = tf.ones([2, 3])
      self.assertEquals(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      z = tf.ones(tf.shape(d))
      self.assertEquals(z.dtype, tf.float32)
      self.assertEqual([2, 3], z.get_shape())
      # Test explicit type control
      for dtype in [tf.float32, tf.float64, tf.int32,
                    tf.uint8, tf.int16, tf.int8,
                    tf.complex64, tf.int64]:
        z = tf.ones([2, 3], dtype=dtype)
        self.assertEquals(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())
        z = tf.ones(tf.shape(d), dtype=dtype)
        self.assertEquals(z.dtype, dtype)
        self.assertEqual([2, 3], z.get_shape())


class OnesLikeTest(tf.test.TestCase):

  def testOnesLike(self):
    for dtype in [tf.float32, tf.float64, tf.int32,
                  tf.uint8, tf.int16, tf.int8,
                  tf.complex64, tf.int64]:
      numpy_dtype = dtype.as_numpy_dtype
      with self.test_session():
        # Creates a tensor of non-zero values with shape 2 x 3.
        d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
        # Constructs a tensor of zeros of the same dimensions and type as "d".
        z_var = tf.ones_like(d)
        # Test that the type is correct
        self.assertEquals(z_var.dtype, dtype)
        z_value = z_var.eval()

      # Test that the value is correct
      self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2)))
      self.assertEqual([2, 3], z_var.get_shape())

  def testGenOnesLike(self):
    for dtype in [tf.float32, tf.float64, tf.int32,
                  tf.uint8, tf.int16, tf.int8,
                  tf.complex64, tf.int64]:
      numpy_dtype = dtype.as_numpy_dtype
      with self.test_session():
        # Creates a tensor of non-zero values with shape 2 x 3.
        d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
        # Constructs a tensor of zeros of the same dimensions and type as "d".
        z_var = tf.ones_like(d)
        # Test that the type is correct
        self.assertEquals(z_var.dtype, dtype)
        z_value = z_var.eval()

      # Test that the value is correct
      self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2)))
      self.assertEqual([2, 3], z_var.get_shape())


class FillTest(tf.test.TestCase):

  def _compare(self, dims, val, np_ans, use_gpu):
    with self.test_session(use_gpu=use_gpu):
      tf_ans = tf.fill(dims, val, name="fill")
      out = tf_ans.eval()
    self.assertAllClose(np_ans, out)
    # Fill does not set the shape.
    # self.assertShapeEqual(np_ans, tf_ans)

  def _compareAll(self, dims, val, np_ans):
    self._compare(dims, val, np_ans, False)
    self._compare(dims, val, np_ans, True)

  def testFillFloat(self):
    np_ans = np.array([[3.1415] * 3] * 2).astype(np.float32)
    self._compareAll([2, 3], np_ans[0][0], np_ans)

  def testFillDouble(self):
    np_ans = np.array([[3.1415] * 3] * 2).astype(np.float64)
    self._compareAll([2, 3], np_ans[0][0], np_ans)

  def testFillInt32(self):
    np_ans = np.array([[42] * 3] * 2).astype(np.int32)
    self._compareAll([2, 3], np_ans[0][0], np_ans)

  def testFillInt64(self):
    np_ans = np.array([[-42] * 3] * 2).astype(np.int64)
    self._compareAll([2, 3], np_ans[0][0], np_ans)

  def testFillComplex(self):
    np_ans = np.array([[0.15] * 3] * 2).astype(np.complex64)
    self._compare([2, 3], np_ans[0][0], np_ans, use_gpu=False)

  def testFillString(self):
    np_ans = np.array([["yolo"] * 3] * 2)
    with self.test_session(use_gpu=False):
      tf_ans = tf.fill([2, 3], np_ans[0][0], name="fill").eval()
    self.assertAllEqual(np_ans, tf_ans)

  def testShapeFunctionEdgeCases(self):
    # Non-vector dimensions.
    with self.assertRaises(ValueError):
      tf.fill([[0, 1], [2, 3]], 1.0)

    # Non-scalar value.
    with self.assertRaises(ValueError):
      tf.fill([3, 2], [1.0, 2.0])

    # Partial dimension information.
    f = tf.fill(
        tf.placeholder(tf.int32, shape=(4,)), 3.0)
    self.assertEqual([None, None, None, None], f.get_shape().as_list())


class PlaceholderTest(tf.test.TestCase):

  def testDtype(self):
    with self.test_session():
      p = tf.placeholder(tf.float32, name="p")
      p_identity = tf.identity(p)
      feed_array = np.random.rand(10, 10)
      self.assertAllClose(p_identity.eval(feed_dict={p: feed_array}),
                          feed_array)

      with self.assertRaisesOpError(
          "must feed a value for placeholder tensor 'p' with dtype float"):
        p_identity.eval()

  def testShape(self):
    with self.test_session():
      p = tf.placeholder(tf.float32, shape=(10, 10), name="p")
      p_identity = tf.identity(p)
      feed_array = np.random.rand(10, 10)
      self.assertAllClose(p_identity.eval(feed_dict={p: feed_array}),
                          feed_array)

      with self.assertRaisesOpError(
          "must feed a value for placeholder tensor 'p' with dtype float and "
          "shape dim { size: 10 } dim { size: 10 }"):
        p_identity.eval()

      with self.assertRaisesWithPredicateMatch(
          ValueError, lambda e: "Cannot feed value of shape" in e.message):
        p_identity.eval(feed_dict={p: feed_array[:5, :5]})

  def testPartialShape(self):
    with self.test_session():
      p = tf.placeholder(tf.float32, shape=[None, 3], name="p")
      p_identity = tf.identity(p)
      feed_array = np.random.rand(10, 3)
      self.assertAllClose(p_identity.eval(feed_dict={p: feed_array}),
                          feed_array)

      with self.assertRaisesWithPredicateMatch(
          ValueError, lambda e: "Cannot feed value of shape" in e.message):
        p_identity.eval(feed_dict={p: feed_array[:5, :2]})

  def testControlDependency(self):
    with self.test_session():
      p = tf.placeholder(tf.int32, shape=[], name="p")
      with tf.control_dependencies([p]):
        c = tf.constant(5, tf.int32)
      d = tf.mul(p, c)
      self.assertEqual(10, d.eval(feed_dict={p: 2}))

  def testFillNegative(self):
    with self.test_session():
      for shape in (-1,), (2, -1), (-1, 2):
        with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                     " must be nonnegative"):
          tf.fill(shape, 7).eval()


if __name__ == "__main__":
  tf.test.main()