title: TypeError: 'Tensor' object is not iterable
body: <p>In tensor flow, I have got a tensor with 512 rows and 2 columns. What I want to do is that: filter column 2 of the tensor on the basis of unique values of column 1 and then for each unique value (of column 1) process corresponding values of column 2 in the inner loop.</p>
  
  <p>So, as an example, I have got a 2-dimensional tensor, value (after evaluating in a session) of which looks like following:   </p>
  
  <pre><code>[[ 509,  270],
  [ 533,  568],
  [ 472,  232],
  ..., 
  [   6,  276],
  [ 331,  165],
  [ 401, 1144]]
  
  509, 533, 472 ... are elements of column1 and 270, 568, 232,... are elements of column 2.
  </code></pre>
  
  <p>Is there a way that I can <code>define</code> following 2 steps within a graph (not while executing the session):</p>
  
  <pre><code>  get unique values of column1
    for each `unique_value` in column1:
        values_in_column2 = values in column2 corresponding to `unique_value` (filter column2 according to unique_value`)
        some_function(values_in_column2)
  </code></pre>
  
  <p>I can do above steps while running the session but I would like to define the above 2 steps in a graph - which I can run in session after defining many subsequent steps.</p>
  
  <p>Is there any way to do this? Appreciate any kind of help in this regard.
  Here is pseudo code for what I want to do. </p>
  
  <pre><code>      tensor1 = tf.stack([column1, column2], axis = 1)
        column1 = tensor1[0, :]
        unique_column1, unique_column1_indexes = tf.unique(column1)
        for unique_column1_value in unique_column1:
            column1_2_indexes = tf.where(column1 == unique_column1_value)
            corresponding_column2_values = tensor1[column1_2_indexes][:, 1]
  </code></pre>
  
  <p>But as of now it gives an error: </p>
  
  <pre><code>  TypeError: 'Tensor' object is not iterable.
  </code></pre>
  
  <p>at the following line:
        for unique_column1_value in unique_column1.</p>
  
  <p>I have followed this question: <a href="https://stackoverflow.com/questions/47241010/typeerror-tensor-object-is-not-iterable-error-with-tensorflow-estimator">&quot;TypeError: &#39;Tensor&#39; object is not iterable&quot; error with tensorflow Estimator</a> which does not apply to me.
  I understand that I need to use <a href="https://www.tensorflow.org/api_docs/python/tf/while_loop" rel="nofollow noreferrer">while_loop</a> but I don't know how.</p>
  
  <p>Regards,<br>
  Sumit</p>