def testHessianInvalidDimension(self):
     for shape in [(10, 10), None]:
         with self.test_session(use_gpu=True):
             x = array_ops.placeholder(dtypes.float32, shape)
             # Expect a ValueError because the dimensions are wrong
             with self.assertRaises(ValueError):
                 gradients.hessians(x, x)
Esempio n. 2
0
 def testHessianInvalidDimension(self):
   for shape in [(10, 10), None]:
     with self.test_session(use_gpu=True):
       x = array_ops.placeholder(tf.float32, shape)
       # Expect a ValueError because the dimensions are wrong
       with self.assertRaises(ValueError):
         gradients.hessians(x, x)
 def testHessian2D_non_square_matrix(self):
     m = 3
     n = 4
     rng = np.random.RandomState([1, 2, 3])
     x_value = rng.randn(m, n).astype("float32")
     with self.test_session(use_gpu=True):
         x = constant_op.constant(x_value)
         x_square = math_ops.reduce_sum(
             math_ops.matmul(array_ops.transpose(x), x) * 0.5)
         hess = gradients.hessians(x_square, x)[0]
         hess_actual = hess.eval()
     hess_value = np.bmat([[elem * np.ones((n, n)) for elem in vec]
                           for vec in np.eye(m)]).astype("float32")
     self.assertAllEqual((m, n, m, n), hess_actual.shape)
     self.assertAllClose(hess_value, hess_actual.reshape((m * n, m * n)))
 def testHessian1D(self):
     # Manually compute the Hessian explicitly for a low-dimensional problem
     # and check that `hessian` matches. Specifically, the Hessian of
     # f(x) = x^T A x is H = A + A^T.
     m = 4
     rng = np.random.RandomState([1, 2, 3])
     mat_value = rng.randn(m, m).astype("float32")
     x_value = rng.randn(m).astype("float32")
     hess_value = mat_value + mat_value.T
     with self.test_session(use_gpu=True):
         mat = constant_op.constant(mat_value)
         x = constant_op.constant(x_value)
         x_mat_x = math_ops.reduce_sum(x[:, None] * mat * x[None, :])
         hess = gradients.hessians(x_mat_x, x)[0]
         hess_actual = hess.eval()
     self.assertAllClose(hess_value, hess_actual)
Esempio n. 5
0
 def testHessian1D(self):
   # Manually compute the Hessian explicitly for a low-dimensional problem
   # and check that `hessian` matches. Specifically, the Hessian of 
   # f(x) = x^T A x is H = A + A^T.
   m = 4
   rng = np.random.RandomState([1, 2, 3])
   mat_value = rng.randn(m, m).astype("float32")
   x_value = rng.randn(m).astype("float32")
   hess_value = mat_value + mat_value.T
   with self.test_session(use_gpu=True):
     mat = constant_op.constant(mat_value)
     x = constant_op.constant(x_value)
     x_mat_x = math_ops.reduce_sum(x[:, None] * mat * x[None, :])
     hess = gradients.hessians(x_mat_x, x)[0]
     hess_actual = hess.eval()
   self.assertAllClose(hess_value, hess_actual)
Esempio n. 6
0
 def testHessian1D_multi(self):
     # Test the computation of the hessian with respect to multiple tensors
     m = 4
     n = 3
     rng = np.random.RandomState([1, 2, 3])
     mat_values = [rng.randn(m, m).astype("float32") for _ in range(n)]
     x_values = [rng.randn(m).astype("float32") for _ in range(n)]
     hess_values = [mat_value + mat_value.T for mat_value in mat_values]
     with self.test_session(use_gpu=True):
         mats = [constant_op.constant(mat_value) for mat_value in mat_values]
         xs = [constant_op.constant(x_value) for x_value in x_values]
         xs_mats_xs = [math_ops.reduce_sum(x[:, None] * mat * x[None, :]) for x, mat in zip(xs, mats)]
         hessians = gradients.hessians(xs_mats_xs, xs)
         hessians_actual = [hess.eval() for hess in hessians]
     for hess_value, hess_actual in zip(hess_values, hessians_actual):
         self.assertAllClose(hess_value, hess_actual)
 def testHessian2D_square_matrix(self):
     # Manually compute the Hessian explicitly for a low-dimensional problem
     # and check that `hessian` matches. Specifically, the Hessian of
     # f(x) = 1/2 * x^T * x is H = constant (block identity matrix)
     m = 3
     rng = np.random.RandomState([1, 2, 3])
     x_value = rng.randn(m, m).astype("float32")
     with self.test_session(use_gpu=True):
         x = constant_op.constant(x_value)
         x_square = math_ops.reduce_sum(
             math_ops.matmul(array_ops.transpose(x), x) * 0.5)
         hess = gradients.hessians(x_square, x)[0]
         hess_actual = hess.eval()
     hess_value = np.bmat([[elem * np.ones((m, m)) for elem in vec]
                           for vec in np.eye(m)]).astype("float32")
     self.assertAllEqual((m, m, m, m), hess_actual.shape)
     self.assertAllClose(hess_value, hess_actual.reshape((m * m, m * m)))
Esempio n. 8
0
 def testHessian2D_non_square_matrix(self):
   m = 3
   n = 4
   rng = np.random.RandomState([1, 2, 3])
   x_value = rng.randn(m, n).astype("float32")
   with self.test_session(use_gpu=True):
     x = constant_op.constant(x_value)
     x_square = math_ops.reduce_sum(
         math_ops.matmul(array_ops.transpose(x), x) * 0.5
     )
     hess = gradients.hessians(x_square, x)[0]
     hess_actual = hess.eval()
   hess_value = np.bmat([
       [elem*np.ones((n, n)) for elem in vec]
       for vec in np.eye(m)
   ]).astype("float32")
   self.assertAllEqual((m, n, m, n), hess_actual.shape)
   self.assertAllClose(hess_value, hess_actual.reshape((m * n, m * n)))
Esempio n. 9
0
 def testHessian1D_multi(self):
   # Test the computation of the hessian with respect to multiple tensors
   m = 4
   n = 3
   rng = np.random.RandomState([1, 2, 3])
   mat_values = [rng.randn(m, m).astype("float32") for _ in range(n)]
   x_values = [rng.randn(m).astype("float32") for _ in range(n)]
   hess_values = [mat_value + mat_value.T for mat_value in mat_values]
   with self.test_session(use_gpu=True):
     mats = [constant_op.constant(mat_value) for mat_value in mat_values]
     xs = [constant_op.constant(x_value) for x_value in x_values]
     xs_mats_xs = [
         math_ops.reduce_sum(x[:, None] * mat * x[None, :])
         for x, mat in zip(xs, mats)
     ]
     hessians = gradients.hessians(xs_mats_xs, xs)
     hessians_actual = [hess.eval() for hess in hessians]
   for hess_value, hess_actual in zip(hess_values, hessians_actual):
     self.assertAllClose(hess_value, hess_actual)
Esempio n. 10
0
 def testHessian2D_square_matrix(self):
   # Manually compute the Hessian explicitly for a low-dimensional problem
   # and check that `hessian` matches. Specifically, the Hessian of
   # f(x) = 1/2 * x^T * x is H = constant (block identity matrix)
   m = 3
   rng = np.random.RandomState([1, 2, 3])
   x_value = rng.randn(m, m).astype("float32")
   with self.test_session(use_gpu=True):
     x = constant_op.constant(x_value)
     x_square = math_ops.reduce_sum(
         math_ops.matmul(array_ops.transpose(x), x) * 0.5
     )
     hess = gradients.hessians(x_square, x)[0]
     hess_actual = hess.eval()
   hess_value = np.bmat([
       [elem*np.ones((m, m)) for elem in vec]
       for vec in np.eye(m)
   ]).astype("float32")
   self.assertAllEqual((m, m, m, m), hess_actual.shape)
   self.assertAllClose(hess_value, hess_actual.reshape((m * m, m * m)))