DEV Community

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

Posted on • Updated on

rand and rand_like in PyTorch

Buy Me a Coffee

*Memos:

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

*Memos:

  • rand() can be used with torch but not with a tensor.
  • The 1st or more arguments with torch are size(Required-Type:int, tuple of int, list of int or size()).
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

torch.rand(size=())
torch.rand(size=torch.tensor(8).size())
# tensor(0.2965)

torch.rand(size=(0,))
torch.rand(0)
torch.rand(size=torch.tensor([]).size())
# tensor([])

torch.rand(size=(3,))
torch.rand(3)
torch.rand(size=torch.tensor([8, 3, 6]).size())
# tensor([0.5138, 0.6443, 0.8991])

torch.rand(size=(3, 2))
torch.rand(3, 2)
torch.rand(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[0.0141, 0.5785],
#         [0.1218, 0.9181],
#         [0.6805, 0.2000]])

torch.rand(size=(3, 2, 4))
torch.rand(3, 2, 4)
# tensor([[[0.1121, 0.0019, 0.8928, 0.8047],
#          [0.2547, 0.4683, 0.1682, 0.1713]],
#         [[0.5103, 0.1068, 0.5911, 0.7893],
#          [0.0854, 0.3679, 0.4761, 0.6386]],
#         [[0.4480, 0.0795, 0.3199, 0.0032],
#          [0.4238, 0.3069, 0.9529, 0.4462]]])

torch.rand(size=(3, 2, 4), dtype=torch.complex64)
torch.rand(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.7756+0.0131j, 0.4101+0.8469j, 0.7885+0.6149j, 0.1891+0.7311j],
#          [0.9582+0.4655j, 0.7719+0.1614j, 0.6985+0.5411j, 0.9334+0.7705j]],
#         [[0.9722+0.4131j, 0.4887+0.4382j, 0.4342+0.0959j, 0.1571+0.3554j],
#          [0.1554+0.0313j, 0.2604+0.5255j, 0.0083+0.6250j, 0.9909+0.3338j]],
#         [[0.2127+0.8406j, 0.5262+0.7138j, 0.1354+0.1573j, 0.3121+0.0703j],
#          [0.3213+0.3891j, 0.6951+0.5847j, 0.0853+0.5330j, 0.3310+0.6671j]]])
Enter fullscreen mode Exit fullscreen mode

rand_like() can replace the zero or more floating-point numbers or complex numbers of a 0D or more D tensor with the zero or more random floating-point numbers or complex numbers between 0 and 1(0<=x<1) as shown below:

*Memos:

  • rand_like() can be used with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of float or complex).
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
    • If it's None, it's inferred from input.
    • device= must be used.
    • My post explains device argument.
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
import torch

my_tensor = torch.tensor(7.)

torch.rand_like(input=my_tensor)
# tensor(0.3634)

my_tensor = torch.tensor([7., 4., 5.])

torch.rand_like(input=my_tensor)
# tensor([0.9000, 0.1314, 0.5240])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])

torch.rand_like(input=my_tensor)
# tensor([[0.7508, 0.5857, 0.1658],
#         [0.3327, 0.1836, 0.3776]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]], 
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.rand_like(input=my_tensor)
# tensor([[[0.8467, 0.3134, 0.9486], [0.3134, 0.1809, 0.1740]],
#         [[0.2624, 0.3929, 0.2138], [0.2034, 0.9147, 0.3421]]])

my_tensor = torch.tensor([[[7.+4.j, 4.+2.j, 5.+3.j],
                           [2.+5.j, 8.+1.j, 3.+9.j]],
                          [[6.+9.j, 0.+3.j, 1.+8.j],
                           [5.+3.j, 9.+4.j, 4.+6.j]]])
torch.rand_like(input=my_tensor)
# tensor([[[0.9179+0.7615j, 0.6811+0.3994j, 0.6854+0.3131j],
#          [0.7352+0.0108j, 0.0999+0.4509j, 0.9586+0.1530j]],
#         [[0.5253+0.7685j, 0.5731+0.0358j, 0.9499+0.8943j],
#          [0.7742+0.3421j, 0.0419+0.8636j, 0.1014+0.8507j]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)