![](/img/trans.png)
[英]find the sublist whose first element is the maximum that is no greater than a given number
[英]Get the tensors element whose counts are greater than 1
我有一个像这样的 TF 张量x = tf.constant([0, 5, 5, 4, 9, 8, 0, 4])
我想获取计数大于 1 的元素,例如[5, 4]
. 这相当于 numpy .count
,就像这样[elem for elem in x if x.count(elem) > 1]
。
我怎么能在 tensorflow 中做到这一点? 谢谢
您可以使用tf.unique_with_counts
。 它为您提供计数张量和具有相应索引的唯一值
x = tf.convert_to_tensor([1, 1, 2, 4, 4, 4, 7, 8, 8])
y, idx, count = tf.unique_with_counts(x)
# y ==> [1, 2, 4, 7, 8]
# idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
# count ==> [2, 1, 3, 1, 2]
在此之后,您可以简单地执行基于条件的过滤器
y_gt_1 = y[count>1]
# y_gt_1 ==> [1, 4, 8]
from collections import Counter
new_list=Counter([int(element) for element in x])
result=[item for item in new_list if new_list[item]>1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.