def testGradient(self): N = 30 initval = np.arange(1, 0.5 * N * (N + 1) + 1) v = tf.Variable(initval[None, :]) with tf.Session(''): f = (vec_to_tri(v) * np.random.randn(N, N)) ** 2.0 # Some function involving vec_to_tri self.assertLess(compute_gradient_error(v, [1, len(initval)], f, [1, N, N]), 10 ** -10)
def testTriToVec(self): mats = [ np.arange(1, 4)[None, :], np.vstack((np.arange(1, 4), np.arange(3, 0, -1))), # Currently, only do matrices np.arange(1, 16)[None, :] ] with tf.Session(''): for m in mats: # The ops are each others' inverse. self.assertTrue(np.all(tri_to_vec(vec_to_tri(m)).eval() == m))
def testBasicFunctionality(self): N = 3 D = 3 reference_matrices = self.getExampleMatrices(D, N) input_vector_tensor = tf.constant( self.referenceInverse(reference_matrices)) test_matrices_tensor = vec_to_tri(input_vector_tensor, N) test_matrices = tf.Session().run(test_matrices_tensor) np.testing.assert_array_almost_equal(reference_matrices, test_matrices)
def testDifferentiable(self): N = 3 D = 3 reference_matrices = self.getExampleMatrices(D, N) input_vector_array = self.referenceInverse(reference_matrices) input_vector_tensor = tf.constant(input_vector_array) test_matrices_tensor = vec_to_tri(input_vector_tensor, N) reduced_sum = tf.reduce_sum(test_matrices_tensor) gradient = tf.gradients(reduced_sum, input_vector_tensor)[0] reference_gradient = np.ones_like(input_vector_array) test_gradient = tf.Session().run(gradient) np.testing.assert_array_almost_equal(reference_gradient, test_gradient)
def testErrorOnIncorrectSize(self): with self.assertRaises(tf.errors.InvalidArgumentError): self.sess.run(vec_to_tri(np.arange(1, 5)[None, :]))
def compare_op(v): with tf.Session(''): return np_vec_to_tri(v) == vec_to_tri(v).eval()