tf.contrib.keras.backend.dot() or tf.matmul()? Matrix Multiplication with Different Rank – TensorFlow Tutorial

By | February 18, 2021

When we are multiplying two matrices with different ranks, we may get this error: ValueError: Shape must be rank 2 but is rank 3. In this tutorial, we will introduce how to multiply two matrices with different ranks in tensorflow.

tf.matmul()

TensorFlow tf.matmul() can multiply matrix. However, it may report ValueError when multiplying two matrices with different ranks. Here is an tutorial:

Fix tf.matmul() ValueError: Shape must be rank 2 but is rank 3 for ‘MatMul’ – TensorFlow Tutorial

tf.contrib.keras.backend.dot()

tf.contrib.keras.backend.dot() is a good choice to multiply matrix with different rank. Here is an example:

import tensorflow as tf
import numpy as np
t3 = tf.Variable(np.array([[200, 4, 5], [20, 5, 70],[2, 3, 5], [5, 5, 7]]), dtype = tf.float32)
w = tf.Variable(tf.random_uniform([3,3], -0.01, 0.01))
t3 = tf.reshape(t3, [2,2,3])
wx =  tf.contrib.keras.backend.dot(t3, w)

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run([wx]))

Here \(t3\) is 2 * 2 *3, rank is 3. \(w\) is 3* 3, rank is 2.

Run this code, you may get this result:

[array([[[ 1.6675205e+00,  1.4983379e+00, -2.1119225e-01],
        [-8.9924693e-02, -5.1424092e-01, -2.8229352e-02]],

       [[ 2.1686245e-02, -1.6539715e-02, -1.1580679e-02],
        [ 5.5628203e-02, -1.5962720e-03, -2.1116760e-02]]], dtype=float32)]

We run this code in tensorflow 1.10. However, if you replace tf.contrib.keras.backend.dot() with tf.matmul(), you will get a ValueError.

Leave a Reply

Your email address will not be published. Required fields are marked *