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 testSparseSoftmax(self, m, n, sparsity): # Helpers to set up the matrices. connector = connectors.Uniform(sparsity) initializer = initializers.Uniform() # Numpy matrix for verification. matrix_np = connector(initializer([m, n])) # TensorFlow graph. matrix = sparse_matrix.SparseMatrix("input", matrix=matrix_np) output = ops.sparse_softmax(matrix) with self.test_session(use_gpu=True) as sess: sess.run(tf.global_variables_initializer()) # Zero terms should not contribute to the softmax. matrix_np[matrix_np == 0] = -1e9 def softmax(x): maxs = np.expand_dims(x.max(axis=1), axis=1) exps = np.exp(x - maxs) return exps / np.expand_dims(np.sum(exps, axis=1), axis=1) expected_output = self.dense_to_scipy(softmax(matrix_np)) actual_output = self.sparse_to_scipy( *sess.run( [output.values, output.row_offsets, output.column_indices]), expected_output.shape) self.assert_sparse_matrix_equal(actual_output, expected_output, atol=1e-03, rtol=1e-05)
def build(self, input_shape): input_shape = input_shape.as_list() input_channels = input_shape[1] with tf.variable_scope(self.name, default_name="sparse_conv2d"): # TODO(tgale): This is a hack to make sure the sparsities # match exactly, not a general solution. sparsity = 1.0 - self.nonzeros / (self.filters * input_channels) self.kernel = sparse_matrix.SparseMatrix( "kernel", [self.filters, input_channels], connector=connectors.Uniform(sparsity)) if self.use_bias: self.bias = tf.get_variable("bias", [self.filters])
def testSpmmGradient(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 = connector(initializer([m, k])) rhs_np = initializer([k, n]) lhs = sparse_matrix.SparseMatrix("lhs", matrix=lhs_np) rhs = tf.Variable(rhs_np, dtype=tf.float32) output = ops.spmm(lhs, rhs) with self.test_session(use_gpu=use_gpu) as sess: sess.run(tf.global_variables_initializer()) error = tf.test.compute_gradient_error( [lhs.values, rhs], [lhs.values.shape.as_list(), [k, n]], output, [m, n]) self.assertLess(error, 1e-3)
def testCreateMatrix(self, m, n, sparsity): matrix = sparse_matrix.SparseMatrix( "matrix", [m, n], connector=connectors.Uniform(sparsity)) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) values, row_indices, row_offsets, column_indices = sess.run([ matrix.values, matrix.row_indices, matrix.row_offsets, matrix.column_indices ]) # Check the shape of the matrix. self.assertLen(values.shape, 1) self.assertLen(row_indices.shape, 1) self.assertLen(row_offsets.shape, 1) self.assertLen(column_indices.shape, 1) # Check the sparsity matches the target. target_nonzeros = m * n - int(round(sparsity * m * n)) self.assertEqual(values.shape[0], target_nonzeros)
def testSpmm(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 = connector(initializer([m, k])) rhs_np = initializer([k, n]) # TensorFlow graph. lhs = sparse_matrix.SparseMatrix("lhs", matrix=lhs_np) rhs = tf.Variable(rhs_np, dtype=tf.float32) output = ops.spmm(lhs, rhs) # Execute the op and compare the results. with self.test_session(use_gpu=use_gpu) as sess: sess.run(tf.global_variables_initializer()) self.assertAllClose(sess.run(output), np.dot(lhs_np, rhs_np), atol=1e-03, rtol=1e-05)
def testCsr2Idx(self, m, n, sparsity, use_gpu): # Helpers to set up the matrices. connector = connectors.Uniform(sparsity) initializer = initializers.Uniform() # Numpy matrix for verification. matrix_np = connector(initializer([m, n])) # TensorFlow graph. matrix = sparse_matrix.SparseMatrix("input", matrix=matrix_np) output = ops.csr2idx(matrix) # Execute the op and compare the results. with self.test_session(use_gpu=use_gpu) as sess: sess.run(tf.global_variables_initializer()) # Calculate the linear indices in numpy. x = self.dense_to_scipy(matrix_np) expected_output = np.concatenate([ x.indices[x.indptr[i]:x.indptr[i + 1]] + i * n for i in range(m) ]) self.assertAllEqual(sess.run(output), expected_output)
def testTranspose(self, m, n, sparsity, use_gpu): # Helpers to set up the matrices. connector = connectors.Uniform(sparsity) initializer = initializers.Uniform() # Numpy matrix for verification. matrix_np = connector(initializer([m, n])) # TensorFlow graph. matrix = sparse_matrix.SparseMatrix("input", matrix=matrix_np) output = ops.transpose(matrix) # 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(np.transpose(matrix_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)