Initialize TensorFlow Variables With Matrix

Initialize TensorFlow variables with matrix of your choice. Example with identity matrix.

Initialize TensorFlow variables with matrix of your choice. Example with identity matrix.

Video Transcript


We import TensorFlow as tf.

import tensorflow as tf


Then we print out what version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.0.1.


In this video, we’re going to initialize a TensorFlow variable as the identity matrix of the size of our choosing using the TensorFlow variable functionality and the TensorFlow eye functionality.


All right, let’s get started.


First, remember that you can use the TensorFlow eye functionality to easily create a square identity matrix.

identity_matrix_ex = tf.eye(5, dtype="float32")

We create a 5x5 identity matrix with a data type of float32 and assign it to the Python variable identity matrix.

So we used tf.eye, give it a size of 5, and the data type is float32.

Note that the default data type for tf.eye is float32 but I like to specify it to make the code easier to read.


Now, let’s print out the identity_matrix_ex Python variable to see what we have.

print(identity_matrix_ex)

We see that it’s a TensorFlow variable, eye/MatrixDiag, the shape is 5x5 which is what we expect given we gave it a size of 5, and the data type is float32 which is what we specified.


Next, let’s initialize a TensorFlow variable using the identity matrix we just created.

identity_tensorflow_variable_ex = tf.get_variable("identity_tf_variable",
                                                   initializer=identity_matrix_ex)

We use the tf.get_variable functionality, we give it a name of identity_tf_variable, and for the initializer, we use the identity_matrix_ex tensor that we just defined, and we assign that to the identity_tensorflow_variable_ex Python variable.


Let’s print out this identity_tensorflow_variable_ex Python variable to see what we have.

print(identity_tensorflow_variable_ex)

We see that it is a TensorFlow tensor, the name is identity_tf_variable, the shape is 5x5, and the data type is float32.


Now that we’ve set up our TensorFlow tensors and variables, it’s time to run the computational graph.\

sess = tf.Session()

We launch the graph in a session.


Then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


Let’s print the identity_matrix_ex tensor in a TensorFlow session to see the values of the identity matrix.

print(sess.run(identity_matrix_ex))

We see that it is a 5x5 identity matrix where it has 1s down the diagonal, all the other entries are 0, and there is a decimal point after each number to show that it is indeed a float32 number.


Next, let’s print the Python variable identity_tensorflow_variable_ex’s initialized_value in a TensorFlow session to see the value.

print(sess.run(identity_tensorflow_variable_ex.initialized_value()))

Remember, this is what we are trying to do – create a TensorFlow variable that had the initialized value of the identity matrix.

When we evaluate this expression, we see that the TensorFlow variable has the initialized values of the identity matrix and the data type is float32.


Finally, we close the TensorFlow session to release the TensorFlow resources we used within the session.

sess.close()


That is how you initialize a TensorFlow variable as the identity matrix of the shape of your choosing using the TensorFlow variable functionality and the TensorFlow eye functionality.

Receive the Data Science Weekly Newsletter every Thursday

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