## tf.ones: Create A TensorFlow Tensor Full of Ones

tf.ones - Create a TensorFlow Constant Tensor full of ones so that each element is a one using the TensorFlow Ones 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

### Transcript:

We import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print the TensorFlow version that 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 ones so that each element is a one using the tf.ones operation.

All right, let’s get started.

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

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

So we use the tf.ones operation.

We pass in the shape which is a Python list that designates that we want it to be 1x2x3, the data type we want is int32, and we assign it to the Python variable tf_int_ones_ex.

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

```
print(tf_int_ones_ex)
```

We see that it’s a TensorFlow tensor.

TensorFlow automatically gives it a name.

The shape is 1x2x3, 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 ones that are floating numbers.

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

Again, we use the tf.ones operation, we say the shape is going to be 2x3x4, and the data type is float32.

We assign that to the Python variable tf_float_ones_ex.

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

```
print(tf_float_ones_ex)
```

We see that it’s a TensorFlow tensor, it has a name given to it by TensorFlow, the shape is 2x3x4, and the data type is float32.

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

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 now print our first TensorFlow tensor full of ones that are int32s.

```
print(sess.run(tf_int_ones_ex))
```

So we see that it is a 1x2x3 tensor, we see that all the elements of the tensor are ones, and we do not see any decimal points—which tells us visually that these are integers rather than floating point numbers.

Next, let’s print our tf_float_ones_ex Python variable to see what we have.

```
print(sess.run(tf_float_ones_ex))
```

We see that it is a 2x3x4 tensor, all the elements of the tensor are ones, and all the elements are one with a decimal point which visually designates to us that these are all floating point numbers as opposed to the integers up here that did not have any decimal points.

Perfect - we are able to create TensorFlow tensors full of ones.

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

```
sess.close()
```

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