Creating Ones Tensor in PyTorch with torch.ones and torch.ones_like

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

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
Out[1]:
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
Instead of list, we can also pass the size in a tuple as shown below.
In [2]:
ones_tensor = torch.ones(size = (3,5))

ones_tensor
Out[2]:
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.

In [3]:
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
Out[4]:
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]]])

PyTorch Ones Tensors with torch.ones_like()

In PyTorch torch.ones_like() function is used to create ones tensor whose size is same as another reference tensor. This function eliminates the two-step process of getting the size of the other tensor and then creating ones tensor of this size.

Example – 1 : Creating 2 Dimensional Ones Tensor with torch.ones_like()

Let us first create a tensor with random values. We will use the size of this tensor to create the ones tensor. We pass the name of this tensor to torch.ones_like function.
In [5]:
random_tensor = torch.rand(size=(4,5))

random_tensor
Out[5]:
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]])
In [6]:
ones_like_tensor = torch.ones_like(random_tensor)

ones_like_tensor
Out [6]:
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
Out[7]:
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]]])
In [8]:
ones_like_tensor = torch.ones_like(random_tensor)

ones_like_tensor
Out[8]:
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.]]])

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *