tf.matmul: Multiply Two Matricies Using TensorFlow MatMul

tf.matmul - Multiply two matricies by using TensorFlow's matmul operation

tf.matmul - Multiply two matricies by using TensorFlow's matmul operation

Video Transcript


We start by importing TensorFlow as tf.

import tensorflow as tf


Then we print out the version of TensorFlow that we are using.

print(tf.__version__)

We are using TensorFlow 1.5.0.


In this video, we’re going to multiply two matrices by using tf.matmul operation.


The first matrix will be a TensorFlow tensor shaped 3x3 with min values of 1, max values of 10, and the data type will be int32.

random_int_var = tf.get_variable("random_int_var_1_to_10",
                                     initializer=tf.random_uniform([3, 3],
                                                 minval=1,
                                                 maxval=10,
                                                 dtype=tf.int32))

We use tf.get_variable and we give it the name random_int_var_1_to_10.

We assign it to the Python variable random_int_var.


Let’s print out the random_int_var Python variable to see what we have.

print(random_int_var)

We see that it’s a TensorFlow variable.

We see the name that we gave it.

The shape is 3x3.

And the data type is int32.

Because we haven’t run it in a TensorFlow session yet, right now it doesn’t have any values.

The same thing will apply to the other matrix we’re about to create.


The second matrix we create will be a TensorFlow tensor shaped 3x3 with integers ones for every element with the data type of int32.

tf_int_ones = tf.ones(shape=[3,3], dtype="int32")

In this case, we’re using tf.ones operation and we’re assigning it to the Python variable tf_int_ones.

We use all ones in the second matrix to make the matrix multiplication easy to check visually.


Let’s print out the tf_int_ones Python variable to see what we have.

print(tf_int_ones)

We see that it’s a tensor.

We see the shape is 3x3.

And the data type is int32.


Now that we’ve created our TensorFlow matrix tensors, it’s time to run the computational graph.


So we launch the graph in a session:

sess = tf.Session()


And we initialize all the global variables in the graph, in this case, our two matrices:

sess.run(tf.global_variables_initializer())


Let’s now print our first TensorFlow matrix, random_int_var, to see what we have.

print(sess.run(random_int_var))

We see that it’s a 3x3 matrix, and we see the numbers are between 1 and 10, and they are all integers.

They don’t have any decimal points.


Next, let’s print our second matrix, tf_int_ones.

print(sess.run(tf_int_ones))

We print it inside of a session run, and we see that it is a 3x3 matrix comprised of ones.


Now that we have our two matrices, let’s do the matrix multiplication using tf.matmul operation.

tf_matrix_multiplication_prod = tf.matmul(random_int_var, tf_int_ones)

So we do tf.matmul.

We see random_int_var, tf_int_ones.

It’s important to remember your matrix multiplication rules so that your columns match your rows.

In this case, we’re multiplying a 3x3 matrix against a 3x3 matrix, so we don’t have to worry about that too much because we’re multiplying two square matrices, and we’re going to set the product equal to the Python variable, tf_matrix_multiplication_prod.


Let’s check our results.

print(sess.run(tf_matrix_multiplication_prod))

We see 14, 14, 14; 19, 19, 19; 23, 23, 23.


All right, so let’s do the visual inspection of results.


So this was our first matrix, and we are multiplying it times the second matrix.

So 4x1 + 3x1 + 7x1. So you can see now why we use the ones.

We can just do the addition of this.

So 4+3 = 7 + 7 = 14.

So we get 14.

First row, second column, we get 14.

First row, third column, we get 14.


Now, we’re going to do second row, first column.

So 8+4 = 12 + 7 = 19, and we see that it’s 19 and 19.


Then for the last row, it will be 7+ 8 = 15 + 8 = 23.

So we would be multiplying this third row by the first column, third row by the second column, third row by the third column.

So we see 23, 23, 23.


Perfect, It worked!


We were able to multiply two matrices together using tf.matmul operation.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.