1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ============================================================================== 15 """Tests for scalar strictness and scalar leniency.""" 16 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 import numpy as np 22 23 from tensorflow.python.framework import ops 24 from tensorflow.python.framework import test_util 25 from tensorflow.python.ops import array_ops 26 from tensorflow.python.ops import gen_io_ops 27 from tensorflow.python.ops import math_ops 28 from tensorflow.python.ops import random_ops 29 from tensorflow.python.ops import sparse_ops 30 import tensorflow.python.ops.nn_grad # pylint: disable=unused-import 31 from tensorflow.python.platform import test 32 33 34 @test_util.with_c_api 35 class ScalarTest(test.TestCase): 36 37 def check(self, op, args, error, correct=None): 38 # Within Google, the switch to scalar strict occurred at version 6. 39 lenient = [] 40 strict = [5, 6] 41 42 # Use placeholders to bypass shape inference, since only the C++ 43 # GraphDef level is ever scalar lenient. 44 def placeholders(args, feed): 45 if isinstance(args, tuple): 46 return [placeholders(x, feed) for x in args] 47 else: 48 x = ops.convert_to_tensor(args).eval() 49 fake = array_ops.placeholder(np.asarray(x).dtype) 50 feed[fake] = x 51 return fake 52 53 # Test various GraphDef versions 54 for version in strict + lenient: 55 with ops.Graph().as_default() as g: 56 test_util.set_producer_version(g, version) 57 with self.test_session(graph=g) as sess: 58 feed = {} 59 xs = placeholders(args, feed) 60 x = op(*xs) 61 if version in strict: 62 with self.assertRaisesOpError(error): 63 sess.run(x, feed_dict=feed) 64 else: 65 r = sess.run(x, feed_dict=feed) 66 if correct is not None: 67 self.assertAllEqual(r, correct) 68 69 def testConcat(self): 70 self.check(array_ops.concat, (([2], [3], [7]), [0]), 71 'axis tensor should be a scalar integer', [2, 3, 7]) 72 for data in (2, 3, 7), (2, [3], 7), (2, 3, [7]): 73 self.check(array_ops.concat, (data, 0), 74 r'Expected \w+ dimensions in the range \[0, 0\)', [2, 3, 7]) 75 for data in ([2], 3, 7), ([2], [3], 7): 76 self.check(array_ops.concat, (data, 0), 77 r'Ranks of all input tensors should match', [2, 3, 7]) 78 79 def testFill(self): 80 self.check(array_ops.fill, (2, 3), 'dims must be a vector', [3, 3]) 81 self.check(array_ops.fill, ([2], [3]), 'value must be a scalar', [3, 3]) 82 83 def testPad(self): 84 self.check(array_ops.pad, (7, [[1, 2]]), 85 'The first dimension of paddings must be the rank of inputs', 86 [0, 7, 0, 0]) 87 88 def testRandom(self): 89 self.check(random_ops.random_uniform, (3,), 'shape must be a vector') 90 91 def testReshape(self): 92 self.check(array_ops.reshape, (7, 1), 'sizes input must be 1-D', [7]) 93 94 def testShardedFilename(self): 95 self.check(gen_io_ops._sharded_filename, ('foo', 4, [100]), 96 'must be a scalar', b'foo-00004-of-00100') 97 98 def testShardedFilespec(self): 99 self.check(gen_io_ops._sharded_filespec, ('foo', [100]), 'must be a scalar', 100 b'foo-?????-of-00100') 101 102 def testUnsortedSegmentSum(self): 103 self.check(math_ops.unsorted_segment_sum, (7, 1, [4]), 104 'num_segments should be a scalar', [0, 7, 0, 0]) 105 106 def testRange(self): 107 self.check(math_ops.range, ([0], 3, 2), 'start must be a scalar', [0, 2]) 108 self.check(math_ops.range, (0, [3], 2), 'limit must be a scalar', [0, 2]) 109 self.check(math_ops.range, (0, 3, [2]), 'delta must be a scalar', [0, 2]) 110 111 def testSlice(self): 112 data = np.arange(10) 113 error = 'Expected begin and size arguments to be 1-D tensors' 114 self.check(array_ops.slice, (data, 2, 3), error, [2, 3, 4]) 115 self.check(array_ops.slice, (data, [2], 3), error, [2, 3, 4]) 116 self.check(array_ops.slice, (data, 2, [3]), error, [2, 3, 4]) 117 118 def testSparseToDense(self): 119 self.check(sparse_ops.sparse_to_dense, (1, 4, 7), 120 'output_shape should be a vector', [0, 7, 0, 0]) 121 122 def testTile(self): 123 self.check(array_ops.tile, ([7], 2), 'Expected multiples to be 1-D', [7, 7]) 124 125 126 if __name__ == '__main__': 127 test.main() 128