1

Note: I have read the TensorFlow guide for Save and Restore

I have a function which requires a constant SparseTensor as input and it takes a while to run:

sparse = coo_matrix(dense)

sparse_indicies = list(zip(
    sparse.row.astype(np.int64).tolist(), 
    sparse.col.astype(np.int64).tolist()
))

input_tensor = tf.SparseTensor(
    indices     = sparse_indicies,
    values      = (sparse.data).astype(np.float32),
    dense_shape = sparse.shape
)

I just want to be able to save input_tensor and load it later when needed. I am not running in eager execution.

Does that mean I have to do this:

file = 'tensor_{}_{}.ckpt'.format(*sparse.shape)


# save
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(init_op)
    save_path = saver.save(sess, file)

and then

# reload
tf.reset_default_graph()

junk, n_rows, n_cols = file.split('.')[0].split('_')
please_work = tf.get_variable("input_tensor", shape=[n_rows, n_cols])


saver = tf.train.Saver()

with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "i_just_want_one_tensor.ckpt")
SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • Yes , i think it should work normally , but the saver will save all elements of your tf graph – Mehdi Bahra Dec 01 '18 at 11:41
  • @MehdiBahra I'm aware... but I do not think there is a way to just get the tensor in a saved file (e.g. `pickle` , `json`, `.npy`) – SumNeuron Dec 01 '18 at 17:30
  • @MehdiBahra trying to run this throws a `ValueError` because there are no variables to save – SumNeuron Dec 02 '18 at 10:55
  • You need to create a variable from it in order to save it , check this stackoverflow thread ,the second response ,https://stackoverflow.com/questions/37001686/using-sparsetensor-as-a-trainable-variable – Mehdi Bahra Dec 02 '18 at 11:13
  • @MehdiBahra it says it is not supported – SumNeuron Dec 02 '18 at 11:16
  • i think you can save it , if you turn it to a variable like follow b_dense = tf.sparse_tensor_to_dense(input_tensor) b_variable = tf.Variable(b_dense) – Mehdi Bahra Dec 02 '18 at 11:19
  • @MehdiBahra, TensorFlow's file formats are not know for being efficient in either size or read speeds. The core issue is that an instance of `SparseTensor` takes forever and this is the lag I wish to circumvent. – SumNeuron Dec 02 '18 at 11:26

0 Answers0