Learn practical skills, build real-world projects, and advance your career

A Random Sampling of Functions for Random Sampling in PyTorch

PyTorch is a Scientific computing package for Python that is similar to NumPy. However, PyTorch has some features that allow it to be more optimized for deep learning research with more flexibility and speed, since it allows the use of the computer's GPU for much faster processing. For my functions, I've selected items that deal with generating tensors that are filled with random numbers selected in different ways, and also breaking large tensors into more manageable pieces. I've explored some of the differences between these functions to determine their best use cases.

  • function 1 - torch.arange()
  • function 2 - torch.rand()
  • function 3 - torch.chunk()
  • function 4 - torch.randint_like()
  • function 5 - torch.randperm()
# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
!pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in c:\users\geralt\anaconda3\lib\site-packages (1.19.2) Requirement already satisfied: torch==1.7.0+cpu in c:\users\geralt\anaconda3\lib\site-packages (1.7.0+cpu) Requirement already satisfied: torchvision==0.8.1+cpu in c:\users\geralt\anaconda3\lib\site-packages (0.8.1+cpu) Requirement already satisfied: torchaudio==0.7.0 in c:\users\geralt\anaconda3\lib\site-packages (0.7.0) Requirement already satisfied: typing-extensions in c:\users\geralt\anaconda3\lib\site-packages (from torch==1.7.0+cpu) (3.7.4.3) Requirement already satisfied: future in c:\users\geralt\anaconda3\lib\site-packages (from torch==1.7.0+cpu) (0.18.2) Requirement already satisfied: dataclasses in c:\users\geralt\anaconda3\lib\site-packages (from torch==1.7.0+cpu) (0.6) Requirement already satisfied: pillow>=4.1.1 in c:\users\geralt\anaconda3\lib\site-packages (from torchvision==0.8.1+cpu) (8.0.1)
# Import torch and other required modules
import torch

Function 1 - .arange()

The torch.arange function is used to create a tensor that is populated with intergers from a specified range. This is useful whenever you need to generate a tensor with a specific size and/or shape that contains numbers with the same step value. This can be used in whatever context the situation calls for. I will show this in the examples below.

x= torch.tensor([[2,5,1],
               [4,2,5],
               [9,3,6]], dtype = torch.float32)
s= x.shape
y= torch.arange(2,6.5,.5, dtype= torch.float32)
y=y.reshape(s)
print('x= ',x)
print('y= ',y)
x= tensor([[2., 5., 1.], [4., 2., 5.], [9., 3., 6.]]) y= tensor([[2.0000, 2.5000, 3.0000], [3.5000, 4.0000, 4.5000], [5.0000, 5.5000, 6.0000]]) torch.Size([3, 3])