def test_multiplex_float(self): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0]) b = tf.constant([10.0, 20.0, 30.0, 40.0, 50.0]) cond = tf.constant([True, False, True, False, True], dtype=bool) # expected result is [1.0, 20.0, 3.0, 40.0, 5.0] expect = np.where(self.evaluate(cond), self.evaluate(a), self.evaluate(b)) result = multiplex_2_op.multiplex(cond, a, b) self.assertAllEqual(result, expect)
def test_multiplex_int(self): a = tf.constant([1, 2, 3, 4, 5], dtype=tf.int64) b = tf.constant([10, 20, 30, 40, 50], dtype=tf.int64) cond = tf.constant([True, False, True, False, True], dtype=bool) expect = np.where(self.evaluate(cond), self.evaluate(a), self.evaluate(b)) # expected result is [1, 20, 3, 40, 5] result = multiplex_2_op.multiplex(cond, a, b) self.assertAllEqual(result, expect)
def test_multiplex_2d(self): a = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.int64) b = tf.constant([[10, 20, 30], [40, 50, 60]], dtype=tf.int64) cond = tf.constant([[True, False, True], [False, True, False]], dtype=bool) expect = np.where(self.evaluate(cond), self.evaluate(a), self.evaluate(b)) # expected result is [[1, 20], [3, 40]] result = multiplex_2_op.multiplex(cond, a, b) self.assertAllEqual(result, expect)
def test_sparse_op_different(self): cond = tf.SparseTensor(indices=[[1], [3], [6]], values=[True, False, True], dense_shape=[7]) a = tf.SparseTensor(indices=[[1], [3], [5]], values=['a0', 'a1', 'a2'], dense_shape=[6]) b = tf.SparseTensor(indices=[[0], [2], [3], [6]], values=['b0', 'b1', 'b2', 'b3'], dense_shape=[7]) result = self.evaluate(multiplex_2_op.multiplex(cond, a, b)) self.assertAllEqual([7], result.dense_shape) self.assertAllEqual([[0], [1], [2], [3]], result.indices) self.assertAllEqual([b'b0', b'a0', b'b1', b'b2'], result.values)
def test_multiplex_bad_size(self): a = tf.constant([1, 2, 3, 4, 5], dtype=tf.int64) # longer than b b = tf.constant([10, 20], dtype=tf.int64) # shorter than a cond = tf.constant([True, False, True, False, True], dtype=bool) with self.assertRaisesRegex( (errors_impl.InvalidArgumentError, ValueError), # Eager mode raises InvalidArgumentError with the following message r'(a and b must have the same shape. ' r'a shape: \[5\] b shape: \[2\](?s).* ' r'\[Op:Examples>MultiplexDense\]' r')|(' # Graph mode raises ValueError with the following message r'Dimension 0 in both shapes must be equal, but are 5 and 2\. ' r'Shapes are \[5\] and \[2\]\.)'): self.evaluate(multiplex_2_op.multiplex(cond, a, b))
def test_multiplex_bad_types(self): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0]) # float b = tf.constant([10, 20, 30, 40, 50], dtype=tf.int64) cond = tf.constant([True, False, True, False, True], dtype=bool) with self.assertRaisesRegex( (errors_impl.InvalidArgumentError, TypeError), # Eager mode raises InvalidArgumentError with the following message r'(cannot compute Examples>MultiplexDense as input #2\(zero-based\) ' r'was expected to be a float tensor but is a int64 tensor ' r'\[Op:Examples>MultiplexDense\]' r')|(' # Graph mode raises TypeError with the following message r"Input 'b' of 'Examples>MultiplexDense' Op has type int64 that " r"does not match type float32 of argument 'a'.)"): self.evaluate(multiplex_2_op.multiplex(cond, a, b))
def test_multiplex_bad_shape(self): a = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.int64) # shape (2,3) b = tf.constant([[10, 20], [30, 40], [50, 60]], dtype=tf.int64) # shape (3,2) cond = tf.constant([[True, False, True], [False, True, False]], dtype=bool) with self.assertRaisesRegex( (errors_impl.InvalidArgumentError, ValueError), # Eager mode raises InvalidArgumentError with the following message r'(a and b must have the same shape.' r' a shape: \[2,3\] b shape: \[3,2\]' r')|(' # Graph mode raises ValueError with the following message r'Dimension 0 in both shapes must be equal, ' r'but are 2 and 3\. Shapes are \[2,3\] and \[3,2\])\.'): self.evaluate(multiplex_2_op.multiplex(cond, a, b))