【问题标题】:tensorflow mask boxes with selected indices具有选定索引的张量流掩码框
【发布时间】:2018-05-06 00:44:05
【问题描述】:

假设我有一个 2 阶张量 A [[1,1,1,1], [2,2,2,2],[3,3,3,3], [4, 4, 4, 4], ...],并且我有一个选定的索引 B(来自 tf.equal() 或其他地方),例如 @987654323 @ .我想让 A[i] 对于 B 中的任何 i 都为零,以便 A 最终变成类似[1,1,1,1], [0,0,0,0],[3,3,3,3], [0,0,0,0], ...]。怎么做或者有可能吗?

【问题讨论】:

    标签: numpy tensorflow


    【解决方案1】:

    有多种方法可以做到这一点。这是一个tf.one_hot()(测试代码):

    import tensorflow as tf
    
    a = tf.constant( [[1,1,1,1], [2,2,2,2],[3,3,3,3], [4, 4, 4, 4]] )
    b = tf.constant( [ 1, 3, 4 ] )
    
    one_hot = tf.one_hot( b, a.get_shape()[ 0 ].value, dtype = a.dtype )
    mask = 1 - tf.reduce_sum( one_hot, axis = 0 )
    res = a * mask[ ..., None ]
    
    with tf.Session() as sess:
        print( sess.run( res ) )
    

    或者这个tf.scatter_nd()(测试代码):

    import tensorflow as tf
    
    a = tf.constant( [[1,1,1,1], [2,2,2,2], [3,3,3,3], [4, 4, 4, 4]] )
    b = tf.constant( [ 1, 3 ] )
    
    mask = 1 - tf.scatter_nd( b[ ..., None ], tf.ones_like( b ), shape = [ a.get_shape()[ 0 ].value ] )
    res = a * mask[ ..., None ]
    
    with tf.Session() as sess:
        print( sess.run( res ) )
    

    都会输出:

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

    根据需要。

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 2019-11-05
      • 2016-02-19
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2019-05-30
      • 1970-01-01
      相关资源
      最近更新 更多