0

I want to improve my current code in order to improve the execution performance on my GPU so I am replacing the operations that it does not support to avoid delegating them to the CPU.

One of this operations is tf.sparse_to_dense. So, is there some way to create a Tensor (constant) from its indices and values as if it were an Sparse Tensor?

I made it work with workarounds like getting the array using numpy and then creating it with tensor = tf.constant(numpyarray) but I was looking for an "only Tensorflow" approach.

dg1996
  • 25
  • 6

1 Answers1

0

tf.constant currently does not support instantiation in coordinate format (indices and values) so the numpy/scipy workaround is actually not a bad one:

import scipy.sparse as sps

A = sps.coo_matrix((values, (indices[0,:], indices[1,:])), shape=your_shape)
tensor_A = tf.constant(A.to_dense())

If having a non-trainable tf.Variable can be an option (see here for the differences with tf.constant), you could use tf.sparse_to_dense

tensor_A = tf.Variable( \
              tf.sparse_to_dense(indices, your_shape, values), \
              trainable=False \
           )
syltruong
  • 2,563
  • 20
  • 33
  • `tf.sparse_to_dense` is my current implementation, but I was trying to replace it since my GPU doesn't support it. – dg1996 Aug 02 '18 at 23:12