【问题标题】:tensorflow comparing each element of a tensortensorflow 比较张量的每个元素
【发布时间】:2017-12-19 14:01:18
【问题描述】:

我的输入张量大小为3x5。 我试图获得值大于 1 的每个张量的总数。 例如:

input list[[0.1 , 1.1 , 1.3, 1.5 , 0.7] ,
           [1.1 , 1.1 , 0.8, 1.5 , 0.7] ,
           [0.1 , 0.0 , 1.3, 0.5 , 1.7]]  
return[[3],[3],[2]]  

因为在列表中,有 3 , 3 , 2 个数值大于 1 的数字。

本来希望用下面的代码来解决的,

tf.reduce_sum(tf.where(tf.greater(inputs , one),one,zero),1)

'one'是一个张量,大小为3x5,所有值都为1。'零'是一个张量,大小为3x5,所有值为零。
但我随后意识到 tf.greater 不能以这种方式使用。有没有推荐的代码来解决我的问题?我已经被这个问题困扰了很长一段时间,还没有找到解决方案。非常感谢!

【问题讨论】:

    标签: tensorflow compare


    【解决方案1】:

    返回

    [3 3 2]

    import tensorflow as tf
    
    inputlist = [[0.1 , 1.1 , 1.3, 1.5 , 0.7] ,
               [1.1 , 1.1 , 0.8, 1.5 , 0.7] ,
               [0.1 , 0.0 , 1.3, 0.5 , 1.7]]
    
    x = tf.Variable(initial_value=inputlist)
    
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    
    print( sess.run( tf.count_nonzero(( tf.greater(inputlist, tf.constant(1.0)) ), 1)) )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      相关资源
      最近更新 更多