【问题标题】:TensorFlow multiplication along axis沿轴的 TensorFlow 乘法
【发布时间】:2021-11-17 18:54:20
【问题描述】:

我只想像这样沿着给定的轴相乘:

a = tf.ones([2,2,3])
b = tf.constant(7)
c = //multiply a[:,:,1] with b

所以c[...,0]c[...,2] 有一个,而c[...,1] 有一个七:

print(c.shape)
> (2, 2, 3)

print(a[...,0]) //output same for a[...,1] and a[...,2]
> tf.Tensor(
[[1. 1.]
 [1. 1.]], shape=(2, 2), dtype=float32)

print(c[...,0])
>tf.Tensor(
 [[1. 1.]
  [1. 1.]], shape=(2, 2), dtype=float32)


print(c[...,1])
>tf.Tensor(
 [[7. 7.]
  [7. 7.]], shape=(2, 2), dtype=float32)


print(c[...,2])
>tf.Tensor(
 [[1. 1.]
  [1. 1.]], shape=(2, 2), dtype=float32)

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    我不太确定您期望什么结果,但如果我理解正确,您可以这样做:

    import tensorflow as tf
    
    a = tf.concat([tf.ones([1, 4, 3], dtype=tf.float32), 
                   tf.ones([1, 4, 3], dtype=tf.float32) * 3, 
                   tf.zeros([2, 4, 3], dtype=tf.float32)], axis=0)
    b = tf.constant(7, dtype=tf.float32)
    
    tensor = tf.slice(a,
                   begin=[1, 0, 0],
                   size=[1, 4, 3])
    c = tensor * b
    result = tf.tensor_scatter_nd_update(a, [[1]], c)
    print('a -->', a, '\n')
    print('c -->', c, '\n')
    print('result -->', result)
    
    a --> tf.Tensor(
    [[[1. 1. 1.]
      [1. 1. 1.]
      [1. 1. 1.]
      [1. 1. 1.]]
    
     [[3. 3. 3.]
      [3. 3. 3.]
      [3. 3. 3.]
      [3. 3. 3.]]
    
     [[0. 0. 0.]
      [0. 0. 0.]
      [0. 0. 0.]
      [0. 0. 0.]]
    
     [[0. 0. 0.]
      [0. 0. 0.]
      [0. 0. 0.]
      [0. 0. 0.]]], shape=(4, 4, 3), dtype=float32) 
    
    c --> tf.Tensor(
    [[[21. 21. 21.]
      [21. 21. 21.]
      [21. 21. 21.]
      [21. 21. 21.]]], shape=(1, 4, 3), dtype=float32) 
    
    result --> tf.Tensor(
    [[[ 1.  1.  1.]
      [ 1.  1.  1.]
      [ 1.  1.  1.]
      [ 1.  1.  1.]]
    
     [[21. 21. 21.]
      [21. 21. 21.]
      [21. 21. 21.]
      [21. 21. 21.]]
    
     [[ 0.  0.  0.]
      [ 0.  0.  0.]
      [ 0.  0.  0.]
      [ 0.  0.  0.]]
    
     [[ 0.  0.  0.]
      [ 0.  0.  0.]
      [ 0.  0.  0.]
      [ 0.  0.  0.]]], shape=(4, 4, 3), dtype=float32)
    

    更新:按照你想要的方式对数据进行切片并不是你想象的那样:

    import tensorflow as tf
    
    a = tf.concat([tf.ones([1, 2, 3], dtype=tf.float32), 
                   tf.zeros([1, 2, 3], dtype=tf.float32)], axis=0)
    
    b = tf.ones([2, 2, 3], dtype=tf.float32)
    
    print(a.shape, a[...,0])
    print(b.shape, b[...,0])
    
    (2, 2, 3) tf.Tensor(
    [[1. 1.]
     [0. 0.]], shape=(2, 2), dtype=float32)
    (2, 2, 3) tf.Tensor(
    [[1. 1.]
     [1. 1.]], shape=(2, 2), dtype=float32)
    

    正如您在使用具有混合值的 (2, 2, 3) 张量时所看到的,当沿最后一个轴切片时,您不仅会得到预期的结果。

    【讨论】:

    • 不安静。 result[:,:,1] 应该是全七,但在上面提出的解决方案中,它们仅在 result[:,:,1][1] 中,其他都是七。
    • 感谢您的回复,您能用您想要的结果示例更新您的问题吗?我认为这也会对其他人有所帮助。也许使用更小的张量:(2, 2, 3)。我认为这也取决于你如何分割你的张量。例如,result[1, :, :] 将导致所有七人制。
    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 2017-12-27
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2021-12-12
    • 2018-09-01
    相关资源
    最近更新 更多