【问题标题】:tensorflow find index of the last occurrence of maximum value in a tensortensorflow 查找张量中最后一次出现最大值的索引
【发布时间】:2020-08-08 21:21:08
【问题描述】:

假设我有一个像 [[0,0,1,1,2,0][0,1,0,0,0,2]] 这样的常量张量。

[0,0,1,1,2,0] 中最后一次出现最大值的索引是4[0,1,0,0,0,2] 中最后一次出现最大值的索引是5

所以我想得到的是[4, 5],有什么想法可以做到吗?谢谢。

我使用的是 tensorflow 1.9。

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    刚刚想通了。

    import tensorflow as tf
    
    a=tf.constant([[0,0,1,1,2,0], [0,1,0,0,0,2]])
    
    max_idx = tf.cast(a.shape[1], tf.int64) - tf.argmax(tf.reverse(a, [1]), axis=1)-1
    
    sess=tf.Session()
    
    sess.run(max_idx)
    
    

    输出是:

    Out[137]: array([4, 5], dtype=int64)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2017-05-04
      • 2013-06-06
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      相关资源
      最近更新 更多