Computing Hadamard Product of Two Tensors in TensorFlow – TensorFlow Example

By | June 11, 2019

In tensorflow, to compute Hadamard Product is very simple, you can use * operator or tf.multiply() function to do.

In this tutorial, we will tell me some details on computing Hadamard Product in TensorFlow.

Example 1: Compute hadamard product on the smae shape of two tensors

import tensorflow as tf
import numpy as np

# the shape of matrix a and b are the same
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 = matrix_a * matrix_b
matrix_d = tf.multiply(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 'compute hadamard product using *'
    print c
    print 'compute hadamard product using tf.multiply()'
    print d

The output is:

From the output in terminal, you will find * operator or tf.multiply() is the same.

However, if the shape of two matrix a and b are not the same.

Example 2: Compute hadamard product on the different shape of two tensors

import tensorflow as tf
import numpy as np

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

matrix_e = matrix_a = tf.constant([[[1,2,3],[4,5,6],[7,8,9]], [[1,2,3],[4,5,6],[7,8,9]]], dtype=tf.float32)
matrix_f = matrix_e * 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)
   
    f= (sess.run([matrix_f]))
    
    print 'compute hadamard product on shape [2,3,3] and [3,3]'
    print f

The output is:

From the output, you will find tensorflow will compute hadamard product of two tensors on the last axis, so you must be sure the last two dimensions of two tensors are same.

Leave a Reply