Multiplication between 3D tensor and a matrix

3,656 views
Skip to first unread message

마피아

unread,
Nov 22, 2015, 3:26:15 AM11/22/15
to Discuss
Hello,

I was trying to compute a dot product between 3d tensor and 2d tensor, but TensorFlow raises an error, saying two tensors need to have the same ranks.

How can I cope with this?

Thank you,

-Taeksoo

Mark Daoust

unread,
Nov 22, 2015, 7:10:14 PM11/22/15
to Discuss
Yeah, some of the broadcasting doesn't match numpy.

If you're trying to matrix multiply each matrix in the 3d tensor by the matrix that is the 2d tensor, like `Cijl = np.matmul(Aijk,Bkl)` you can do it with a simple reshape.
think of all the matrices in `Aijk` as being one big matrix with `i*j` rows, just folded up. So unfold it, do a regular matmul, and refold it: `tf.reshape(tf.matmul(tf.reshape(Aijk,[i*j,k]),Bkl),[i,j,l])`

ashoke...@gmail.com

unread,
Feb 3, 2017, 7:04:00 PM2/3/17
to Discuss, daou...@gmail.com

I have a 3d tensor A of size (M,N,N) and two matrices: P of size (K,M) and X of size (K,N). I want to implement the following equation in Tensorflow compactly. Can you let me know if there is a simple procedure to do the same?

Reply all
Reply to author
Forward
0 new messages