繁体   English   中英

张量加权平均减少

[英]Tensor weighted mean reduce

使用张量x和权重w作为输入:

w = [1, 1, 0]
x = tf.constant(
    [[[1., 2.],
      [5., 3.],
      [6., 4.]],

     [[5., 7.],
      [10., 8.],
      [11., 9.]]]
)


有人怎么能输出加权的reduce_mean张量y?

y = tf.constant(
    [[w[0]*[1., 2.],
      w[1]*[5., 3.],
      w[2]*[6., 4.]],

     [w[0]*[5., 7.],
      w[1]*[10., 8.],
      w[2]*[11., 9.]]]
)

预期结果是(平均值的计算方法是总和除以权重1的数量):

y = tf.constant(
    [[3., 2.5]],

     [[7.5, 7.5]]
)

检查此代码,它给出了您想要的答案,解决方案是多次使用map_fn

w = tf.constant([1.0, 1.0, 0.0])
x = tf.constant(
    [[[1., 2.],
      [5., 3.],
      [6., 4.]],

     [[5., 7.],
      [10., 8.],
      [11., 9.]]]
)

def apply_weight(x, w): 
    return tf.map_fn(mult, x)

def mult(a):
    transposed_a = tf.transpose(a)
    return tf.map_fn(mult_pars, transposed_a)

def mult_pars(b): 
    return tf.reduce_sum(w * b) / tf.reduce_sum(w)

print(apply_weight(x,w))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM