【TensorFlow隨筆】關於一個矩陣與多個矩陣相乘的問題


問題描述:

Specifically, I want to do matmul(A,B) where
 'A' has shape (m,n)
 'B' has shape (k,n,p)
and the result should have shape (k,m,p)


參考網站:

https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/4tgsOSxwtkY

https://stackoverflow.com/questions/38235555/tensorflow-matmul-of-input-matrix-with-batch-data


解決辦法:

1,我們知道TensorFlow的matmul已經支持了batch,即:

A = tf.Variable(tf.random_normal(shape=(a, b, n, m)))
B = tf.Variable(tf.random_normal(shape=(a, b, m, k)))
tf.matmul(A, B)

會返回(a,b,n,k),前面的N個維度會被保留。但是適用情景與題目不符。

2,所以我們自然想到reshape。

You can conflate the two dimensions not used in the multiplication using reshape, multiply the two matrices, and then call reshape again to get the desired shape. This is equivalent to doing batch multiplication.

簡而言之呢,就是,你可以將乘法中用不到的維度reshape到后面去,比如

(k, m, p) => (m, p * k)

進行矩陣乘法得到:(n, p * k)

之后reshape成:(k, n, p)。

雖然有些麻煩,但這是唯一的解決辦法了。

適用情景:A矩陣只有一個,但是B矩陣有batch_num個,需要逐個進行矩陣乘法的場合。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM