简体   繁体   English

我如何取出(或切片)秩2张量中的元素,其第一个元素是唯一的?

[英]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 . 现在它返回两个值: yidx 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包含唯一输出yx的每个值的索引。

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. 我多么希望它具有第三个返回值,该值包含原始张量xy的每个值的第一个索引。 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.

相关问题 如何将 PyTorch 张量与另一个张量切片? - How can I slice a PyTorch tensor with another tensor? 我怎样才能采取元素形式 beautifulsoup 跳过前几个和最后几个元素? - How can i take element form beautifulsoup skipping first few and last few elements? 如何检索列表中第一个元素的唯一值? - How can I retrieve the unique values of the first element of a list? 如何访问张量中的每个元素? - How can I access each elements in tensor? Pytorch:如何在二维张量的每一行中找到第一个非零元素的索引? - Pytorch: How can I find indices of first nonzero element in each row of a 2D tensor? rank1 张量的 20 个元素的切片然后重塑抛出“重塑的输入是具有 10272 个值的张量,但请求的形状需要 20 的倍数” - Slice of 20 elements of rank1 tensor then reshaping throws "Input to reshape is tensor with 10272 values, but requested shape requires multiple of 20" Tensorflow:如何将1级张量转换为2级张量 - Tensorflow: How to convert a rank 1 tensor into a rank 2 tensor 如何从 Tensorflow 张量中获取第二个最大值? - How can I take the 2nd max from a Tensorflow tensor? 如何在N张量(ndarray)中选择我选择的2个指数 - How can I contract 2 indices at my choice in a rank N tensor (ndarray) 如何获取张量张量的元素日志,然后将每个张量乘以tf中的不同标量? - How can I take the elementwise log of a tensor of tensors and then multiply each tensor by different scalars in tf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM