Tensorflow:获取零数组行的索引 [英] Tensorflow : Get indices of array rows which are zero

查看:23
本文介绍了Tensorflow:获取零数组行的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于张量

[[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]?

how can I get the indices of the 0 rows? I.e. the list [1,3], in Tensorflow?

推荐答案

据我所知,您无法像使用更高级的库(如 NumPy)那样在一个命令中真正做到这一点.如果你真的想使用 TF 函数,我可以推荐一些:

As far as I know, you can't really do that in one command like you would with a more advanced library like NumPy. If you really want to use TF functions I could suggest a few like:

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]]

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

Or you could use the following if you just want to get only the zero lines:

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]]

这篇关于Tensorflow:获取零数组行的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆