tf.zeros - How to use tf zeros operation to create a TensorFlow zeros Tensor

tf.zeros - How to use tf zeros operation to create a TensorFlow zeros Tensor

We import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print the TensorFlow version we are using.

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

We are using TensorFlow 1.0.1.

In this video, we’re going to create a TensorFlow constant tensor full of zeros so that each element is a zero using the tf.zeros operation.

All right, let’s get started.

For the first example, we’ll create a TensorFlow tensor full of zeros that are full of integers.

```
tf_int_zeros_ex = tf.zeros(shape=[1,2,3], dtype="int32")
```

We use tf.zeros.

We pass in the shape and we pass in a Python list that says 1, 2, 3, and the data type that we want is int32.

We assign it to the Python variable tf_int_zeros_ex.

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

```
print(tf_int_zeros_ex)
```

We see that it’s a TensorFlow tensor, it has the name, the shape is 1x2x3 which is what we defined it as, and the data type is int32.

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

For the second example, we’ll create a TensorFlow tensor full of zeros that are now float numbers.

```
tf_float_zeros_ex = tf.zeros(shape=[2,3,4], dtype="float32")
```

So we use again tf.zeros, the shape is going to be 2x3x4, and the data type is float32.

We assign it to the Python variable tf_float_zeros_ex.

We print out the tf_float_zeros_ex Python variable to see what we have:

```
print(tf_float_zeros_ex)
```

And we see again that it is a TensorFlow tensor, the shape is 2x3x4, and the data type is float32.

Now that we’ve created the TensorFlow tensors, let’s run the computational graph.

First, we launch the graph in a TensorFlow session.

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

Then we initialize all the global variables in the graph.

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

Let’s now print our first TensorFlow tensor full of zeros that are int32s.

```
print(sess.run(tf_int_zeros_ex))
```

So we do a print and then we evaluate the variable in a TensorFlow session run, and we see that it is a 1x2x3 tensor.

All the values are zeros for every element place and there is no decimal point which designates to us visually that these are integers.

Next, let’s print our other TensorFlow tensor which is full of zeros that are float32 numbers.

```
print(sess.run(tf_float_zeros_ex))
```

We print it inside a TensorFlow session run and we see a 2x3x4 tensor.

We see that every single element is a zero, and because it is a float32 data type, we see that each element has a decimal point.

So zero and decimal points are all the elements.

Perfect - We were able to create a TensorFlow tensor full of zeros – in the first example, full of integer zeros, in the second example, full of float32 zeros.

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

```
sess.close()
```

That is how you create a TensorFlow constant tensor full of zeros so that each element is a zero using the tf.zeros operation.

Receive the Data Science Weekly Newsletter every Thursday

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