tf.zeros: Create A TensorFlow Tensor Full of Zeros
tf.zeros - Create a TensorFlow Constant Tensor full of zeros so that each element is a zero using the TensorFlow Zeros operation
< > 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 the TensorFlow version we are using.
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.
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:
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.
Let’s now print our first TensorFlow tensor full of zeros that are int32s.
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.
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.
That is how you create a TensorFlow constant tensor full of zeros so that each element is a zero using the tf.zeros operation.