“Deep Learning with PyTorch: Zero to GANs”

It is an online course intended to provide a coding first introduction to deep learning using the PyTorch framework.

PyTorch is an open source machine learning framework that fast tracks the path from research prototyping to production deployment. It was primarily developed by Facebook’s artificial intelligence research group.

PyTorch is predominantly used to implement various neural network architectures like recurrent neural networks (RNNs), convolution neural networks (CNNs), long term short memory (LSTM), and other similar high-level neural networks.

This course is taught by Aakash Sir and his team at Jovian.ml in collaboration with Freecodecamp, I’d like to briefly dive into some of the tensor functions in the PyTorch library.So that we can develop a solid understanding of PyTorch tensors.

# PyTorch Basics:

-> A short introduction about PyTorch and about the chosen functions:

1.) torch.mean

2.) torch.eye

3.) torch.reshape

4.) torch.full

5.) torch.log

-— -> Function 1.) torch.mean

torch.mean(input) → Tensor

The torch.mean function returns the mean or average of your tensor.

  • > Example 1.(explanation) As we can see in this example torch.randn has generated the tensor of 4 rows and 5 columns and the torch.mean function is applied which is just adding all the numbers of our tensor and dividing it by the count.
  • >Example 2.(explanation) As we can see in this example torch.randn has generated the tensor of 2 rows and 4 columns and the torch.mean function is applied which is just adding all the numbers of our tensor and dividing it by the count. Here comes yet another thing to see,we have set dim parameter to 0. This is specifying that we want the average over all rows (for each column)
  • >Example 3.(explanation) As we can see in this example torch.randn has generated the tensor of 2 rows and 4 columns and the torch.mean function is applied which is just adding all the numbers of our tensor and dividing it by the count. Here comes yet another thing to see,we have set dim parameter to 1. This is specifying that we want the average over all columns (for each row)
  • >Example 4.(explanation) Applying the mean function to this boolean tensor results in a RuntimeError because you cannot get the mean of boolean data types. That is why the RunetimeError says it can only calculate the mean of floating types.

This function should be used when you need to obtain the mean of your tensor, you can also specify which dimension to calculate the mean.Also you can set dim to 0 or 1 according to your need.

— — -> Function 2.) torch.eye

The torch.eye function generates a tensor that is an identity matrix,it takes the size of the matrix as a parameter

  • >Example 1.(explanation) So,we have passed parameter 4 ,so a tensor of 4 rows and 4 columns wiil be generated,torch.eye function generates a tensor that is basically an identity matrix.
  • >Example 2.(explanation) So,we have passed parameter 10 ,so a tensor of 10 rows and 10 columns wiil be generated,torch.eye function generates a tensor that is basically an identity matrix.And then we have multiplied it by 7 to get the desired tensor.

This function should be used when you need to obtain the identity matrix of desired size.

— — -> Function 3.) torch.reshape

torch.reshape(input, shape) → Tensor

The torch.reshape function reshapes your tensor into another shape, if applicable. The specified shape would need to fit the number elements in your tensor.

  • >Example 1.(explanation) As we can see in this example torch.randn has generated the tensor of 2 rows and 10 columns,now we have reshaped our tensor temp into a (4, 5) matrix.

->Example 2.(explanation) As we can see in this example torch.randn has generated the tensor of 16 rows and 2 columns,now we have reshape our tensor temp into aedit (8, 4) matrix.

  • >Example 3.(explanation) When specifying the reshape size, we need to make the specified size that will fit the number of elements in our input tensor. Otherwise you will end up with a RuntimeError of invalid size

This function should be used if you need to reshape your data.

-— -> Function 4.) torch.full

The toch.full function generates a tensor of a specified size with all elements being equal to the number specified.

  • >Example 1.(explanation) It has generated the tensor of 4 rows and 5 columns with all elements of tensor being equal to 7.
  • >Example 2.(explanation) It has generated the tensor of 2 rows and 5 columns with all elements of tensor being equal to 4.

This function should be used when you want your tensor to contain all the elements equal to the number specified.

— — -> Function 5.) torch.log

torch.log(input, out=None) → Tensor

The torch.log function simply returns the natural log

  • >Example 1.(explanation) As we can see in this example torch.randn has generated the tensor of 4 rows and 6 columns ,now the torch.log function is applied to a (4, 6) matrix. As We can see some resulting values labeled as ‘nan’ since the natural log of a negative number is undefined.
  • >Example 2.(explanation) As we can see in this example torch.randn has generated the tensor of 4 rows and 6 columns ,now the torch.log function is applied to a (4, 6) matrix. As We can see some resulting values labeled as ‘nan’ since the natural log of a negative number is undefined.

->Example 3.(explanation) We can see in this example that the torch.log function requires the input to be a tensor object type. We cannot simply pass in an array object or else we get a TypeError.

This function should be used when you need to take the natural log of all values in your tensor.

CONCLUSION

https://pytorch.org/docs/stable/tensors.html

For those interested in learning more about Deep Learning with PyTorch, be sure to visit Jovian.ml and follow along.

REFERENCES:

https://jovian.ml/kanchanjeswani2000/01-tensor-operations-b0234

https://jovian.ml/aakashns/01-pytorch-basics

https://pytorch.org/docs/stable/tensors.html

COMPETITIVE PROGRAMMER|MACHINE LEARNING ENTHUSIAST|ALGORITHMIC ENTHUSIAST