def testSort_staticallyKnownRank_constantTransposition(self): # The transposition array should be a constant if the rank of "values" is # statically known. tensor = random_ops.random_uniform( # Rank is statically known to be 5, but the dimension lengths are not # known. random_ops.random_uniform( shape=(5,), minval=0, maxval=10, dtype=dtypes.int32)) sort_ops.sort(tensor, axis=1) transposition = ( ops.get_default_graph().get_tensor_by_name('sort/transposition:0')) self.assertFalse(tensor_util.constant_value(transposition) is None) self.assertAllEqual( # Swaps "1" and "4" to put "1" at the end. tensor_util.constant_value(transposition), [0, 4, 2, 3, 1])
def testDescending(self): arr = np.random.random((10, 5, 5)) with self.cached_session(): self.assertAllEqual( np.sort(arr, axis=0)[::-1], sort_ops.sort(constant_op.constant(arr), axis=0, direction='DESCENDING').eval())
def testSort_staticallyKnownRank_constantTransposition(self): # The transposition array should be a constant if the rank of "values" is # statically known. tensor = random_ops.random_uniform( # Rank is statically known to be 5, but the dimension lengths are not # known. random_ops.random_uniform(shape=(5, ), minval=0, maxval=10, dtype=dtypes.int32)) sort_ops.sort(tensor, axis=1) transposition = ( ops.get_default_graph().get_tensor_by_name('sort/transposition:0')) self.assertFalse(tensor_util.constant_value(transposition) is None) self.assertAllEqual( # Swaps "1" and "4" to put "1" at the end. tensor_util.constant_value(transposition), [0, 4, 2, 3, 1])
def testDescending(self): arr = np.random.random((10, 5, 5)) with self.cached_session(): self.assertAllEqual( np.sort(arr, axis=0)[::-1], sort_ops.sort( constant_op.constant(arr), axis=0, direction='DESCENDING').eval())
def testScalar(self): # Create an empty scalar where the static shape is unknown. zeros_length_1 = array_ops.zeros(random_ops.random_uniform( [1], minval=0, maxval=1, dtype=dtypes.int32), dtype=dtypes.int32) scalar = array_ops.zeros(zeros_length_1) sort = sort_ops.sort(scalar) with self.cached_session(): with self.assertRaises(errors.InvalidArgumentError): sort.eval()
def testScalar(self): # Create an empty scalar where the static shape is unknown. zeros_length_1 = array_ops.zeros( random_ops.random_uniform([1], minval=0, maxval=1, dtype=dtypes.int32), dtype=dtypes.int32) scalar = array_ops.zeros(zeros_length_1) sort = sort_ops.sort(scalar) with self.cached_session(): with self.assertRaises(errors.InvalidArgumentError): sort.eval()
def testRandom_highDimensionality(self): np.random.seed(100) for _ in range(20): rank = np.random.randint(5, 15) shape = [np.random.randint(1, 4) for _ in range(rank)] arr = np.random.random(shape) sort_axis = np.random.choice(rank) with self.cached_session(): self.assertAllEqual( np.sort(arr, axis=sort_axis), sort_ops.sort(constant_op.constant(arr), axis=sort_axis).eval())
def _testRandom_lowDimensionality(self, negative_axis): np.random.seed(42) for _ in range(20): rank = np.random.randint(1, 3) shape = [np.random.randint(0, 20) for _ in range(rank)] arr = np.random.random(shape) sort_axis = np.random.choice(rank) if negative_axis: sort_axis = -1 - sort_axis with self.cached_session(): self.assertAllEqual( np.sort(arr, axis=sort_axis), sort_ops.sort(constant_op.constant(arr), axis=sort_axis).eval())
def testNegativeOutOfBounds_staticShape(self): arr = constant_op.constant([3, 4, 5]) with self.assertRaises(ValueError): sort_ops.sort(arr, axis=-4)