# Difference Between tf.multiply() and tf.matmul() in TensorFlow – TensorFlow Tutorial

By | June 11, 2019

tf.multiply() and tf.matmul() are common used functions in tensorflow, what is the difference between them?

If matrix A is m*p and B is m*p.

c = tf.multiply(A,B) , c is also m * p

tf.matmul(): compute the matrix product of two tensors.

If matrix A is m*p and B is p * n

c = tf.matmul(A,B) , c is m * n

Here is an example to illustrate the difference between them.

import tensorflow as tf;
import numpy as np

matrix_a = tf.constant([[1,2,3],[4,5,6],[7,8,9]], dtype=tf.float32)
matrix_b = tf.constant([[2,3,4],[4,5,6],[7,8,9]], dtype=tf.float32)

matrix_c = tf.multiply(matrix_a,matrix_b)
matrix_d = tf.matmul(matrix_a,matrix_b)
init = tf.global_variables_initializer()
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
sess.run([init, init_local])
np.set_printoptions(precision=4, suppress=True)

c, d= (sess.run([matrix_c, matrix_d]))

print 'tf.multiply'
print c
print 'tf.matmul'
print d

The output is: From the output, you will find tf.muttiply() and tf.matmul() is not the same on the same tensor a and b.