tf.constant_initializer - Use TensorFlow constant initializer operation to initialize a constant in TensorFlow

tf.constant_initializer - Use TensorFlow constant initializer operation to initialize a constant in TensorFlow

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.

In this video, we’re going to use the tf.constant_initializer operation to do a simple TensorFlow variable creation such that the initialized values of the variable get the value that you pass into the initializer operation.

All right, let’s get started.

First, let’s create a Python variable to hold our constant_initializer operation.

```
bias_initializer = tf.constant_initializer(value=0.1)
```

So we use tf.constant_initializer.

We want to assign the value to be 0.1 and we assign it to the Python variable bias_initializer.

Next, let’s print out the bias_initializer Python variable to see what we have.

```
print(bias_initializer)
```

We see that it is a TensorFlow ops which means that it represents a computation rather than a value.

Further, because we haven’t run it in a TensorFlow session, it doesn’t do anything at this time.

Next, let’s define our TensorFlow variable using the get variable operation and make the initializer the variable bias_initializer that we just created.

```
bias_one = tf.get_variable(name="bias_one_tf_var",
shape=[10],
initializer=bias_initializer)
```

So we give it the name bias_one_tf_var.

The shape that we want is 10.

The initializer uses the bias_initializer we just created.

And we set it equal to the Python variable bias_one.

Let’s print the bias_one variable to see what we have.

```
print(bias_one)
```

We see that it is a TensorFlow tensor.

We see that it has the name that we assigned to it.

We see that it has the shape 10 which is what we wanted.

And we see that the data type is float32 which is what we would expect given the value we had for the constant_initializer was a float.

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

First, we launch the graph in a session.

```
sess = tf.Session()
```

Then we initialize all the global variables in the graph.

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

Let’s first print our TensorFlow initializer operation in a TensorFlow session to see what’s inside.

```
print(sess.run(bias_initializer))
```

Yikes!

We get an error, an error that says, “Object has invalid type.”

And it says, “Class ‘tensorflow.python.ops.init_ops.Constant’ must be a string or Tensor.

Cannot convert a Constant into a Tensor or Operation.”

What this basically means is that printing a TensorFlow operation doesn’t do anything.

This is because a TensorFlow operation is a graph node that does computations on tensors, so trying to print it breaks the functionality.

Let’s instead print our TensorFlow variable we assigned to the bias_one Python variable.

```
print(sess.run(bias_one))
```

Perfect - the bias_one TensorFlow variable received the values of 0.1 for all 10 entries.

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

```
sess.close()
```

That is how you use the tf.constant_initializer operation to do a simple TensorFlow variable creation such that the initialized values of the variable get the value that you pass into the initializer operation.

Receive the Data Science Weekly Newsletter every Thursday

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