<jupyter_start><jupyter_text>5 Statistical Function in PyTorch Subtitle HereAn short introduction about PyTorch and about the chosen functions. - torch.bernoulli()- torch.poisson()- torch.normal()- torch.normal_()- torch.norm()<jupyter_code># Import torch and other required modules
import torch<jupyter_output><empty_output><jupyter_text>Function 1 - torch.bernoulli(input, *, generator=None, out=None)It draws  binary random numbers (0 or 1) from a Bernoulli distribution.Parameters : input (Tensor) – the input tensor of probability values for the Bernoulli distributiongenerator (torch.Generator, optional) – a pseudorandom number generator for samplingout (Tensor, optional) – out tensor only has values 0 or 1 and is of the same shape as input.<jupyter_code># Example 1 - # generate a  matrix 
a=torch.tensor([[0.33,0.55,0.99],[0.09,0.78,0.89],[0.29,0.19,0.39]])
#Draws a binary random nuber(0 or 1)
torch.bernoulli(a)<jupyter_output><empty_output><jupyter_text>All one's in the matrix<jupyter_code># Example 2 - Here all are one's in the matrix
b = torch.ones(5, 5) # probability of drawing "1" is 1
b
#draw a binary number
torch.bernoulli(b)<jupyter_output><empty_output><jupyter_text>All zero's in the matrix<jupyter_code># Example 3 - 
c = torch.zeros(3, 3) # probability of drawing "1" is 0
c
#draw a binary number
torch.bernoulli(c)<jupyter_output><empty_output><jupyter_text>These are 3 types of example of torch.bernoulli() function Bernoulli Distribution is a random experiment that has only two outcomes (usually called a “Success” or a “Failure”).It is best used when we have two outcomes of a given event.  Function 2 - torch.poisson(input *, generator=None)It returns a tensor sampled from a Poisson distribution.Parameters:input (Tensor) – the input tensor containing the rates of the Poisson distributiongenerator (torch.Generator, optional) – a pseudorandom number generator for sampling<jupyter_code># Example 1 - working
rates = torch.rand(3, 3) * 6  # rate parameter between 0 and 6 with 3 row and 3 col 
rates
torch.poisson(rates)<jupyter_output><empty_output><jupyter_text>A input tensor contain rate parameter between 0 and 6 with 3 rows and 3 columns, returns a output from a poission distribution<jupyter_code># Example 2 - working
rates = torch.rand(5, 4) * 7  # rate parameter between 0 and 7 with 5 row and 4 col 
torch.poisson(rates)<jupyter_output><empty_output><jupyter_text>A input tensor contain rate parameter between 0 and 7 with 5 rows and 4 columns, returns a output from a poission distribution The Poisson distribution is popular for modeling the number of times an event occurs in an interval of time or space.  Function 3 -  torch.normal(mean, std, *, generator=None, out=None) It returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.The mean is a tensor with the mean of each output element’s normal distributionThe std is a tensor with the standard deviation of each output element’s normal distributionParameters :    mean (Tensor) – the tensor of per-element means    std (Tensor) – the tensor of per-element standard deviations    generator (torch.Generator, optional) – a pseudorandom number generator for sampling    out (Tensor, optional) – the output tensor.<jupyter_code># Example 1 - working
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))<jupyter_output><empty_output><jupyter_text>It returns a tensor of random numbers drawn from separate normal distributions.<jupyter_code># Example 2 - torch.normal(mean, std=1.0, out=None) → Tensor
#Similar to the function above, but the standard-deviations are shared among all drawn elements.

#Parameters
#mean (Tensor) – the tensor of per-element means

#std (float, optional) – the standard deviation for all distributions

#out (Tensor, optional) – the output tensor
torch.normal(mean=torch.arange(3., 9.))<jupyter_output><empty_output><jupyter_text>It returns a tensor of random numbers drawn from separate normal distributions whose  standard deviation is 1<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
torch.normal(mean=0,std=1)<jupyter_output><empty_output><jupyter_text>It does not work when mean is 0 and std is 1. The normal distribution is a probability function that describes how the values of a variable are distributed  Function 4 - normal_(mean=0, std=1, *, generator=None)    In-place version of torch.normal()    Fills self tensor with elements samples from the normal distribution parameterized by mean and std.<jupyter_code># Example 1 - working
t = torch.Tensor(5, 5)
t.normal_() # Tensor.normal_(mean=0, std=1)<jupyter_output><empty_output><jupyter_text>It creates a standard normal distribution with mean =0 and std=1<jupyter_code># Example 2 - working
#Let's create a matrix Z (a 1d tensor) of dimension 1 × 5
torch.empty(5).normal_(mean=4,std=0.5) # random normal distribution in pytorch and mean and std are 4, 0.5 respectively<jupyter_output><empty_output><jupyter_text>It create a matrix Z (a 1d tensor) of dimension 1 × 5, filled with random elements samples from the normal distribution parameterized by mean = 4 and std = 0.5. It helps to create standard normal distribution  Function 5 - torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None) It returns the matrix norm or vector norm of a given tensor.Parameters:    input (Tensor) – the input tensor    p (int, float, inf, -inf, 'fro', 'nuc', optional) -the order of norm. Default: 'fro'.        dim (int, 2-tuple of python:ints, 2-list of python:ints, optional) – If it is an int, vector norm will be calculated, if it is 2-tuple of ints, matrix norm will be calculated. If the value is None, matrix norm will be calculated when the input tensor only has two dimensions, vector norm will be calculated when the input tensor only has one dimension. If the input tensor has more than two dimensions, the vector norm will be applied to last dimension.     keepdim (bool, optional) – whether the output tensors have dim retained or not.      out (Tensor, optional) – the output tensor. Ignored if dim = None and out = None.     dtype (torch.dtype, optional) – the desired data type of returned tensor.<jupyter_code># Example 1 - working
k = torch.tensor([[ 1, 2, 3],[-1, 1, 4]] , dtype= torch.float)
torch.norm(k, dim=0)<jupyter_output><empty_output><jupyter_text>It returns vector norm of a given tensor where dim =0<jupyter_code># Example 2 - working
torch.norm(k, dim=1)<jupyter_output><empty_output><jupyter_text>It returns vector norm of a given tensor where dim =1<jupyter_code># Example 3  
torch.norm(k, p=1, dim=1)<jupyter_output><empty_output><jupyter_text>It returns vector norm of a given tensor where dim =1 and p=1 Matrix norms are indirectly used in any applications that require matrix functions and/or matrix series.  ConclusionThese are the 5 statistical PyTorch function that I find interesting as discussed above and link to the medium post:   Reference LinksProvide links to your references and other interesting articles about tensors* Official documentation for `torch.Tensor`: https://pytorch.org/docs/stable/tensors.html* https://stackoverflow.com/<jupyter_code>!pip install jovian --upgrade --quiet
import jovian
jovian.commit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08471555860178204, 'fraction_numerical': 0.019328307059629884, 'mean_word_length': 5.284237726098191, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '365912', 'license': 'MIT', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2094, 'n_words': 914, 'path': 'week 1/01-tensor-operations.ipynb', 'repo_name': 'anuraglahon16/DeepLearning-with-PyTorch', 'size': '7295'}