Ejemplo n.º 1
0
  def _verifyRegularized(self, x, y, l2_regularizer):
    for np_type in [np.float32, np.float64]:
      # Test with a single matrix.
      a = x.astype(np_type)
      b = y.astype(np_type)
      np_ans = BatchRegularizedLeastSquares(a, b, l2_regularizer)
      with self.test_session():
        # Test with the batch version of  matrix_solve_ls on regular matrices
        tf_ans = tf.batch_matrix_solve_ls(
            a, b, l2_regularizer=l2_regularizer, fast=True).eval()
        self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)

        # Test with the simple matrix_solve_ls on regular matrices
        tf_ans = tf.matrix_solve_ls(
            a, b, l2_regularizer=l2_regularizer, fast=True).eval()
        self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)

      # Test with a 2x3 batch of matrices.
      a = np.tile(x.astype(np_type), [2, 3, 1, 1])
      b = np.tile(y.astype(np_type), [2, 3, 1, 1])
      np_ans = BatchRegularizedLeastSquares(a, b, l2_regularizer)
      with self.test_session():
        tf_ans = tf.batch_matrix_solve_ls(
            a, b, l2_regularizer=l2_regularizer, fast=True).eval()
      self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)
    def _verifyRegularized(self, x, y, l2_regularizer):
        for np_type in [np.float32, np.float64]:
            # Test with a single matrix.
            a = x.astype(np_type)
            b = y.astype(np_type)
            np_ans = BatchRegularizedLeastSquares(a, b, l2_regularizer)
            with self.test_session():
                # Test with the batch version of  matrix_solve_ls on regular matrices
                tf_ans = tf.batch_matrix_solve_ls(
                    a, b, l2_regularizer=l2_regularizer, fast=True).eval()
                self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)

                # Test with the simple matrix_solve_ls on regular matrices
                tf_ans = tf.matrix_solve_ls(a,
                                            b,
                                            l2_regularizer=l2_regularizer,
                                            fast=True).eval()
                self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)

            # Test with a 2x3 batch of matrices.
            a = np.tile(x.astype(np_type), [2, 3, 1, 1])
            b = np.tile(y.astype(np_type), [2, 3, 1, 1])
            np_ans = BatchRegularizedLeastSquares(a, b, l2_regularizer)
            with self.test_session():
                tf_ans = tf.batch_matrix_solve_ls(
                    a, b, l2_regularizer=l2_regularizer, fast=True).eval()
            self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)
 def testWrongDimensions(self):
   # The matrix and right-hand sides should have the same number of rows.
   with self.test_session():
     matrix = tf.constant([[1., 0.], [0., 1.]])
     rhs = tf.constant([[1., 0.]])
     with self.assertRaises(ValueError):
       tf.matrix_solve_ls(matrix, rhs)
     with self.assertRaises(ValueError):
       tf.batch_matrix_solve_ls(matrix, rhs)
 def testWrongDimensions(self):
     # The matrix and right-hand sides should have the same number of rows.
     with self.test_session():
         matrix = tf.constant([[1., 0.], [0., 1.]])
         rhs = tf.constant([[1., 0.]])
         with self.assertRaises(ValueError):
             tf.matrix_solve_ls(matrix, rhs)
         with self.assertRaises(ValueError):
             tf.batch_matrix_solve_ls(matrix, rhs)
 def _verifySolveBatch(self, x, y):
   # Since numpy.linalg.lsqr does not support batch solves, as opposed
   # to numpy.linalg.solve, we just perform this test for a fixed batch size
   # of 2x3.
   for np_type in [np.float32, np.float64]:
     a = np.tile(x.astype(np_type), [2, 3, 1, 1])
     b = np.tile(y.astype(np_type), [2, 3, 1, 1])
     np_ans = np.empty([2, 3, a.shape[-1], b.shape[-1]])
     for dim1 in range(2):
       for dim2 in range(3):
         np_ans[dim1, dim2, :, :], _, _, _ = np.linalg.lstsq(
             a[dim1, dim2, :, :], b[dim1, dim2, :, :])
     for fast in [True, False]:
       with self.test_session():
         tf_ans = tf.batch_matrix_solve_ls(a, b, fast=fast).eval()
       self.assertEqual(np_ans.shape, tf_ans.shape)
       # Check residual norm.
       tf_r = b - BatchMatMul(a, tf_ans)
       tf_r_norm = np.sum(tf_r * tf_r)
       np_r = b - BatchMatMul(a, np_ans)
       np_r_norm = np.sum(np_r * np_r)
       self.assertAllClose(np_r_norm, tf_r_norm)
       # Check solution.
       if fast or a.shape[-2] >= a.shape[-1]:
         # We skip this test for the underdetermined case when using the
         # slow path, because Eigen does not return a minimum norm solution.
         # TODO(rmlarsen): Enable this check for all paths if/when we fix
         # Eigen's solver.
         self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)
 def _verifySolveBatch(self, x, y):
     # Since numpy.linalg.lsqr does not support batch solves, as opposed
     # to numpy.linalg.solve, we just perform this test for a fixed batch size
     # of 2x3.
     for np_type in [np.float32, np.float64]:
         a = np.tile(x.astype(np_type), [2, 3, 1, 1])
         b = np.tile(y.astype(np_type), [2, 3, 1, 1])
         np_ans = np.empty([2, 3, a.shape[-1], b.shape[-1]])
         for dim1 in range(2):
             for dim2 in range(3):
                 np_ans[dim1, dim2, :, :], _, _, _ = np.linalg.lstsq(
                     a[dim1, dim2, :, :], b[dim1, dim2, :, :])
         for fast in [True, False]:
             with self.test_session():
                 tf_ans = tf.batch_matrix_solve_ls(a, b, fast=fast).eval()
             self.assertEqual(np_ans.shape, tf_ans.shape)
             # Check residual norm.
             tf_r = b - BatchMatMul(a, tf_ans)
             tf_r_norm = np.sum(tf_r * tf_r)
             np_r = b - BatchMatMul(a, np_ans)
             np_r_norm = np.sum(np_r * np_r)
             self.assertAllClose(np_r_norm, tf_r_norm)
             # Check solution.
             if fast or a.shape[-2] >= a.shape[-1]:
                 # We skip this test for the underdetermined case when using the
                 # slow path, because Eigen does not return a minimum norm solution.
                 # TODO(rmlarsen): Enable this check for all paths if/when we fix
                 # Eigen's solver.
                 self.assertAllClose(np_ans, tf_ans, atol=1e-5, rtol=1e-5)
Ejemplo n.º 7
0
 def testBatchResultSize(self):
   # 3x3x3 matrices, 3x3x1 right-hand sides.
   matrix = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9.] * 3).reshape(3, 3, 3)
   rhs = np.array([1., 2., 3.] * 3).reshape(3, 3, 1)
   answer = tf.batch_matrix_solve(matrix, rhs)
   ls_answer = tf.batch_matrix_solve_ls(matrix, rhs)
   self.assertEqual(ls_answer.get_shape(), [3, 3, 1])
   self.assertEqual(answer.get_shape(), [3, 3, 1])
Ejemplo n.º 8
0
 def testBatchResultSize(self):
   # 3x3x3 matrices, 3x3x1 right-hand sides.
   matrix = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9.] * 3).reshape(3, 3, 3)
   rhs = np.array([1., 2., 3.] * 3).reshape(3, 3, 1)
   answer = tf.batch_matrix_solve(matrix, rhs)
   ls_answer = tf.batch_matrix_solve_ls(matrix, rhs)
   self.assertEqual(ls_answer.get_shape(), [3, 3, 1])
   self.assertEqual(answer.get_shape(), [3, 3, 1])