def _log_abs_determinant(self):
   logging.warn(
       "Using (possibly slow) default implementation of determinant."
       "  Requires conversion to a dense matrix and O(N^3) operations.")
   if self._can_use_cholesky():
     diag = array_ops.matrix_diag_part(linalg_ops.cholesky(self.to_dense()))
     return 2 * math_ops.reduce_sum(math_ops.log(diag), axis=[-1])
   _, log_abs_det = linalg.slogdet(self.to_dense())
   return log_abs_det
 def _log_abs_determinant(self):
   logging.warn(
       "Using (possibly slow) default implementation of determinant."
       "  Requires conversion to a dense matrix and O(N^3) operations.")
   if self._can_use_cholesky():
     diag = array_ops.matrix_diag_part(linalg_ops.cholesky(self.to_dense()))
     return 2 * math_ops.reduce_sum(math_ops.log(diag), axis=[-1])
   _, log_abs_det = linalg.slogdet(self.to_dense())
   return log_abs_det
  def testSimple(self):
    # 2x2 matrices
    matrix_np = np.array([[4., 6., 8., 10.], [6., 45., 54., 63.],
                          [8., 54., 146., 166.], [10., 63., 166., 310.]])

    with self.session() as sess:
      matrix = array_ops.placeholder(dtype=np.float32, shape=(4, 4))
      with self.test_scope():
        log_det = linalg_impl.slogdet(matrix)
      _, result = sess.run(log_det, {matrix: matrix_np})
    expected = 14.1601
    self.assertAllClose(result, expected, 1e-4)
Exemple #4
0
 def test_log_abs_det(self):
     with self.session(graph=ops.Graph()) as sess:
         sess.graph.seed = random_seed.DEFAULT_GRAPH_SEED
         operator, mat = self.operator_and_matrix(
             shapes_info, dtype, use_placeholder=use_placeholder)
         op_log_abs_det = operator.log_abs_determinant()
         _, mat_log_abs_det = linalg.slogdet(mat)
         if not use_placeholder:
             self.assertAllEqual(shapes_info.shape[:-2],
                                 op_log_abs_det.shape)
         op_log_abs_det_v, mat_log_abs_det_v = sess.run(
             [op_log_abs_det, mat_log_abs_det])
         self.assertAC(op_log_abs_det_v, mat_log_abs_det_v)
 def test_log_abs_det(self):
   with self.session(graph=ops.Graph()) as sess:
     sess.graph.seed = random_seed.DEFAULT_GRAPH_SEED
     operator, mat = self.operator_and_matrix(
         shapes_info, dtype, use_placeholder=use_placeholder)
     op_log_abs_det = operator.log_abs_determinant()
     _, mat_log_abs_det = linalg.slogdet(mat)
     if not use_placeholder:
       self.assertAllEqual(
           shapes_info.shape[:-2], op_log_abs_det.get_shape())
     op_log_abs_det_v, mat_log_abs_det_v = sess.run(
         [op_log_abs_det, mat_log_abs_det])
     self.assertAC(op_log_abs_det_v, mat_log_abs_det_v)
 def test_log_abs_det(self):
   self._skip_if_tests_to_skip_contains("log_abs_det")
   for use_placeholder in self._use_placeholder_options:
     for shape in self._shapes_to_test:
       for dtype in self._dtypes_to_test:
         with self.test_session(graph=ops.Graph()) as sess:
           sess.graph.seed = random_seed.DEFAULT_GRAPH_SEED
           operator, mat, feed_dict = self._operator_and_mat_and_feed_dict(
               shape, dtype, use_placeholder=use_placeholder)
           op_log_abs_det = operator.log_abs_determinant()
           _, mat_log_abs_det = linalg.slogdet(mat)
           if not use_placeholder:
             self.assertAllEqual(shape[:-2], op_log_abs_det.get_shape())
           op_log_abs_det_v, mat_log_abs_det_v = sess.run(
               [op_log_abs_det, mat_log_abs_det], feed_dict=feed_dict)
           self.assertAC(op_log_abs_det_v, mat_log_abs_det_v)
 def test_log_abs_det(self):
   self._skip_if_tests_to_skip_contains("log_abs_det")
   for use_placeholder in self._use_placeholder_options:
     for shape in self._shapes_to_test:
       for dtype in self._dtypes_to_test:
         with self.test_session(graph=ops.Graph()) as sess:
           sess.graph.seed = random_seed.DEFAULT_GRAPH_SEED
           operator, mat, feed_dict = self._operator_and_mat_and_feed_dict(
               shape, dtype, use_placeholder=use_placeholder)
           op_log_abs_det = operator.log_abs_determinant()
           _, mat_log_abs_det = linalg.slogdet(mat)
           if not use_placeholder:
             self.assertAllEqual(shape[:-2], op_log_abs_det.get_shape())
           op_log_abs_det_v, mat_log_abs_det_v = sess.run(
               [op_log_abs_det, mat_log_abs_det], feed_dict=feed_dict)
           self.assertAC(op_log_abs_det_v, mat_log_abs_det_v)