## TensorFlow Identity Matrix Creation

TensorFlow Identity Matrix Creation with TensorFlow eye (tf.eye)

### < > 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 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 an identity matrix using the TensorFlow eye functionality.

All right, let’s get started.

The first of our two example TensorFlow identity matrices will be of size 2.

```
ident_matrix_ex_1 = tf.eye(2)
```

We create it using the TensorFlow eye functionality and assign it to the Python variable ident_matrix_ex_1.

So we see tf.eye(2).

That’s the size: 2.

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

```
print(ident_matrix_ex_1)
```

We see that it is a TensorFlow tensor, it’s an eye/MatrixDiag, the shape is 2x2, and the data type is float32.

Notice that TensorFlow assumes that we’re creating a square matrix which has the same number of rows as columns, and so by giving it a size of 2 when we define it, it created a 2x2 matrix.

The second example will also be a TensorFlow identity matrix of size 2, only this time, rather than having float32 numbers, it’ll be composed of integers.

```
ident_matrix_ex_2 = tf.eye(2, dtype="int32")
```

So we create the second example using the TensorFlow eye functionality and assign it to the Python variable ident_matrix_ex_2.

You can see that we pass the size 2 and this time, our data type or dtype is going to be int32.

Now that we have that, let’s print out the ident_matrix_ex_2 Python variable to see what we have.

```
print(ident_matrix_ex_2)
```

We see the same thing.

It’s TensorFlow tensor eye_1/MatrixDiag.

Shape is 2x2.

This time, the data type is int32, whereas before it was float32.

That’s because when we defined it, we told it that we wanted the data type to be int32.

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

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

So we launch the graph in a session.

Then we initialize all the global variables in the graph. We have two variables, so those will be the ones that are initialized.

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

Let’s now print our first TensorFlow identity matrix which had the data type of float32.

```
print(sess.run(ident_matrix_ex_1))
```

We see a 2x2 matrix where the diagonal entries have the number 1, and the rest of the entries are 0.

Note that all those numbers have a decimal point to show they are indeed float32 numbers.

Next, let’s print our second TensorFlow identity matrix which had the data type of int32.

```
print(sess.run(ident_matrix_ex_2))
```

We see a 2x2 matrix where the diagonal entries have number 1 and the rest of the entries are 0, only this time, there are no decimal places which means they are indeed int32 numbers.

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

```
sess.close()
```

That is how you create an identity matrix using the TensorFlow eye functionality.