 ## Create A PyTorch Identity Matrix

Create a PyTorch identity matrix by using the PyTorch eye operation

Type: FREE     Duration: 1:03   Technologies: PyTorch, Python

Page Sections: Video  |  Code  |  Transcript

### < > 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.

### Transcript:

This video will show you how to create a PyTorch identity matrix by using the PyTorch eye operation.

First, we import PyTorch.

``````import torch
``````

Then we print the PyTorch version we are using.

``````print(torch.__version__)
``````

We are using PyTorch 0.4.0.

Let’s now create a PyTorch identity matrix of size 3x3.

``````pt_3_by_3_eye_ex = torch.eye(3)
``````

So we say torch.eye and we pass a number to it.

This n, which is 3 in our case, will be the number of rows and the number of columns that we have.

We assign this tensor that’s created to the Python variable pt_3_by_3_eye_ex.

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

``````print(pt_3_by_3_eye_ex)
``````

We see that it’s a two-dimensional PyTorch floating tensor, so it has rows and columns.

We can see that the size is one, two, three rows by one, two, three columns.

We see that the diagonal elements are all the number one.

We also see that the rest of the elements are the number zero.

So you can see that this is an identity matrix.

Perfect - We were able to create a PyTorch identity matrix by using the PyTorch eye operation.

Back to PyTorch Tutorial Lesson List