def testInvalidN(self): with self.assertRaisesRegexp(ValueError, "non-negative but is -1"): nn_ops.nth_element([5], -1) with self.assertRaisesRegexp(ValueError, "scalar but has rank 1"): nn_ops.nth_element([5, 6, 3], [1])
def testGradients(self): x = [ [2., -1., 1000., 3., 1000.], [1., 5., 2., 4., 3.], [2., 2., 2., 2., 2.], ] grad_ys = [[-1., 2., 5.]] result = [ [0, 0, -0.5, 0, -0.5], [0, 0, 0, 2, 0], [1, 1, 1, 1, 1], ] if context.executing_eagerly(): inputs = ops.convert_to_tensor(x) with backprop.GradientTape() as tape: tape.watch(inputs) values = nn_ops.nth_element(inputs, 3) grad = tape.gradient(values, inputs, ops.convert_to_tensor(grad_ys)) self.assertAllClose(grad[0], result) # Test with tf.gradients with ops.Graph().as_default(): with self.session(use_gpu=False) as sess: inputs = array_ops.placeholder(dtypes.float32, shape=[3, 5]) values = nn_ops.nth_element(inputs, 3) grad = sess.run(gradients_impl.gradients(values, inputs, grad_ys=grad_ys), feed_dict={inputs: x}) self.assertAllClose(grad[0], result)
def testInvalidInput(self): with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError), "at least rank 1 but is rank 0"): nn_ops.nth_element(5, 0) # Test with placeholders with ops.Graph().as_default(): with self.session(use_gpu=False): v = array_ops.placeholder(dtype=dtypes.int32) with self.assertRaisesOpError("at least rank 1 but is rank 0"): nn_ops.nth_element(v, 0).eval(feed_dict={v: 5})
def testNTooLarge(self): inputs = [[0.1, 0.2], [0.3, 0.4]] with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError), "must have last dimension > n = 2"): nn_ops.nth_element(inputs, 2) # Test with placeholders with ops.Graph().as_default(): with self.session(use_gpu=False): n = array_ops.placeholder(dtypes.int32) values = nn_ops.nth_element(inputs, n) with self.assertRaisesOpError( "must have last dimension > n = 2"): values.eval(feed_dict={n: 2})
def testInvalidN(self): with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError), "non-negative but is -1"): nn_ops.nth_element([5], -1) with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError), "scalar but has rank 1"): nn_ops.nth_element([5, 6, 3], [1]) # Test with placeholders with ops.Graph().as_default(): with self.session(use_gpu=False): n = array_ops.placeholder(dtypes.int32) values = nn_ops.nth_element([5], n) with self.assertRaisesOpError("non-negative but is -1"): values.eval(feed_dict={n: -1})
def testNTooLargeAtEval(self): inputs = [[0.1, 0.2], [0.3, 0.4]] with self.session(use_gpu=False): n = array_ops.placeholder(dtypes.int32) values = nn_ops.nth_element(inputs, n) with self.assertRaisesOpError(r"Input must have at least n\+1 columns"): values.eval(feed_dict={n: 2})
def testInvalidNAtEval(self): inputs = [[0.1, 0.2], [0.3, 0.4]] with self.session(use_gpu=False): n = array_ops.placeholder(dtypes.int32) values = nn_ops.nth_element(inputs, n) with self.assertRaisesOpError("Need n >= 0, got -7"): values.eval(feed_dict={n: -7})
def testInvalidNAtEval(self): inputs = [[0.1, 0.2], [0.3, 0.4]] with self.test_session(use_gpu=False): n = array_ops.placeholder(dtypes.int32) values = nn_ops.nth_element(inputs, n) with self.assertRaisesOpError("Need n >= 0, got -7"): values.eval(feed_dict={n: -7})
def _validateNthElement(self, inputs, dtype, n, reverse, expected_values): np_expected_values = np.array(expected_values) with self.cached_session(use_gpu=False) as sess: inputs_op = ops.convert_to_tensor(inputs, dtype=dtype) values_op = nn_ops.nth_element(inputs_op, n, reverse=reverse) values = sess.run(values_op) self.assertShapeEqual(np_expected_values, values_op) self.assertAllClose(np_expected_values, values)
def _validateNthElement(self, inputs, dtype, n, reverse, expected_values): np_expected_values = np.array(expected_values) with self.cached_session(use_gpu=False) as sess: inputs_op = ops.convert_to_tensor(inputs, dtype=dtype) values_op = nn_ops.nth_element(inputs_op, n, reverse=reverse) values = self.evaluate(values_op) self.assertShapeEqual(np_expected_values, values_op) self.assertAllClose(np_expected_values, values)
def testGradients(self): with self.session(use_gpu=False) as sess: inputs = array_ops.placeholder(dtypes.float32, shape=[3, 5]) values = nn_ops.nth_element(inputs, 3) grad = sess.run( gradients_impl.gradients( values, inputs, grad_ys=[[-1., 2., 5.]]), feed_dict={inputs: [[2., -1., 1000., 3., 1000.], [1., 5., 2., 4., 3.], [2., 2., 2., 2., 2.], ]}) self.assertAllClose(grad[0], [[0, 0, -0.5, 0, -0.5], [0, 0, 0, 2, 0], [1, 1, 1, 1, 1], ])
def testInvalidInputAtEval(self): with self.session(use_gpu=False): v = array_ops.placeholder(dtype=dtypes.float32) with self.assertRaisesOpError("Input must be >= 1-D"): nn_ops.nth_element(v, 0).eval(feed_dict={v: 5.0})
def testNTooLarge(self): inputs = [[0.1, 0.2], [0.3, 0.4]] with self.assertRaisesRegexp(ValueError, "must have last dimension > n = 2"): nn_ops.nth_element(inputs, 2)
def testInvalidInput(self): with self.assertRaisesRegexp(ValueError, "at least rank 1 but is rank 0"): nn_ops.nth_element(5, 0)
def testInvalidInputAtEval(self): with self.test_session(use_gpu=False): v = array_ops.placeholder(dtype=dtypes.float32) with self.assertRaisesOpError("Input must be >= 1-D"): nn_ops.nth_element(v, 0).eval(feed_dict={v: 5.0})