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

Assignment 1:

Tensor-Operations

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab.

  • function 1 torch.std_mean
    it will return mean and standard deviation.
  • function 2 torch.var
    Returns the variance of each row of the input tensor in the given dimension dim.
  • function 3 torch.mul
    Multiplies each element of the input input with the scalar other and returns a new resulting tensor.
  • function 4 torch.div
    Divides each element of the input input with the scalar other and returns a new resulting tensor.
  • function 5 torch.reciprocal
    Returns a new tensor with the reciprocal of the elements of input
# Import torch and other required modules
import torch

Function 1 - torch.std_mean

torch.std_mean(input, unbiased=True) -> (Tensor, Tensor)

Returns the standard-deviation and mean of all elements in the input tensor.

# Example 1 - working (change this)
a = torch.randn(5,5)
print (a)
torch.std_mean(a,True)
tensor([[-0.0489, -0.3833, 2.3151, -0.5002, 0.4141], [-1.4792, 1.6757, -0.5013, -1.2570, 1.8524], [-0.4884, -0.1377, -0.1736, -1.3417, 0.2077], [-1.0843, 0.0414, -0.9005, 1.2514, -0.3942], [ 0.0246, -0.6541, 0.4642, -0.7626, -0.2874]])
(tensor(0.9822), tensor(-0.0859))

In above example torch.randn (5,5) will create a 5 x 5 matrix and then save it as a tensore name as a "a"
then we apply our function which will calculate standard deviation and mean values. here unbiased is True mans it will use Bessel’s correction will be used.

standard deviation : This is the formula for Standard Deviation:
[ (1/N) times Sigma i=1 to N of (input - mean)^2 ]^(1/2)