Reputation: 877
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.
So, as an example, I have got a 2-dimensional tensor, value (after evaluating in a session) of which looks like following:
[[ 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.
Is there a way that I can define
following 2 steps within a graph (not while executing the session):
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)
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.
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.
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]
But as of now it gives an error:
TypeError: 'Tensor' object is not iterable.
at the following line: for unique_column1_value in unique_column1.
I have followed this question: "TypeError: 'Tensor' object is not iterable" error with tensorflow Estimator which does not apply to me. I understand that I need to use while_loop but I don't know how.
Regards,
Sumit
Upvotes: 1
Views: 2962
Reputation: 75
Updated: There is a solution for when column1
is sorted here. Note that this is also a feature request for the more general version, but is closed for inactivity. The sorted version solution is like:
column1 = tf.constant([1,2,2,2,3,3,4])
column2 = tf.constant([5,6,7,8,9,10,11])
tensor1 = tf.stack([column1, column2], axis = 1)
unique_column1, unique_column1_indices, counts = tf.unique_with_counts(column1)
unique_ix = tf.cumsum(tf.pad(counts,[[1,0]]))[:-1]
output = tf.gather(tensor1, unique_ix)
which outputs: [[ 1, 5][ 2, 6][ 3, 9][ 4, 11]]
Upvotes: 1