1

我尝试了以下代码

batch_size= 128
c1 = tf.zeros([128,32,32,16])
c2 = tf.zeros([128,32,32,16])
c3 = tf.zeros([128,32,32,16])

c = tf.stack([c1, c2, c3], 4) (size: [128, 32, 32, 16, 3])

alpha = tf.zeros([128,3,1])

M = tf.matmul(c,alpha)

它在tf.matmul.

我想要的只是 alpha[0]*c1 + alpha[1]*c2 + alpha[2]*c3每个样本的线性组合。当批量大小为 1 时,此代码会很好,但如果不是,我该怎么办?

我应该重塑c1,c2,c3吗?

4

1 回答 1

0

我认为这段代码有效;验证了它。

import tensorflow as tf
import numpy as np

batch_size= 128
c1 = tf.ones([128,32,32,16])
c2 = tf.ones([128,32,32,16])
c3 = tf.ones([128,32,32,16])

c = tf.stack([c1, c2, c3], 4)

alpha = tf.zeros([1,3])

for j in range(127):
    z = alpha[j] + 1
    z = tf.expand_dims(z,0)
    alpha = tf.concat([alpha,z],0)


M = tf.einsum('aijkl,al->aijk',c,alpha)



print('')

with tf.Session() as sess:
    _alpha = sess.run(alpha)
    _M = sess.run(M)


print('')
于 2017-06-30T00:47:02.297 回答