Contents

## Introduction

In this article, we will see how to create ones tensor in PyTorch by using torch.ones() and torch.ones_like() functions. Ones tensors are tensors whose all values are one as shown in the below illustration.

This illustration shows 2-D ones tensors of size 4Ã—4 and 4Ã—3 respectively.

**PyTorch Ones Tensors with torch.ones()**

It is very easy to create Tensors with all ones in PyTorch by using torch.ones function. Let us understand this function in more detail with help of a few examples. Before starting, let us import the PyTorch library as shown below â€“

In [0]:

importÂ torch;

Â

**Example â€“ 1 : Creating 2 Dimensional Ones Tensor with torch.ones()**

In the first example, we will generate ones tensor of size 3Ã—5. For this, we pass this size as a list in torch.ones function as shown below.

In [1]:

ones_tensorÂ =Â torch.ones(sizeÂ =Â [3,5]) ones_tensor

tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])

ones_tensorÂ =Â torch.ones(sizeÂ =Â (3,5)) ones_tensor

tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])

**Example â€“ 2 : Creating Ones Tensor with Specific Data Type**

The default data type of torch ones function is float. This is visible in the above examples that have ones with decimals. We can however use the dtype parameter to specify the data type explicitly.

In the following example, we are using type value as int that produces all one with int type, i.e without decimals.

ones_tensorÂ =Â torch.ones(sizeÂ =Â (3,5),Â dtypeÂ =int) ones_tensor

Out[3]:

tensor([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])

**Example â€“ 3 :Â ****Creating 3 Dimensional Ones Tensor with torch.ones()**

In this example, we are generating 3-D ones tensor in PyTorch as shown below.

In [4]:

ones_tensorÂ =Â torch.ones(sizeÂ =Â (2,3,5),Â dtypeÂ =int) ones_tensor

tensor([[[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]])

random_tensorÂ =Â torch.rand(size=(4,5)) random_tensor

tensor([[0.5019, 0.3582, 0.6476, 0.0707, 0.1487], [0.9490, 0.7027, 0.8373, 0.0666, 0.0776], [0.9029, 0.8233, 0.6871, 0.0676, 0.0342], [0.0643, 0.7627, 0.4676, 0.9771, 0.6908]])

ones_like_tensorÂ =Â torch.ones_like(random_tensor) ones_like_tensor

tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])

**Example â€“ 2 : Creating 3 Dimensional Ones Tensor with torch.ones_like()**

Let us again generate a 3-D random valued tensor of size 2x4x5 and then use it inÂ torch.ones_like function.

In [7]:

random_tensorÂ =Â torch.rand(size=(2,4,5)) random_tensor

tensor([[[0.5540, 0.2965, 0.7656, 0.0864, 0.4719], [0.1797, 0.8056, 0.5731, 0.3665, 0.7524], [0.7091, 0.5366, 0.6332, 0.8776, 0.0760], [0.1010, 0.5353, 0.9416, 0.2458, 0.8791]], [[0.8621, 0.0030, 0.5289, 0.6757, 0.2340], [0.5413, 0.6108, 0.5478, 0.5954, 0.1434], [0.6445, 0.6780, 0.4488, 0.3309, 0.0727], [0.2652, 0.6528, 0.6589, 0.6072, 0.5976]]])

ones_like_tensorÂ =Â torch.ones_like(random_tensor) ones_like_tensor

tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]])