Pytorch also allows us to generate tensors with random values. In this chapter of Pytorch Tutorial, you will learn how to generate random tensors and how to access and change the seed of the random generator. Seeding is useful to ensure repeatability of the code.
torch.rand()
torch.rand()
will return a tensors of the required shape with random values between 0
(inclusive) and 1
(exclusive). The values of the tensor are distributed uniformly over this interval. The dimensions or the shape of the required tensor are passed to the function as argument.
Syntax:
torch.rand(*size)
Example
# This will create a tensor with the shape 2, 3 with random values
tensor_1 = torch.rand(2, 3)
print(tensor_1)
# Outputs- tensor([[0.8823, 0.9150, 0.3829], [0.9593, 0.3904, 0.6009]])
# We can also pass a tuple specifying the desired shape of tensor.
tensor_2 = torch.rand((2, 3))
print(tensor_2)
# Outputs- tensor([[0.2566, 0.7936, 0.9408], [0.1332, 0.9346, 0.5936]])
Note– While the shape of the required tensor is passed as a tuple to the rand()
function in the above example. The shape can also be passed as a list.
torch.randint()
torch.randint()
function returns a tensor with random integer values within a given range. You need to provide a low value, a high value the shape of the required as parameter. The values within the tensor are uniformly distributed between the low(including) and high(exclusive).
Syntax:
torch.randint(low, high, size)
Example
In the below example, you will be generating a tensor of shape (2, 5)
with values between -10
(inclusive) and 10
(exclusive).
tensor_1 = torch.randint(-10, 10, (4, 5))
print(tensor_1)
# Outputs- tensor([[0, 4, 1, 2, 5], [5, -3, -4, -1, 6]])
Note– If you choose to skip the low parameter, it will default to 0.
Example
In the below example, the value of low parameter is not passed. Hence the value of low will be automatically set to 0. The tensor so generated will have the shape (2, 3)
and contain values between 0
(inclusive) and 10
(exclusive).
tensor_1 = torch.randint(10, (2, 3))
print(tensor_1)
# Outputs- tensor([[0, 9, 6], [9, 5, 4]])
Random Seed
Pytorch generates pseudo-random numbers. What it means it that, random numbers produced by pytorch can be predicted and re-generated if we know the the number(also called ‘seed’) used to ‘seed’ the random generator. This is great for reproducing same results as generated before as it removes the uncertainty caused by generating completely random numbers each time.
The generator is seeded with a random number at the beginning. This seed can be accessed by calling the initial_seed()
function in the pytorch library.
Example
torch.initial_seed()
# Outputs- 16791183524288837558
We can get the random tensor we generated in the earlier example by seeding the (pseudo-)random generator again with this number and calling the rand function again. We can seed the generator manually by calling the manual_seed()
function in the pytorch library. The value of ‘seed’ is passed as a parameter to this function.
Example
# sets the seed manually to 42
torch.manual_seed(42)
torch.rand((2, 3))
# Outputs- tensor([[0.8823, 0.9150, 0.3829], [0.9593, 0.3904, 0.6009]])
Note– This tensor is the same as the random tensor that we generated before in the above example.
Important– You can generate the same random numbers as generated before if you know the seed.
You can also seed the generator with a new random number. This can be done by calling the seed()
function in the pytorch library.
Example
# This will seed for the random generator with a new seed.
torch.seed()
# Outputs- 15377867763402226090
# The new seed is 15377867763402226090
torch.initial_seed()
# Outputs- 15377867763402226090
# Checking if the value of seed has changed
Note– We are checking if the value of the seed has changed by calling the initial_seed()
function.