tf.variable: TensorFlow Variable Initialize With NumPy Values

tf.variable - TensorFlow variable initialize with NumPy Values by using tf's get_variable operation

Type: FREE   By: Sebastian Gutierrez, Instructor Sebastian Gutierrez   Duration: 3:15   Technologies: TensorFlow, Python

Page Sections: Video  |  Code  |  Transcript

< > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

    or   Log In


We import TensorFlow as tf.

import tensorflow as tf

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


We are using TensorFlow 1.0.1.

We import NumPy as np.

import numpy as np

Then we print out the version of NumPy we are using.


We are using NumPy 1.13.3.

In this video, we’re going to initialize a TensorFlow variable with NumPy values by using TensorFlow’s get_variable operation and setting the variable initializer to the NumPy values.

All right, let’s get started.

First, we create a NumPy multidimensional array using NumPy’s random operation.

numpy_mda = np.random.rand(2,3,4)

We’re going to have it have the dimensions of 2x3x4, and we assign it to the Python variable numpy_mda.

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


We see that they are floating numbers and we see that the dimensions are 2x3x4.


What we want to do now is to use these NumPy values to initialize a TensorFlow variable with them so that the variable has the same values.

To do this, we’re going to create a new TensorFlow variable using TensorFlow’s get variable operation.

tf_var_from_np = tf.get_variable("tf_var_initialized_from_np",

We’re going to give it the name tf_var_initialized_from_np.

And for the initializer, we’re going to pass our Python variable numpy_mda which holds the NumPy values.

Then we’re going to set it to the Python variable tf_var_from_np.

Let’s print what we have.


We see that it’s a tensor.

We see that it has the name that we gave it.

We see that the shape is 2x3x4.

So because it took in the values from NumPy, it was able to figure out what the shape was, and it also figured out that these numbers were float64.

So it figured out the data type.

Just to make sure, let’s double check that the data type of the NumPy multidimensional array was float64 as well.

So we print the numpy_mda.dtype.


We see that it is float64.

So the tf.get_variable operation was able to figure out not only the shape but also the data type by passing the NumPy values to the initializer.

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

We launch the graph in a session.

sess = tf.Session()

We then initialize all the global variables in the graph.

Let’s first print again our numpy_mda Python variable so that it’s easy to visually compare the numbers.



So that was our numpy_mda variable.

Now, let’s print our TensorFlow variable in the TensorFlow session to see what’s inside.


And you can see that the numbers have been generated, you can see that it’s the same dimensions, and you can see that it’s the same values.

Let’s look at the first one to spot check visually that’s the same thing – 0.13012673, 0.13012673.

We’ll check the last one – 0.01004758, 0.01004758.

Perfect - we were able to initialize our TensorFlow variable with the NumPy values.

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


That is how you initialize a TensorFlow variable with NumPy values by using TensorFlow’s get variable operation and setting the variable initializer to the NumPy values.

Back to TensorFlow Tutorial Lesson List