DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

randint() and randperm() in PyTorch

Buy Me a Coffee

*Memos:

randint() can create the 0D or more D tensor of the zero or more random integers(Default) or floating-point numbers between low and high-1(low<=x<=high-1) as shown below:

*Memos:

  • randint() can be used with torch but not with a tensor.
  • The 1st argument with torch is low(Optional-Default:0-Type:int): *Memos:
    • It must be lower than high.
    • The 0D or more D tensor of one integer works.
  • The 2nd argument with torch is high(Required-Type:int): *Memos:
    • It must be greater than low.
    • The 0D or more D tensor of one integer works.
  • The 3rd argument with torch is size(Required-Type:tuple of int, list of int or size()).
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, it is torch.int64.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

torch.randint(high=10, size=())
torch.randint(high=10, size=torch.tensor(8).size())
# tensor(7)

torch.randint(high=10, size=(3,))
torch.randint(high=10, size=torch.tensor([8, 3, 6]).size())
# tensor([7, 4, 8])

torch.randint(high=10, size=(3, 2))
torch.randint(high=10, size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[8, 9], [6, 5], [5, 2]])

torch.randint(high=10, size=(3, 2, 4))
# tensor([[[1, 5, 9, 0], [4, 6, 7, 2]],
#         [[5, 2, 1, 5], [9, 3, 2, 6]],
#         [[9, 3, 6, 4], [0, 4, 7, 5]]])

torch.randint(low=10, high=20, size=(3,))
# tensor([17, 12, 10])

torch.randint(low=10, high=20, size=(3, 2))
# tensor([[14, 18], [10, 19], [15, 16]])

torch.randint(low=10, high=20, size=(3, 2, 4))
# tensor([[[16, 14, 11, 19], [19, 15, 18, 13]],
#         [[14, 10, 11, 13], [16, 11, 10, 16]], 
#         [[17, 12, 17, 10], [13, 16, 11, 10]]])

torch.randint(low=-5, high=5, size=(3,))
# tensor([-1,  2, -3])

torch.randint(low=-5, high=5, size=(3, 2))
# tensor([[-5,  4], [ 1, -1], [-4, -3]])

torch.randint(low=-5, high=5, size=(3, 2, 4))
# tensor([[[-2, 0, 1, -5], [4, -5, -3, 1]],
#         [[-4, -1, -1, -1], [-3, 2, -4, -1]],
#         [[4, -1, -5, -3], [2, -3, -2, 2]]])

torch.randint(low=-5, high=5, size=(3, 2, 4), dtype=torch.float32)
torch.randint(low=torch.tensor(-5),
              high=torch.tensor([5]),
              size=(3, 2, 4),
              dtype=torch.float32)
# tensor([[[-4., 1., -1., -3.], [-3., -5., -4., 1.]],
#         [[-5., 3., 3., 1.], [-1., 4., -5., 2.]],
#         [[-2., -4., -5., 3.], [4., 1., -3., 3.]]])

torch.randint(high=1, size=(0,))
torch.randint(low=0, high=1, size=(0,))
torch.randint(low=10, high=20, size=(0,))
# tensor([], dtype=torch.int64)
Enter fullscreen mode Exit fullscreen mode

randperm() can create the 1D tensor of zero or more random integers(Default) or floating-point numbers between 0 and n-1(0<=x<=n-1) as shown below:

*Memos:

  • randperm() can be used with torch but not with a tensor.
  • The 1st argument with torch is n(Required-Type:int): *Memos:
    • It must be greater than or equal to 1.
    • The 0D or more D tensor of one integer works.
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, dtype is torch.int64.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

torch.randperm(n=0)
# tensor([], dtype=torch.int64)

torch.randperm(n=5)
# tensor([3, 0, 4, 2, 1])

torch.randperm(n=10)
# tensor([8, 6, 9, 2, 1, 3, 5, 0, 7, 4])

torch.randperm(n=10, dtype=torch.float32)
torch.randperm(n=torch.tensor([[10]]), dtype=torch.float32)
# tensor([7., 4., 2., 1., 8., 3., 0., 6., 9., 5.])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)