[英]How can I take out(or slice) the elements in a rank-2 tensor , whose first element is unique?
My title might be ambiguous due to my awkward English. 由于我的英语不好,我的头衔可能模棱两可。 But I mean this: suppose i have a tensor
a
like this: 但是我的意思是:假设我有
a
像这样的张量:
array([[1, 2, 3],
[2, 2, 3],
[2, 2, 4],
[3, 2, 3],
[4, 2, 3]], dtype=int32)
the 'first column' of this tensor could contain duplicate elements (eg [1, 2, 2, 3, 4] or [1, 1, 2, 3, 3, 4, 5, 5]), and which element is duplicated is not known beforehand. 张量的“第一列”可能包含重复元素(例如[1、2、2、3、4]或[1、2、3、3、4、5、5]),并且哪个元素是重复的事先未知。
and i wanna take out a tensor this: 我想取出张量:
array([[1, 2, 3],
[2, 2, 3],
[3, 2, 3],
[4, 2, 3]], dtype=int32)
as u can see, I take out the rows whose first element is a unique element in the column of a
. 就像你看到的,我拿出它的第一个元素是在该列中的独特元素的行
a
。
I first wanted to use the function tf.unique()
. 我首先想使用功能
tf.unique()
。 BUT the idx
value returned by it doesn't indicate the first index of each value of output tensor in the original tensor. 但是它返回的
idx
值并不表示输出张量的每个值在原始张量中的第一个索引。
tf.unique()
works like this: 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]
The function tf.unique(x, name=None)
finds the unique elements in a 1-D tensor. 函数
tf.unique(x, name=None)
查找一维张量中的唯一元素。 And it now returns two value: y
and idx
. 现在它返回两个值:
y
和idx
。 y
contains all of the unique elements of x
sorted inthe same order that they occur in x
. y
包含x
所有唯一元素,其排序顺序与它们在x
出现的顺序相同。 idx
contains the index of each value of x
in the unique output y
. idx
包含唯一输出y
中x
的每个值的索引。
How I wish it has a third return value which contains the first index of each value of y
in the original tensor x
is also needed. 我多么希望它具有第三个返回值,该值包含原始张量
x
中y
的每个值的第一个索引。 It might work like this: 它可能像这样工作:
# 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]
Just like its equivalent in Numpy does: 就像它在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]
IF i have this idx_ori
, i can solve my problem by tf.gather()
: 如果我有这个
idx_ori
,我可以通过tf.gather()
解决我的问题:
_, _1, idx_ori = tf.unique(a[:, 0])
result = tf.gather(a, idx_ori)
Any idea to workaround this problem? 有解决此问题的想法吗? or any idea to get this indices that i want.
或任何想法来获得我想要的索引。
PS I know my description is tediously long ... :-p 附言:我知道我的描述很冗长... :-p
This is a bit gross, but you could do: 这有点麻烦,但是您可以执行以下操作:
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.