Understand torch.tensor.random_() with Examples – PyTorch Tutorial

By | May 13, 2022

In this tutorial, we will use some examples to show you how to use torch.tensor.random_() correctly. You may find there are some notices you may need concern.

torch.tensor.random_()

It is defined as:

Tensor.random_(from=0, to=None, *, generator=None)

It will fill itself with numbers sampled from the discrete uniform distribution over [from, to – 1]. However, it also return a new tensor.

For example:

import torch
import torch.nn as nn

x = torch.empty([3,5], dtype = torch.float32)
print(x)
y = x.random_(-1, 3)

print("x = ")
print(x)
print("y=")
print(y)

In this code, we first create an empty tensor x. It is:

tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

Then we will fill it using x.random_(-1, 3). At this step ,x will be converted:

x = 
tensor([[ 2.,  2.,  0.,  2., -1.],
        [ 2.,  2., -1.,  0.,  2.],
        [ 1., -1.,  2., -1., -1.]])

However, y = x.random_(-1, 3), we also can find tensor.random_() will return a tensor y, it also be:

y=
tensor([[ 2.,  2.,  0.,  2., -1.],
        [ 2.,  2., -1.,  0.,  2.],
        [ 1., -1.,  2., -1., -1.]])

from and to parameter

We can find Tensor.random_() contains from and to parameter, can we set value for them? For example:

print(x)
y = x.random_(from = -1, to = 3)

However, run this code, we will get this error:

y = x.random_(from = -1, to = 3)
^
SyntaxError: invalid syntax

Look at this new example:

y = x.random_(-1, to = 3)

Here we only set a value to to parameter, run this code we will get:

x = 
tensor([[ 0.,  0., -1.,  2.,  0.],
        [ 1.,  2.,  0., -1.,  2.],
        [-1.,  0.,  0., -1.,  0.]])
y=
tensor([[ 0.,  0., -1.,  2.,  0.],
        [ 1.,  2.,  0., -1.,  2.],
        [-1.,  0.,  0., -1.,  0.]])

It means we can not assign a value to from parameter except for to parameter.

How about float number in torch.tensor.random_()?

From example above, we can find from and to parameter are integers. How about they are float number?

print(x)
y = x.random_(-1.0, 3.0)

Run this code, we will see:

torch.tensor.random_() float number error

We can find from and to parameter can not be float.

How about default parameter in torch.tensor.random_()?

If we do not set value for to parameter, x will be filled with numbers from [0, 2^mantissa]. Here mantissa is determined by x data type.

For example:

import torch
import torch.nn as nn

x = torch.empty([3,5], dtype = torch.float32)
print(x)
x.random_()

print("x = ")
print(x)

Run this code, we will get:

tensor([[-1.7629e-31,  1.0370e-42, -1.7629e-31,  1.0370e-42, -1.7629e-31],
        [ 1.0370e-42, -1.7629e-31,  1.0370e-42, -1.7631e-31,  1.0370e-42],
        [-1.7631e-31,  1.0370e-42, -1.7632e-31,  1.0370e-42, -1.7632e-31]])
x = 
tensor([[13760033., 13636719., 16242066.,  6668118.,  1255615.],
        [16138854., 10929515., 12875734., 15931016., 12547827.],
        [11036358.,  9970906., 15599017.,  9812039., 10870919.]])

Here the data type of x is float32.

If the data type of x is int8, how about the result?

import torch
import torch.nn as nn

x = torch.empty([3,5], dtype = torch.int8)
print(x)
x.random_()

print("x = ")
print(x)

We will get:

tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int8)
x = 
tensor([[104, 101,  61, 111,  10],
        [ 70,  54,  10, 102,  89],
        [ 57,  21,  93,   4, 125]], dtype=torch.int8)

Leave a Reply