如何在 TensorFlow 中批量执行矩阵标量乘法?
How to do matrix-scalar multiplication in TensorFlow with batch?
一开始,我会描述公式,我要计算的是什么:
Math formula on google chat api(我不能post直接上图。)
其中 I 是形状为 (M,M) 的单位矩阵,N_i 是向量 (C),T 是矩阵 (C*F,M),T_c 是子矩阵形状 (F,M).
我的 tensorflow 枚举代码如下所示:
N_p = tf.placeholder(floatX, shape=[C], name='N_p')
I = tf.Variable(np.eye(M),dtype=tf.float32, name="I")
T = tf.Variable(np.random.rand(C*F,M),dtype=tf.float32, name="T")
L = I
for i,T_c in enumerate([T[i:i+F,:] for i in xrange(0,F*C,F)]):
L=tf.add(L,tf.scalar_mul(N_p[i],tf.matmul(tf.transpose(T_c),T_c)))
这很好用,不幸的是,我需要将其扩展为批处理,这里 N_p 将是:
N_p = tf.placeholder(floatX, shape=[None,C], name='N_p')
不幸的是,我不知道或更改我的 tensorflow 公式。
问题出在 scalar_mul.
L=tf.add(L,tf.scalar_mul(N_p[:,i],tf.matmul(tf.transpose(T_c),T_c)))
为什么很明显,但如何重写呢?
非常感谢任何建议。
您可以以矩阵形式实现上述内容,无需任何循环:
T --> [C, F, M]
T_1 --> transpose T to --> [C, F, M]
T_2 --> transpose T to --> [C, M, F]
d --> matmul (T_1, T_2) --> [C, M, M] --> transpose --> [M, M, C]
out --> multiply (d, N) : d -> [1, M, M, C], N -> [batch, 1, 1, C]
--> [batch, M, M, C] --> reduce_sum (axis=2) --> [batch, M, M]
--> add I
工作代码(与 batch=1
的代码匹配):
N_1 = tf.placeholder(tf.float32, [None, C])
reshape_T = tf.reshape(T, [C, F, M])
# reshape to do a batch matrix multiplication (C, F, M) and (C, M, F)
T_1 = tf.transpose(reshape_T, [0, 2, 1])
T_2 = tf.transpose(reshape_T, [0, 1, 2])
d = tf.transpose(tf.matmul(T_1,T_2), [2,1,0])
out = tf.reduce_sum(d[None,...]* tf.reshape(N_1, [-1, 1, 1, C]), axis=3) + I
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(out, {N_1: inp}))
一开始,我会描述公式,我要计算的是什么:
Math formula on google chat api(我不能post直接上图。)
其中 I 是形状为 (M,M) 的单位矩阵,N_i 是向量 (C),T 是矩阵 (C*F,M),T_c 是子矩阵形状 (F,M).
我的 tensorflow 枚举代码如下所示:
N_p = tf.placeholder(floatX, shape=[C], name='N_p')
I = tf.Variable(np.eye(M),dtype=tf.float32, name="I")
T = tf.Variable(np.random.rand(C*F,M),dtype=tf.float32, name="T")
L = I
for i,T_c in enumerate([T[i:i+F,:] for i in xrange(0,F*C,F)]):
L=tf.add(L,tf.scalar_mul(N_p[i],tf.matmul(tf.transpose(T_c),T_c)))
这很好用,不幸的是,我需要将其扩展为批处理,这里 N_p 将是:
N_p = tf.placeholder(floatX, shape=[None,C], name='N_p')
不幸的是,我不知道或更改我的 tensorflow 公式。 问题出在 scalar_mul.
L=tf.add(L,tf.scalar_mul(N_p[:,i],tf.matmul(tf.transpose(T_c),T_c)))
为什么很明显,但如何重写呢? 非常感谢任何建议。
您可以以矩阵形式实现上述内容,无需任何循环:
T --> [C, F, M]
T_1 --> transpose T to --> [C, F, M]
T_2 --> transpose T to --> [C, M, F]
d --> matmul (T_1, T_2) --> [C, M, M] --> transpose --> [M, M, C]
out --> multiply (d, N) : d -> [1, M, M, C], N -> [batch, 1, 1, C]
--> [batch, M, M, C] --> reduce_sum (axis=2) --> [batch, M, M]
--> add I
工作代码(与 batch=1
的代码匹配):
N_1 = tf.placeholder(tf.float32, [None, C])
reshape_T = tf.reshape(T, [C, F, M])
# reshape to do a batch matrix multiplication (C, F, M) and (C, M, F)
T_1 = tf.transpose(reshape_T, [0, 2, 1])
T_2 = tf.transpose(reshape_T, [0, 1, 2])
d = tf.transpose(tf.matmul(T_1,T_2), [2,1,0])
out = tf.reduce_sum(d[None,...]* tf.reshape(N_1, [-1, 1, 1, C]), axis=3) + I
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(out, {N_1: inp}))