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

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

We import TensorFlow as tf.

```
import tensorflow as tf
```

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

```
print(tf.__version__)
```

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.

```
print(np.__version__)
```

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.

```
print(numpy_mda)
```

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

Great.

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",
initializer=numpy_mda)
```

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.

```
print(tf_var_from_np)
```

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.

```
print(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.

```
sess.run(tf.global_variables_initializer())
```

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

```
print(numpy_mda)
```

Okay.

So that was our numpy_mda variable.

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

```
print(sess.run(tf_var_from_np))
```

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.

```
sess.close()
```

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.

Receive the Data Science Weekly Newsletter every Thursday

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form.

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