简体   繁体   中英

Tensorflow - tf.matmul of conv features and a vector as a batch matmul

I tried the following code

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)

And it makes error at tf.matmul .

What I want is just a linear combination alpha[0]*c1 + alpha[1]*c2 + alpha[2]*c3 at each sample. When batch size is 1, this code will be fine, but when it is not how can I do it?

Should I reshape c1,c2,c3 ?

I think this code works; verified it.

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('')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM