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

One Step closer to Deep Learning: 5 Important Functions to start PyTorch

PyTorch is an open-source machine learning library for Python, based on Torch, used for applications such as natural language processing. They main target is that:

  1. Use power of GPU and replace Numpy
  2. Maximum the speed and flexibility

#Journey Basic&Advance Pytorch
Here go nothing!

An short introduction about PyTorch and about the chosen functions.

  • torch reshape()
  • Basic Operations(eg. torch.add /sub /mul /div)
  • torch.mean()
  • torch.cat()
  • torch.flatten()
# Import torch and other required modules
import torch

Function 1 - torch.reshape

torch.reshape function is a very powerful method that able reshape your tensor in any shape you want.But in one condition, it need to fit the number elements in your tensor

# Example 1 - working 1 by 12 elements
import torch 

oldShape = torch.randn(1,12)
print(oldShape)

torch.reshape(oldShape,(2,6))
tensor([[ 0.6657, -0.7545, 0.7079, 2.3358, -0.8056, -0.9755, 0.1040, -0.5961, -0.5408, 0.3900, 1.0602, -0.5328]])
tensor([[ 0.6657, -0.7545,  0.7079,  2.3358, -0.8056, -0.9755],
        [ 0.1040, -0.5961, -0.5408,  0.3900,  1.0602, -0.5328]])

As you can see the, we can reshape our tensor "oldShape" which has 12 elements into a (2,6) matrix.