Sparse Tensor (matrix) from a dense Tensor Tensorflow
You can use tf.where and tf.gather_nd to do that: import numpy as np import tensorflow as tf # Make a tensor from a constant a = np.reshape(np.arange(24), (3, 4, 2)) a_t = tf.constant(a) # Find indices where the tensor is not zero idx = tf.where(tf.not_equal(a_t, 0)) # Make the sparse tensor # Use tf.shape(a_t, out_type=tf.int64) … Read more