The basic functions of PyTorch everyone should know
PyTorch is very powerful package for deep learning.It provides two high-level features:
- Tensor computation (like NumPy) with strong GPU acceleration.
- Deep neural networks built on a tape-based autograd system.
In mathematics, a tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects related to a vector space.Objects that tensors may map between include vectors and scalars , and recursively, even other tensors.
Now we have a basic understanding of what is PyTorch and Tensors, here is the list of 5 interesting functions to know.
torch.linspace()
The torch.linspace function returns a 1-D tensor with points between a starting parameter and ending parameter. The number of points depend on your step parameter.
torch.log()
The torch.log function simply returns the natural log of each value from another corresponding tensor.
torch.mean()
This function returns the mean or average of your tensor.
We can see in the second example the torch.mean function is applied to (3,4) matrix and the dimension parameter is set to 0. This is specifying that the mean should be calculated along all rows for each column. If you want to calculate mean for all columns for each row the dimension shold be set to 1.
torch.reshape()
The torch.reshape functon reshapes your tensor into another shape,if applicable.The specified shape would need to fit the number elements in your tensor.
When specifying the reshape size, we need to make the specified size will fit the number of elements in out input tensor.The size has to be just right, it cant be too big or too small or else you will end up with the run time error.
torch.t()
This function transposes your tensor based on the dimensions of that tensor. So if you had (n,d) tensor then after applyting this function you get a (d,n) tensor.
In the second example, since our input tensor is of one dimension vector and hence the transpose will be same 1-D vector.This will apply to 0-D and 1-D vectors.