How can I take out(or slice) the elements in a rank-2 tensor , whose first element is unique?
<p>My title might be ambiguous due to my awkward English. But I mean this:
suppose i have a tensor <code>a</code> like this:</p>

<pre><code>array([[1, 2, 3],
       [2, 2, 3],
       [2, 2, 4],
       [3, 2, 3],
       [4, 2, 3]], dtype=int32)
</code></pre>

<p><strong>the 'first column' of this tensor could contain duplicate elements (e.g.  [1, 2, 2, 3, 4] or [1, 1, 2, 3, 3, 4, 5, 5]), and which element is duplicated is not known beforehand.</strong></p>

<p>and i wanna take out a tensor this:</p>

<pre><code>array([[1, 2, 3],
       [2, 2, 3],
       [3, 2, 3],
       [4, 2, 3]], dtype=int32)
</code></pre>

<p>as u can see, I take out the rows whose first element is a unique element in the column of <code>a</code>.  </p>

<p>I first wanted to use the function <code>tf.unique()</code> . BUT the <code>idx</code> value returned by it doesn't indicate the first index of each value of output tensor in the original tensor.</p>

<p><code>tf.unique()</code> works like this:</p>

<pre><code># tensor 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8]
y, idx = tf.unique(x)
y ==&gt; [1, 2, 3, 7, 8]
idx ==&gt; [0, 0, 1, 2, 2, 2, 3, 4, 4]
</code></pre>

<p>The function <code>tf.unique(x, name=None)</code> finds the unique elements in a 1-D tensor. And it now returns two value: <code>y</code> and <code>idx</code>. <code>y</code> contains all of the unique elements of <code>x</code> sorted inthe same order that they occur in <code>x</code>. <code>idx</code> contains the index of each value of <code>x</code> in the unique output <code>y</code>.</p>

<p>How I wish it has a third return value which contains the first index of each value of <code>y</code> in the original tensor <code>x</code> is also needed. It might work like this:</p>

<pre><code># tensor 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8]
y, idx, idx_ori  = tf.unique(x)
y ==&gt; [1, 2, 3, 7, 8]
idx ==&gt; [0, 0, 1, 2, 2, 2, 3, 4, 4]
idx_ori ==&gt; [0, 2, 3, 6, 7]
</code></pre>

<p>Just like its equivalent in Numpy does:</p>

<pre><code>array 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8]
y, idx_ori = np.unique(x, return_index=True)
y ==&gt; [1, 2, 3, 7, 8]
idx_ori ==&gt; [0, 2, 3, 6, 7]
</code></pre>

<p>IF i have this <code>idx_ori</code>, i can solve my problem by <code>tf.gather()</code>:</p>

<pre><code>_, _1, idx_ori = tf.unique(a[:, 0])
result = tf.gather(a, idx_ori)
</code></pre>

<p>Any idea to workaround this problem?   or any idea to get this indices that i want.</p>

<p>P.S. I know my description is tediously long ... :-p</p>

<p>This is a bit gross, but you could do:</p>

<pre><code>print a
y, idx = tf.unique(a[:,0])
z = tf.one_hot(idx, tf.shape(y)[0])
s = tf.cumsum(z)
e = tf.equal(s, 1)  # only seen once so far
ss = tf.to_int32(e) * tf.to_int32(z) # and we equal the thing
m = tf.reduce_max(ss, reduction_indices=1)
out = tf.boolean_mask(a, tf.equal(m, 1))
sess = tf.Session()
print sess.run(out)

[[1 2 3]
 [2 2 3]
 [2 2 4]
 [3 2 3]
 [4 2 3]]
[[1 2 3]
 [2 2 3]
 [3 2 3]
 [4 2 3]]
</code></pre>

python numpy tensorflow unique