Search code examples
pythontensorflowsparse-matrix

Using Sparse Matrix Arguments in a Tensorflow Function


I am new to Tensorflow. I am trying to write a function in python using Tensorflow that operates on a sparse matrix input. Normally I would define a tensorflow placeholder, but apparently there is no placeholder for sparse matrices.

What is the proper way to define a function that operates on sparse data in tensorflow and pass values into it?

Specifically, I am trying to rewrite the fundamental example of a multilayer perceptron, found here https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py, to accept sparse input instead of dense.

As a dummy example, how would you write a function that looks something like this?

import tensorflow as tf


x = tf.placeholder("sparse")
y = tf.placeholder("float", [None, n_classes])

# Create model
def sparse_multiply(x, y):

    outlayer = tf.sparse_tensor_dense_matmul(x, y)

    return out_layer

pred = multiply(x, y)

# Launch the graph
with tf.Session() as sess:
    result = sess.run(pred, feed_dict={x: x_input, y: y_input})

Someone at the link https://github.com/tensorflow/tensorflow/issues/342 recommended, as a workaround, passing in the elements needed to construct the sparse matrix and then creating the sparse matrix on the fly within the function. That seems a little hacky, and I get errors when I try to construct it that way.

Any help, especially answers with code, would be greatly appreciated!


Solution

  • I think I figured it out. The suggestion I linked to actually did work, I just needed to correct all the inputs to have consistent types. Here is the dummy example I listed in the question, coded correctly:

    import tensorflow as tf
    
    import sklearn.feature_extraction
    import numpy as np
    
    
    def convert_csr_to_sparse_tensor_inputs(X):
        coo = X.tocoo()
        indices = np.mat([coo.row, coo.col]).transpose()
        return indices, coo.data, coo.shape
    
    
    X = ____ #Some sparse 2 x 8 csr matrix
    
    y_input = np.asarray([1, 1, 1, 1, 1, 1, 1, 1])
    y_input.shape = (8,1)
    
    
    x_indices, x_values, x_shape = convert_csr_to_sparse_tensor_inputs(X)
    
    # tf Graph input
    y = tf.placeholder(tf.float64)
    values = tf.placeholder(tf.float64) 
    indices = tf.placeholder(tf.int64)
    shape = tf.placeholder(tf.int64) 
    
    # Create model
    def multiply(values, indices, shape, y):
    
        x_tensor = tf.SparseTensor(indices, values, shape)    
    
        out_layer = tf.sparse_tensor_dense_matmul(x_tensor, y)
    
    
        return out_layer
    
    pred = multiply(values, indices, shape, y)
    
    # Launch the graph
    with tf.Session() as sess:
        result = sess.run(pred, feed_dict={values: x_values, indices: x_indices, shape: x_shape, y: y_input})