[Rapid learning] Introduction to Pytorch ①: Try dealing with torch

2020/4/12

What is PyTorch?

PyTorch is a deep learning framework developed by Facebook. Compared to TensorFlow and keras, the user population is small, but it features a Define by run format that allows flexible network construction, and is growing rapidly now.Originally it was Chainer's fork, but since it was widely spread to researchers in Europe and the United States in advance, it seems that the PFN side has now stopped Chainer and cooperated with and joined the development of PyTorch.

PyTorch features the following two features.

・ "Torch" equivalent to NumPy that can be accelerated by GPU
・ Flexible and fast DefineByRun type deep learning platform

How to install PyTorch from this official page:https://pytorch.org/
Details etc. are covered in many Japanese articles (Reference:Getting Started with PyTorch!6 basic knowledge you need to know about the rising popularity of PyTorch

How to use Torch

In Pytorch, even if you input numpy type data, it cannot be calculated, and the calculation is performed using the data type called torch.tensor.Therefore, it is necessary to create and convert the data with torch.tensor type.It's almost like numpy, but it's capable of fast operations on Nvidia's GPU.

In addition, the torch module contains a multidimensional tensor (like a high-order matrix) data structure, which enables efficient tensor calculation and type conversion.

In 2]:
・ You can check the tensor size with .size ()
-The elements of the matrix can be extracted by slicing the list and can be handled in the same way as a numpy array.
In 3]: Basic operations such as addition / subtraction remainder and differentiation can be performed
In 4]: Change the shape of the array with .view ()
In 5]: Can be converted to and from numpy
In 6]: To calculate on GPU, pass with .to (device)