gpt4 book ai didi

arrays - tensorflow : Get indices of array rows which are zero

转载 作者:行者123 更新时间:2023-12-02 06:20:46 24 4
gpt4 key购买 nike

对于张量

[[1 2 3 1]
[0 0 0 0]
[1 3 5 7]
[0 0 0 0]
[3 5 7 8]]

如何获取 0 行的索引? IE。 Tensorflow 中的列表 [1,3]?

最佳答案

据我所知,您无法像使用 NumPy 等更高级的库那样通过一个命令真正做到这一点。如果你真的想使用 TF 函数,我可以推荐一些类似的函数:

x = tf.Variable([
[1,2,3,1],
[0,0,0,0],
[1,3,5,7],
[0,0,0,0],
[3,5,7,8]])

y = tf.Variable([0,0,0,0])
condition = tf.equal(x, y)
indices = tf.where(condition)

这将导致以下结果:

[[1 0]
[1 1]
[1 2]
[1 3]
[3 0]
[3 1]
[3 2]
[3 3]]

或者,如果您只想获取零行,则可以使用以下内容:

row_wise_sum = tf.reduce_sum(tf.abs(x),1)
select_zero_sum = tf.where(tf.equal(row_wise_sum,0))

with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(select_zero_sum))

结果是:

[[1]
[3]]

关于arrays - tensorflow : Get indices of array rows which are zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41806689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com