Pytorch toturial
PyTorch is a python Deep Learning open library developped and maintained by Facebook. In this notebook, we will explore 5 functions and show how they work and break. The chosen functions as listed below:
- function 1: torch.randn
- function 2: torch.numel
- function 3: torch.cat
- function 4: torch.reshape
- function 5: torch.where
They correspond respectively for generating normal random samples, computing the number of elements of a tensor, concatenating and reshaping tensors, and filtering and replacing tensors values.
# Import torch and other required modules
import torch
Function 1 - torch.randn
The function torch.randn can be used to generate random samples from a normal distribution given a size and data type. Let's illustrate how it works and brake.
# Example 1 - working
rand_samples_1 = torch.randn((2, 3, 4), dtype=torch.float64)
rand_samples_1
tensor([[[-0.3894, -1.0230, 0.3322, 0.1062],
[-0.3839, -0.1314, 1.1745, -0.7863],
[ 0.4125, 1.9637, -0.1670, -2.3159]],
[[-0.8091, -0.6501, -0.6064, 0.6309],
[-0.5878, -1.6139, 0.4727, 0.6539],
[-0.5988, 0.3458, -1.9131, -0.1992]]], dtype=torch.float64)
In the example above, we generate a random normal distribution tensor with size (2, 3, 4) and assign it to the variable rand_samples. The size (2, 3, 4) means that we will the rand_samples will contain 2 matrix of size (3, 4) each. We've all specify the type of data to torch.float64.