[英]Multiply each matrix in a list of matrices with a unique scalar found in a vector
[英]tensorflow - multiply a vector of matrices against each matrix in another vector
如果我有两个张量-A形状为[4,3,3]
和B形状为[2,3,3]
。 对于每一个[3,3]
中的矩阵我想与每个乘以[3,3]
在乙矩阵以产生张量下用形状[4,2,3,3]
如何在Tensorflow中完成此操作?
您可以使用tf.einsum('ikl,jkl->ijkl', A, B)
:
例:
A = tf.reshape(tf.range(36), [4,3,3])
B = tf.reshape(tf.range(18), [2,3,3])
tf.einsum('ikl,jkl->ijkl', A, B)
# <tf.Tensor 'einsum/transpose_2:0' shape=(4, 2, 3, 3) dtype=int32>
您可以为此操作拆开它们:
A = tf.reshape(tf.range(36), [4,3,3])
B = tf.reshape(tf.range(18), [2,3,3])
AB=[tf.matmul(i,j) for i in tf.unstack(A,int(A.shape[0]),0) for j in tf.unstack(B,int(B.shape[0]),0)]
AB=tf.reshape(tf.stack(output,0),[4,2,3,3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.