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

标签 arrays indexing 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]?

最佳答案

据我所知,您无法像使用 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/

相关文章:

java - 错误: incompatible types: int cannot be converted to int[] & other errors

c - 在 C 中写入数组的数组并显示数组的数组中的单独实体

php - MySQL 查询帮助 : Getting content from associated table with position

与列表相关的java难题

arrays - Powershell搜索文本文件以进行匹配,并在行尾添加回车符

ios - 以编程方式读取 PDF 嵌入式搜索索引

database - postgres 数据库中文本字段的合适索引是什么?

python - Keras 中的自定义 Hebbian 层实现 - 输入/输出暗淡和横向节点连接

python - 使用自定义层加载 Keras 中保存的模型,预测结果不同?

python - 将 Estimator 转换为 TPUEstimator