[英]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.