2016-09-28 40 views
0

由于我的英语尴尬,我的标题可能不明确。但是,我的意思是这样的: 假设我有一个张量a这样的:如何取出(或切片)第一个元素是唯一的等级2张量中的元素?

array([[1, 2, 3], 
     [2, 2, 3], 
     [2, 2, 4], 
     [3, 2, 3], 
     [4, 2, 3]], dtype=int32) 

这个张量的 '第一列中的' 可以包含重复的元素(例如,[1,2,2,3,4]或[ 1,1,2,3,3,4,5,5]),并且哪个元素是重复的,事先是未知的。

,我想带出一个张这样的:

array([[1, 2, 3], 
     [2, 2, 3], 
     [3, 2, 3], 
     [4, 2, 3]], dtype=int32) 

就像你看到的,我拿出它的第一个元素是在a列中的独特元素的行。

我第一次想用的功能tf.unique()。但它返回的idx值并不表示输出张量在原张量中的每个值的第一个索引。

tf.unique()是这样的:

# tensor 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8] 
y, idx = tf.unique(x) 
y ==> [1, 2, 3, 7, 8] 
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] 

功能tf.unique(x, name=None)发现在1-d张量的唯一元素。现在它返回两个值:yidxy包含了所有排序在矿井相同顺序发生在xx的独特元素。 idx包含唯一输出y中每个值x的索引。

我如何希望它有第三个返回值,其中包含原始张量x中的每个值y的第一个索引也是需要的。它可能像这样工作:

# tensor 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8] 
y, idx, idx_ori = tf.unique(x) 
y ==> [1, 2, 3, 7, 8] 
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] 
idx_ori ==> [0, 2, 3, 6, 7] 

就像它相当于在numpy的作用:

array 'x' is [1, 1, 2, 3, 3, 3, 7, 8, 8] 
y, idx_ori = np.unique(x, return_index=True) 
y ==> [1, 2, 3, 7, 8] 
idx_ori ==> [0, 2, 3, 6, 7] 

,如果我有这个idx_ori,我可以通过tf.gather()解决我的问题:

_, _1, idx_ori = tf.unique(a[:, 0]) 
result = tf.gather(a, idx_ori) 

任何想法解决这个问题?或任何想法得到我想要的这个指标。

P.S.我知道我的描述是冗长...... :-P

回答

0

这是一个有点恶心,但你可以这样做:

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]] 
相关问题