Tensorflow - tf.matmul 个转换特征和一个向量作为批量矩阵乘法
Tensorflow - tf.matmul of conv features and a vector as a batch matmul
我尝试了以下代码
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
。当batch size为1时,这段代码就可以了,但是当它不是时我该怎么办?
我应该重塑 c1,c2,c3
吗?
我认为这段代码有效;验证过了。
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('')
我尝试了以下代码
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
。当batch size为1时,这段代码就可以了,但是当它不是时我该怎么办?
我应该重塑 c1,c2,c3
吗?
我认为这段代码有效;验证过了。
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('')