Esempio n. 1
0
 def testInvalidShapeAtEval(self):
   with self.test_session(use_gpu=self._use_gpu):
     v = tf.placeholder(dtype=tf.float32)
     with self.assertRaisesOpError("input must be at least 2-dim"):
       tf.batch_matrix_set_diag(v, [v]).eval(feed_dict={v: 0.0})
     with self.assertRaisesOpError(
         r"but received input shape: \[1,1\] and diagonal shape: \[\]"):
       tf.batch_matrix_set_diag([[v]], v).eval(feed_dict={v: 0.0})
Esempio n. 2
0
 def testInvalidShapeAtEval(self):
   with self.test_session(use_gpu=self._use_gpu):
     v = tf.placeholder(dtype=tf.float32)
     with self.assertRaisesOpError("input must be at least 2-dim"):
       tf.batch_matrix_set_diag(v, [v]).eval(feed_dict={v: 0.0})
     with self.assertRaisesOpError(
         r"but received input shape: \[1,1\] and diagonal shape: \[\]"):
       tf.batch_matrix_set_diag([[v]], v).eval(feed_dict={v: 0.0})
Esempio n. 3
0
 def testVector(self):
     with self.test_session(use_gpu=self._use_gpu):
         v = np.array([1.0, 2.0, 3.0])
         mat = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0]])
         mat_set_diag = np.array([[1.0, 1.0, 0.0], [1.0, 2.0, 1.0],
                                  [1.0, 1.0, 3.0]])
         output = tf.batch_matrix_set_diag(mat, v)
         self.assertEqual((3, 3), output.get_shape())
         self.assertAllEqual(mat_set_diag, output.eval())
Esempio n. 4
0
def offdiagonal_constrain(var, scope=None):
    """Constrain all offdiagonal values to be positive for variable `var`"""
    if tf.get_variable_scope().reuse: return
    with tf.name_scope(scope or 'offdiagonal_constrain'):
        diag = tf.diag_part(var)
        constrained_value = tf.where(tf.greater_equal(var, 0.0), var,
                                     tf.zeros_like(var))
        constrained_value = tf.batch_matrix_set_diag(constrained_value, diag)
        update = tf.assign(var, constrained_value)
        tf.add_to_collection('constraints', update)
        return update
Esempio n. 5
0
 def testVector(self):
   with self.test_session(use_gpu=self._use_gpu):
     v = np.array([1.0, 2.0, 3.0])
     mat = np.array([[0.0, 1.0, 0.0],
                     [1.0, 0.0, 1.0],
                     [1.0, 1.0, 1.0]])
     mat_set_diag = np.array([[1.0, 1.0, 0.0],
                              [1.0, 2.0, 1.0],
                              [1.0, 1.0, 3.0]])
     output = tf.batch_matrix_set_diag(mat, v)
     self.assertEqual((3, 3), output.get_shape())
     self.assertAllEqual(mat_set_diag, output.eval())
Esempio n. 6
0
 def testGradWithNoShapeInformation(self):
   with self.test_session(use_gpu=self._use_gpu) as sess:
     v = tf.placeholder(dtype=tf.float32)
     mat = tf.placeholder(dtype=tf.float32)
     grad_input = tf.placeholder(dtype=tf.float32)
     output = tf.batch_matrix_set_diag(mat, v)
     grads = tf.gradients(output, [mat, v], grad_ys=grad_input)
     grad_input_val = np.random.rand(3, 3).astype(np.float32)
     grad_vals = sess.run(
         grads, feed_dict={v: 2 * np.ones(3), mat: np.ones((3, 3)),
                           grad_input: grad_input_val})
     self.assertAllEqual(np.diag(grad_input_val), grad_vals[1])
     self.assertAllEqual(grad_input_val - np.diag(np.diag(grad_input_val)),
                         grad_vals[0])
Esempio n. 7
0
 def testGrad(self):
   shapes = ((3, 4, 4), (7, 4, 8, 8))
   with self.test_session(use_gpu=self._use_gpu):
     for shape in shapes:
       x = tf.constant(np.random.rand(*shape), dtype=tf.float32)
       x_diag = tf.constant(np.random.rand(*shape[:-1]), dtype=tf.float32)
       y = tf.batch_matrix_set_diag(x, x_diag)
       error_x = tf.test.compute_gradient_error(x, x.get_shape().as_list(),
                                                y, y.get_shape().as_list())
       self.assertLess(error_x, 1e-4)
       error_x_diag = tf.test.compute_gradient_error(
           x_diag, x_diag.get_shape().as_list(),
           y, y.get_shape().as_list())
       self.assertLess(error_x_diag, 1e-4)
Esempio n. 8
0
 def testGradWithNoShapeInformation(self):
   with self.test_session(use_gpu=self._use_gpu) as sess:
     v = tf.placeholder(dtype=tf.float32)
     mat = tf.placeholder(dtype=tf.float32)
     grad_input = tf.placeholder(dtype=tf.float32)
     output = tf.batch_matrix_set_diag(mat, v)
     grads = tf.gradients(output, [mat, v], grad_ys=grad_input)
     grad_input_val = np.random.rand(3, 3).astype(np.float32)
     grad_vals = sess.run(
         grads, feed_dict={v: 2 * np.ones(3), mat: np.ones((3, 3)),
                           grad_input: grad_input_val})
     self.assertAllEqual(np.diag(grad_input_val), grad_vals[1])
     self.assertAllEqual(grad_input_val - np.diag(np.diag(grad_input_val)),
                         grad_vals[0])
Esempio n. 9
0
 def testGrad(self):
   shapes = ((3, 4, 4), (7, 4, 8, 8))
   with self.test_session(use_gpu=self._use_gpu):
     for shape in shapes:
       x = tf.constant(np.random.rand(*shape), dtype=tf.float32)
       x_diag = tf.constant(np.random.rand(*shape[:-1]), dtype=tf.float32)
       y = tf.batch_matrix_set_diag(x, x_diag)
       error_x = tf.test.compute_gradient_error(x, x.get_shape().as_list(),
                                                y, y.get_shape().as_list())
       self.assertLess(error_x, 1e-4)
       error_x_diag = tf.test.compute_gradient_error(
           x_diag, x_diag.get_shape().as_list(),
           y, y.get_shape().as_list())
       self.assertLess(error_x_diag, 1e-4)
Esempio n. 10
0
    def testBatchVector(self):
        with self.test_session(use_gpu=self._use_gpu):
            v_batch = np.array([[-1.0, -2.0, -3.0], [-4.0, -5.0, -6.0]])
            mat_batch = np.array([[[1.0, 0.0, 3.0], [0.0, 2.0, 0.0],
                                   [1.0, 0.0, 3.0]],
                                  [[4.0, 0.0, 4.0], [0.0, 5.0, 0.0],
                                   [2.0, 0.0, 6.0]]])

            mat_set_diag_batch = np.array([[[-1.0, 0.0, 3.0], [0.0, -2.0, 0.0],
                                            [1.0, 0.0, -3.0]],
                                           [[-4.0, 0.0, 4.0], [0.0, -5.0, 0.0],
                                            [2.0, 0.0, -6.0]]])
            output = tf.batch_matrix_set_diag(mat_batch, v_batch)
            self.assertEqual((2, 3, 3), output.get_shape())
            self.assertAllEqual(mat_set_diag_batch, output.eval())
Esempio n. 11
0
  def testBatchVector(self):
    with self.test_session(use_gpu=self._use_gpu):
      v_batch = np.array([[-1.0, -2.0, -3.0],
                          [-4.0, -5.0, -6.0]])
      mat_batch = np.array(
          [[[1.0, 0.0, 3.0],
            [0.0, 2.0, 0.0],
            [1.0, 0.0, 3.0]],
           [[4.0, 0.0, 4.0],
            [0.0, 5.0, 0.0],
            [2.0, 0.0, 6.0]]])

      mat_set_diag_batch = np.array(
          [[[-1.0, 0.0, 3.0],
            [0.0, -2.0, 0.0],
            [1.0, 0.0, -3.0]],
           [[-4.0, 0.0, 4.0],
            [0.0, -5.0, 0.0],
            [2.0, 0.0, -6.0]]])
      output = tf.batch_matrix_set_diag(mat_batch, v_batch)
      self.assertEqual((2, 3, 3), output.get_shape())
      self.assertAllEqual(mat_set_diag_batch, output.eval())
Esempio n. 12
0
 def testInvalidShape(self):
     with self.assertRaisesRegexp(ValueError, "must have rank at least 2"):
         tf.batch_matrix_set_diag(0, [0])
     with self.assertRaisesRegexp(ValueError, "must have rank at least 1"):
         tf.batch_matrix_set_diag([[0]], 0)
Esempio n. 13
0
 def testInvalidShape(self):
   with self.assertRaisesRegexp(ValueError, "must have rank at least 2"):
     tf.batch_matrix_set_diag(0, [0])
   with self.assertRaisesRegexp(ValueError, "must have rank at least 1"):
     tf.batch_matrix_set_diag([[0]], 0)