## Create A PyTorch Tensor Full Of Ones

Create a PyTorch Tensor full of ones so that each element is a ones using the PyTorch 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:

This video will show how to create a PyTorch tensor where each element is a one by using the PyTorch ones operation.

First, we import PyTorch.

```
import torch
```

Then we print the PyTorch version we are using.

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

We can see we are using PyTorch 0.3.1.post2.

Now, let’s create the PyTorch tensor full of ones using the ones operation.

```
pt_ones_tensor_ex_one = torch.ones(4, 3, 2)
```

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

```
print(pt_ones_tensor_ex_one)
```

We see that we have four matrices where each matrix has three rows and two columns.

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

We also see that the PyTorch ones operation creates a floating tensor by default.

If we want to have a PyTorch tensor full of ones that are integers, we could cast this floating tensor to be an integer tensor.

```
pt_ones_tensor_ex_two = pt_ones_tensor_ex_one.int()
```

We use the PyTorch int operation.

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

```
print(pt_ones_tensor_ex_two)
```

We see that we have four matrices where each matrix has three rows and two columns.

Again, we see that all of the elements are the number one.

We also see that we now have a PyTorch IntTensor.

Brilliant!

We were able to create a PyTorch tensor where every element is a floating one or an integer one using the PyTorch ones operation.