tf.constant - Create Tensorflow constant tensor with scalar value using tf constant operation.

tf.constant - Create Tensorflow constant tensor with scalar value using tf constant operation.

First, we import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print out 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 populated with a scalar value by using the TensorFlow constant operation as well as defining the shape and the data type.

All right, let's get started.

For the first example, we'll create a TensorFlow constant tensor with dimensions 2, 3, 4, which is populated with a scalar value 10.0, and a data type of float32.

```
constant_float_tensor = tf.constant(10.0, shape=[2, 3, 4], dtype="float32")
```

Note that we used tf.constant and we assigned it to the Python variable constant_float_tensor.

Something to note is that if we don't specify the shape, then tf.constant will use the dimensions of the value that we pass in to create the constant.

And because we're passing in a scalar which has dimensions of 0, the constant wouldn't be a tensor.

So we need to pass in the shape that we want which is 2x3x4.

We give it a data type.

So when it produces this constant, it will be a tensor that's 2x3x4 filled with 10.0 for every entry.

Let's now print to see what we have.

```
print(constant_float_tensor)
```

We see that it's a TensorFlow tensor, its name is Const, the shape is 2x3x4, and the data type is float32.

Because we haven't run and evaluated the tensor in a TensorFlow session, we don't see any values.

For the second example, we'll create a TensorFlow constant tensor with dimensions of 1x2x3 with a data type of int32.

```
constant_int_tensor = tf.constant(-255, shape=[1, 2, 3], dtype="int32")
```

The value we want to fill this tensor with is -255, and we're using the tf.constant operation, and we're going to assign it to the Python variable constant_int_tensor.

And we print it to see what we have.

```
print(constant_int_tensor)
```

We see that it is a TensorFlow tensor.

The name has changed to Const_1.

That's because this one had the name of Const.

The shape is 1x2x3, and the data type is int32.

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 and tensors in the graph.

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

Let's now print our two constant tensors we created to see what's inside.

We first print constant_float_tensor in a sess.run

```
print(sess.run(constant_float_tensor))
```

And we see that it is a 2x3x4 tensor filled with 10.0, 10.0, 10.0 for every single element.

We next print constant_int_tensor

```
print(sess.run(constant_int_tensor))
```

And we see that it is a shape 1x2x3, and it is filled with -255, and it has no decimal points which shows us visually that it is not a float32 data type.

It is an int32 data type.

Perfect.

It worked.

We were able to create our two TensorFlow constants with the dimensions that we wanted, and the data types that we wanted.

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 populated with a scalar value by using the tf.constant operation as well as defining the shape and data type that you want.

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.