In TensorFlow, how can I get nonzero values and their indices from a tensor with python?
<p>I want to do something like this.<br>
Let's say we have a tensor A.  </p>

<pre><code>A = [[1,0],[0,4]]
</code></pre>

<p>And I want to get nonzero values and their indices from it.  </p>

<pre><code>Nonzero values: [1,4]  
Nonzero indices: [[0,0],[1,1]]
</code></pre>

<p>There are similar operations in Numpy.<br>
<code>np.flatnonzero(A)</code> return indices that are non-zero in the flattened A.<br>
<code>x.ravel()[np.flatnonzero(x)]</code> extract elements according to non-zero indices.<br>
Here's <a href="http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.flatnonzero.html" rel="nofollow">a link</a> for these operations.</p>

<p>How can I do somthing like above Numpy operations in Tensorflow with python?<br>
(Whether a matrix is flattened or not doesn't really matter.)</p>

<p>You can achieve same result in Tensorflow using <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops.html#not_equal" rel="nofollow">not_equal</a> and <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops.html#where" rel="nofollow">where</a> methods. </p>

<pre><code>zero = tf.constant(0, dtype=tf.float32)
where = tf.not_equal(A, zero)
</code></pre>

<p><code>where</code> is a tensor of the same shape as <code>A</code> holding <code>True</code> or <code>False</code>, in the following case </p>

<pre><code>[[True, False],
 [False, True]]
</code></pre>

<p>This would be sufficient to select zero or non-zero elements from <code>A</code>. If you want to obtain indices you can use <code>where</code>method as follows:</p>

<pre><code>indices = tf.where(where)
</code></pre>

<p><code>where</code> tensor has two <code>True</code> values so <code>indices</code> tensor will have two entries. <code>where</code> tensor has rank of two, so entries will have two indices:</p>

<pre><code>[[0, 0],
 [1, 1]]
</code></pre>

python tensorflow indices