[英]tensorflow, How to index the true value of a tensor?
我有一個一維張量,像:
[false, false, true, false, true, false]
如何找到所有true
值的索引?
我的解決方案是:
false
/ 0
。 true
/ 1
值 但是這種解決方案不是很好。
import tensorflow as tf
a = tf.constant([False,False,True,False,True],dtype=tf.bool)
b = tf.where(a)
sess = tf.Session()
print(sess.run(b))
這是您要找的東西嗎? [k for k, value in enumerate(tensor) if value]
In [1]: import tensorflow as tf
In [2]: a = tf.constant([False, False, True, True])
In [3]: a_n = [tf.cond(tf.equal(v, tf.constant(True)), lambda: tf.constant(k), lambda: tf.constant(-1)) for k, v in enumerate(tf.unstack(a))]
In [4]: sess = tf.Session()
In [5]: sess.run(a_n)
Out[5]: [-1, -1, 2, 3]
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.