【问题标题】:Subtract columns in Tensorflow tensor减去 Tensorflow 张量中的列
【发布时间】:2017-01-17 08:37:21
【问题描述】:

在 Tensorflow 中,我想从另一列中减去 2D 张量中的一列。我查看了使用 tf.split()tf.slice() 拆分为 2 个张量然后相减,但这似乎不必要地复杂。我目前的方法是将一列乘以 -1,然后 reduce_sum 列:

input = tf.constant(
        [[5.8, 3.0],
         [4.0, 6.0],
         [7.0, 9.0]])
oneMinusOne = tf.constant([1., -1.])
temp = tf.mul(input, oneMinusOne)
delta = tf.reduce_sum(temp, 1)

看起来仍然是不必要的复杂。有没有更简单的方法?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    numpy 的许多数组索引在 TensorFlow 中按预期工作。以下作品:

    input = tf.constant(
        [[5.8, 3.0],
         [4.0, 6.0],
         [7.0, 9.0]])
    sess = tf.InteractiveSession()
    ans = input[:, :1] - input[:, 1:]
    print(ans.eval())
    
    array([[ 2.80000019],
       [-2.        ],
       [-2.        ]], dtype=float32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多