def testSddmm(self, m, k, n, sparsity, use_gpu): # Helpers to set up the matrices. connector = connectors.Uniform(sparsity) initializer = initializers.Uniform() # Numpy matrices for verification. lhs_np = initializer([m, k]) rhs_np = initializer([n, k]) output_np = connector(np.ones([m, n])) # TensorFlow graph. output_topology = sparse_matrix.SparseMatrix("output", matrix=output_np) lhs = tf.Variable(lhs_np, dtype=tf.float32) rhs = tf.Variable(rhs_np, dtype=tf.float32) output = ops.sddmm(lhs, rhs, output_topology, transpose_rhs=True) # Execute the op and compare the results. with self.test_session(use_gpu=use_gpu) as sess: sess.run(tf.global_variables_initializer()) expected_output = self.dense_to_scipy( output_np * np.dot(lhs_np, np.transpose(rhs_np))) actual_output = self.sparse_to_scipy(*sess.run( [output.values, output.row_offsets, output.column_indices]), shape=expected_output.shape) self.assert_sparse_matrix_equal(actual_output, expected_output, atol=1e-03, rtol=1e-05)
def testSddmmGradient(self, m, k, n, sparsity, use_gpu): # Helpers to set up the matrices. connector = connectors.Uniform(sparsity) initializer = initializers.Uniform() # Numpy matrices for verification. lhs_np = initializer([m, k]) rhs_np = initializer([n, k]) output_np = connector(np.ones([m, n])) # TensorFlow graph. output_topology = sparse_matrix.SparseMatrix("output", matrix=output_np) lhs = tf.Variable(lhs_np, dtype=tf.float32) rhs = tf.Variable(rhs_np, dtype=tf.float32) output = ops.sddmm(lhs, rhs, output_topology, transpose_rhs=True) # Execute the op and compare the results. with self.test_session(use_gpu=use_gpu) as sess: sess.run(tf.global_variables_initializer()) error = tf.test.compute_gradient_error( [lhs, rhs], [[m, k], [n, k]], output.values, output.values.shape.as_list()) self.assertLess(error, 1e-3)