【问题标题】:Tensorflow: Iterate row-wise, elementwise multiplication, reduce-sum. Custom convolutionTensorflow:逐行迭代、逐元素乘法、归约和。自定义卷积
【发布时间】:2018-01-02 21:04:09
【问题描述】:

我有两个张量,

x = shape(batchsize, 29, 64), 
y = shape(batchsize, 29, 29, 64)

我想在 y 上逐行迭代,与 x 执行元素乘法,sum_reduce 结果,并将这些结果堆叠到新张量。 结果应该是一个形状(batchsize, 29, 64)。 它与卷积非常相似。

我将如何按顺序编程:

for batchnr in range(x.shape[0]): 
    for n in range(y.shape[1]):
        temp = tf.multiply(x[batchnr][n], y[batchnr]) #shape(29,64)
        prod = tf.reduce_sum(temp) # shape(1,64)
        res[batchnr][n] = prod

我已经创建了那个解释图:由于对每一行都进行了 reduce_sum,所以结果又是一个形状为 (batchsize, 29, 64) 的张量。

我不知道如何正确高效地进行操作。谢谢。

【问题讨论】:

    标签: python tensorflow deep-learning matrix-multiplication convolution


    【解决方案1】:

    我想我已经找到了解决方案。而不是迭代 y:

    • 将x放大为y的形状
    • 相乘
    • 减少

    在代码中是这样的:

    m = K.tf.constant([1, 29, 1], dtype=K.tf.int32)
    x = K.tf.tile(Z_RBF[0], m) #vermehrfache Z und stacke es zu RBF shape
    x = K.tf.reshape(x, shape=(-1, *Z_RBF[1].shape[1:]))
    x = K.tf.multiply(x, Z_RBF[1])
    x = K.tf.reduce_sum(x, axis=2)  # shape (batchsize, 29, 64)
    

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 2021-12-28
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2016-08-11
      • 1970-01-01
      相关资源
      最近更新 更多