Python tensorflow.python.ops.gradient_checker.compute_gradient_error() Examples

The following are 16 code examples of tensorflow.python.ops.gradient_checker.compute_gradient_error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.ops.gradient_checker , or try the search function .
Example #1
Source File: lstm1d_test.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def testSequenceToSequenceGradient(self):
    with self.test_session():
      size = (17, 1, 15)
      output_size = (17, 1, 8)
      inputs = constant_op.constant(_rand(*size))
      outputs = lstm1d.ndlstm_base(inputs, 8, dynamic=False)
      variables.global_variables_initializer().run()
      gradients = gradients_impl.gradients(outputs, inputs)
      if 1:  # pylint: disable=using-constant-test
        gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
        self.assertEqual(gradients.shape, size)
      else:
        # TODO(tmb) tf.test.compute_gradient error is currently broken
        # with dynamic_rnn. Enable this test case eventually.
        err = gradient_checker.compute_gradient_error(
            inputs, size, outputs, output_size, delta=1e-4)
        self.assert_(not np.isnan(err))
        self.assert_(err < 0.1) 
Example #2
Source File: lstm1d_test.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def testSequenceToSequenceGradientReverse(self):
    with self.test_session():
      size = (17, 1, 15)
      output_size = (17, 1, 8)
      inputs = constant_op.constant(_rand(*size))
      outputs = lstm1d.ndlstm_base(inputs, 8, reverse=1, dynamic=False)
      variables.global_variables_initializer().run()
      if 1:  # pylint: disable=using-constant-test
        gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
        self.assertEqual(gradients.shape, size)
      else:
        # TODO(tmb) tf.test.compute_gradient error is currently broken
        # with dynamic_rnn. Enable this test case eventually.
        err = gradient_checker.compute_gradient_error(
            inputs, size, outputs, output_size, delta=1e-4)
        self.assert_(not np.isnan(err))
        self.assert_(err < 0.1) 
Example #3
Source File: lstm1d_test.py    From keras-lambda with MIT License 6 votes vote down vote up
def testSequenceToSequenceGradient(self):
    with self.test_session():
      size = (17, 1, 15)
      output_size = (17, 1, 8)
      inputs = constant_op.constant(_rand(*size))
      outputs = lstm1d.ndlstm_base(inputs, 8, dynamic=False)
      variables.global_variables_initializer().run()
      gradients = gradients_impl.gradients(outputs, inputs)
      if 1:  # pylint: disable=using-constant-test
        gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
        self.assertEqual(gradients.shape, size)
      else:
        # TODO(tmb) tf.test.compute_gradient error is currently broken
        # with dynamic_rnn. Enable this test case eventually.
        err = gradient_checker.compute_gradient_error(
            inputs, size, outputs, output_size, delta=1e-4)
        self.assert_(not np.isnan(err))
        self.assert_(err < 0.1) 
Example #4
Source File: lstm1d_test.py    From keras-lambda with MIT License 6 votes vote down vote up
def testSequenceToSequenceGradientReverse(self):
    with self.test_session():
      size = (17, 1, 15)
      output_size = (17, 1, 8)
      inputs = constant_op.constant(_rand(*size))
      outputs = lstm1d.ndlstm_base(inputs, 8, reverse=1, dynamic=False)
      variables.global_variables_initializer().run()
      if 1:  # pylint: disable=using-constant-test
        gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
        self.assertEqual(gradients.shape, size)
      else:
        # TODO(tmb) tf.test.compute_gradient error is currently broken
        # with dynamic_rnn. Enable this test case eventually.
        err = gradient_checker.compute_gradient_error(
            inputs, size, outputs, output_size, delta=1e-4)
        self.assert_(not np.isnan(err))
        self.assert_(err < 0.1) 
Example #5
Source File: layers_test.py    From tf-slim with Apache License 2.0 5 votes vote down vote up
def testPoincareNormalizeGradient(self):
    x_shape = [20, 7, 3]
    np.random.seed(1)
    x_np = np.random.random_sample(x_shape).astype(np.float64)
    for dim in range(len(x_shape)):
      with self.cached_session():
        x_tf = constant_op.constant(x_np, name='x')
        y_tf = _layers.poincare_normalize(x_tf, dim)
        err = gradient_checker.compute_gradient_error(x_tf, x_shape, y_tf,
                                                      x_shape)
      print('PoinCareNormalize gradient err = %g ' % err)
      self.assertLess(err, 1e-4)


# TODO(b/28426988): Add separate tests for non-legacy versions. 
Example #6
Source File: test_render_sprites.py    From auto_yolo with MIT License 5 votes vote down vote up
def _test_gradient(device):

    if device == "gpu" and visible_gpu():
        pytest.xfail("no gpu is visible")

    with NumpySeed(100):
        with tf.device('/{}:0'.format(device)):
            sprites, scales, offsets, backgrounds = get_data(random_alpha=True, squash=0.99)

            sprites_tf = constant_op.constant(sprites)
            scales_tf = constant_op.constant(scales)
            offsets_tf = constant_op.constant(offsets)
            backgrounds_tf = constant_op.constant(backgrounds)

            images = render_sprites.render_sprites(sprites_tf, scales_tf, offsets_tf, backgrounds_tf)

            sess = get_session()
            with sess.as_default():
                with tf.device(device):
                    err = gradient_checker.compute_gradient_error(
                        [sprites_tf, scales_tf, offsets_tf, backgrounds_tf],
                        [sprites.shape, scales.shape, offsets.shape, backgrounds.shape],
                        images,
                        backgrounds.shape,
                        [sprites, scales, offsets, backgrounds],
                        delta=0.002)

            print("Jacobian error: {}".format(err))
            threshold = 2e-4
            assert err < threshold, "Jacobian error ({}) exceeded threshold ({})".format(err, threshold) 
Example #7
Source File: maxpool_gradgrad_tests.py    From tensorflow-forward-ad with MIT License 5 votes vote down vote up
def test_basics(self):
    tf.set_random_seed(1234)
    x_shape = [4, 4, 4, 5]
    rnd = np.random.RandomState(0)
    x_np = rnd.uniform(-1.0, 1.0, x_shape).astype(np.float32)

    # test op max_pool_grad
    with tf.Graph().as_default(), tf.Session() as sess:
      x_tf = tf.constant(x_np, name="x")
      y_tf1, _ = tf.nn.max_pool_with_argmax(
          x_tf,
          ksize=[1, 3, 3, 1],
          strides=[1, 2, 2, 1],
          padding='SAME',
          name="y1")
      y_tf2 = tf.nn.max_pool(
          x_tf,
          ksize=[1, 3, 3, 1],
          strides=[1, 2, 2, 1],
          padding='SAME',
          name="y2")
      z_tf1 = tf.reduce_sum(tf.square(y_tf1))
      z_tf2 = tf.reduce_sum(tf.square(y_tf2))
      dx1 = tf.gradients(z_tf1, x_tf, name='dx1')[0]
      dx2 = tf.gradients(z_tf2, x_tf, name='dx2')[0]
      err = gradient_checker.compute_gradient_error(
          x_tf, x_shape, dx1, x_shape, delta=1e-3, x_init_value=x_np)
      self.assertTrue(err < 1e-3)
      err = gradient_checker.compute_gradient_error(
          x_tf, x_shape, dx2, x_shape, delta=1e-3, x_init_value=x_np)
      self.assertTrue(err < 1e-3) 
Example #8
Source File: icp_grad_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def test_grad_transform(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #9
Source File: icp_grad_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def test_grad_transform_same_ego_motion(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.1, 0, 0]
      ego_motion = tf.constant([[0.1, 0.0, 0.0, 0.0, 0.0, 0.0]],
                               dtype=tf.float32)
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #10
Source File: icp_grad_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def test_grad_residual(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      unused_transform, residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(
          cloud_source, cloud_source.shape.as_list(), residual,
          residual.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #11
Source File: icp_grad_test.py    From models with Apache License 2.0 5 votes vote down vote up
def test_grad_transform(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #12
Source File: icp_grad_test.py    From models with Apache License 2.0 5 votes vote down vote up
def test_grad_transform_same_ego_motion(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.1, 0, 0]
      ego_motion = tf.constant([[0.1, 0.0, 0.0, 0.0, 0.0, 0.0]],
                               dtype=tf.float32)
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #13
Source File: icp_grad_test.py    From models with Apache License 2.0 5 votes vote down vote up
def test_grad_residual(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      unused_transform, residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(
          cloud_source, cloud_source.shape.as_list(), residual,
          residual.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #14
Source File: icp_grad_test.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def test_grad_transform(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #15
Source File: icp_grad_test.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def test_grad_transform_same_ego_motion(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.1, 0, 0]
      ego_motion = tf.constant([[0.1, 0.0, 0.0, 0.0, 0.0, 0.0]],
                               dtype=tf.float32)
      transform, unused_residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(ego_motion,
                                                    ego_motion.shape.as_list(),
                                                    transform,
                                                    transform.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3) 
Example #16
Source File: icp_grad_test.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def test_grad_residual(self):
    with self.test_session():
      cloud_source = self.small_cloud
      cloud_target = cloud_source + [0.05, 0, 0]
      ego_motion = self.identity_transform
      unused_transform, residual = self._run_icp(cloud_source, ego_motion,
                                                 cloud_target)
      err = gradient_checker.compute_gradient_error(
          cloud_source, cloud_source.shape.as_list(), residual,
          residual.shape.as_list())
    # Since our gradient is an approximation, it doesn't pass a numerical check.
    # Nonetheless, this test verifies that icp_grad computes a gradient.
    self.assertGreater(err, 1e-3)