Using torch.rand() and torch.rand_like() to create Random Tensors in PyTorch

Introduction

This tutorial will show you how to create Tensors with random values in PyTorch by using torch.rand() and torch.rand_like() functions. In deep learning, creating tensors there initialized with random numbers is a very common operation. We will explain these functions with the help of examples for better understanding.

PyTorch Random Tensors with torch.rand()

torch.rand function is used to create a tensor with the random values from the uniform distribution that lies between the interval [0,1) i.e. greater than or equal to 0 and less than 1.

Let us understand this better with the examples, but before that let us import the PyTorch library.

In [0]:

import torch;

 

Example – 1: Creating 2 Dimensional Random Tensor with torch.rand()

In the first example, we are generating a tensor of size 3×3 with random values with torch.rand function. There are three different syntaxes to do this –

Method – 1:

In this example, we just pass the size as 3,3 to torch.rand() as shown below.

In [1]:

tensor = torch.rand(3,3)

tensor

Out[1]:

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

Method – 2:

In this method, we can pass the size as a list [3,3] to torch.rand() as shown below.

In [2]:

tensor = torch.rand(size = [3,3])

tensor

Out[2]:

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

Method – 3:

In this method, we can pass the size as a tuple (3,3) to torch.rand() as shown below.

In [3]:

tensor = torch.rand(size = (3,3))

tensor

Out[3]:

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

In this example, we are creating a 1-D random tensor of 10 elements.

In [4]:

tensor = torch.rand(10);

tensor

Out[4]:

tensor([0.8266, 0.7850, 0.4197, 0.9229, 0.7172, 0.8324, 0.3692, 0.4916, 0.7819,
        0.7687])

Example – 3: Creating 3 Dimensional Random Tensor

In the below example, we are creating 3-D PyTorch tensor of size 2x3x3.

In [5]:

tensor = torch.rand(size=(2,3,3))

tensor

Out[5]:

tensor([[[0.6133, 0.9458, 0.9013],
         [0.4031, 0.5596, 0.0875],
         [0.5414, 0.9022, 0.8020]],

        [[0.2309, 0.2696, 0.6551],
         [0.7222, 0.2852, 0.8607],
         [0.8311, 0.4471, 0.2992]]])

Example – 4: Using dtype parameter with torch.rand

In torch.rand function dtype parameter is used to specify the data type as shown below.
In [6]:
tensor = torch.rand(size=(3,3), dtype=torch.float32)

tensor
Out[6]:
tensor([[0.3449, 0.0083, 0.6991],
        [0.4702, 0.8682, 0.2594],
        [0.1540, 0.4070, 0.5919]])

Issue with Int data type with torch.rand() :

There is a catch over here if you try to specify int data type with dtype parameter. Let us see the below example, the output errors out when we are passing int with dtype. This is because torch.rand only generates data between the interval [0,1), i.e. greater than equal to 0 and less than 1.  Since it is always below 1, it can never generate an integer hence it throws the error.
In [7]:
tensor = torch.rand(size=(3,3), dtype=torch.int32);

tensor
Out[7]:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-28-9554a5ee2190> in <module>()
----> 1 tensor = torch.rand(size=(3,3), dtype=torch.int32);
2 tensor

RuntimeError: "check_uniform_bounds" not implemented for 'Int'

Example – 5: Using Seed Value with torch.rand

Whenever you use torch.rand() function it will generate different values each time randomly. However, if you want to make sure that every time you call this function it generates the same random values then we have to pass the seed value as explained below.

We first create a generator object gen and initialize it with a seed value with manual_seed(). And then we use this generator object inside torch.rand as shown below. Now, whenever we use this block of code it will generate the tensor with the same values.

In [8]:

gen = torch.Generator()
gen.manual_seed(2947587447)
tensor = torch.rand(size=(4,3), generator = gen)
tensor

Out[8]:

tensor([[0.0371, 0.6312, 0.3432],
        [0.3925, 0.3106, 0.4346],
        [0.1148, 0.1085, 0.2297],
        [0.7899, 0.3889, 0.0603]])

PyTorch Random Tensors with torch.rand_like()

Example – 1: Creating 2 Dimensional Random Tensor with torch.rand_like()

First, we shall create a tensor with zero values that will be used for creating the random tensor of the same size. Next, we pass the name of this tensor to torch.rand_like function.

In [9]:

reference_tensor = torch.zeros(size=(3,4))

reference_tensor

Out[9]:

tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
In [10]:
tensor = torch.rand_like(reference_tensor)

tensor

Out[10]:

tensor([[[0.8383, 0.9491, 0.6800, 0.6901],
         [0.4085, 0.5711, 0.7195, 0.0400],
         [0.7486, 0.7667, 0.2196, 0.8198]],

        [[0.7664, 0.1315, 0.7272, 0.4294],
         [0.7604, 0.1315, 0.7219, 0.7277],
         [0.5066, 0.0683, 0.4788, 0.6700]]])

Example – 2: Creating 3 Dimensional Random Tensor with torch.rand_like()

Here we create a 3-D zero tensor and then use it as a reference to create random tensor.

In [11]:

reference_tensor = torch.zeros(size=(2,3,4))

reference_tensor
Out[11]:
tensor([[[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]],

        [[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]])
In [12]:
tensor = torch.rand_like(reference_tensor)

tensor

Out[12]:

tensor([[[0.1704, 0.4320, 0.6740, 0.4405],
         [0.5392, 0.0576, 0.6135, 0.2433],
         [0.2174, 0.9259, 0.7687, 0.5202]],

        [[0.8903, 0.2329, 0.1755, 0.6491],
         [0.5983, 0.3904, 0.4308, 0.4082],
         [0.6989, 0.9511, 0.8717, 0.9099]]])
  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

Follow Us

Leave a Reply

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