Overview of Pytorch

Pytorch is a library for deep learning. It's a collection of technology, not just python. Pythonic syntax is for ease of use. There is also: C++, CUDA for performance. What is CUDA : it is a C++ like language for programming NVIDIA GPUs. GPU is for accelerating training, and or distributed, parallel analysis. We can use Pytorch to build models, train models, and use the trained models to solve real world problems.There's also C++ runtime for deploying models for inference.

Why Pytorch : Pytorch is an easy-to-use, top machine learning framework. It is easy to use for prototyping and research and can be used in production. It is supported by Facebook and it is open-sourced. For installation you will want to visit the installation page of the official website. We recommend skipping the complex configuration and use Google Colab for your Pytorch study.Google Colab, in-browser notebook for data scientists and machine learning practitioners According to pytorch developer conferences: pytorch is used at Uber, Tesla, Microsoft, Captum, Fastai, MARS, AllenNLP, ClassyVision. Pytorch is believed to be popular among university researchers too. Many deep learning papers use either Pytorch and/or Tensorflow. In Pytorch's own words: the machine learning framework provides important components deep learning such as neural network layers, activation functions, loss functions, and optimizers. It also allows developers to write python code, use autograd, eager mode (as opposed to lazy graph compute). There are production and deployment features: torchscript, torchserve, quantization. Ever since Pytorch 1.0, the library is supposed to be research friendly, as well as production ready-ish.

Keep in mind all our content in all our sites, channels and social media are for educational, information purpose only. Our tutorials are not for commercial usage, not for production. They are not production ready code. Read our full disclaimer before using.

Check out our Medium Pytorch guide Pytorch code snippet, cheat sheet - Uniqtech Guide [medium]. This article is featured on KDnuggets.

Pytorch pro tip flash card - Uniqtech

Frequently used Pytorch libraries. Below are three modules that are often used. torch.nn for neural networks, torch.nn.functional for layers activations, and finally optim for the optimizer, source pytorch official documentation


Tensors are essential components of Pytorch. It is the main data structure used to handle deep learning data. Don't scared by the name. A 1 dimensional tensor is a list of numbers with a lot of fancy deep learning capabilities. A 2 dimensional tensor is a matrix of numbers with fancy DL super powers. Usually tensor referrs to 3 dimensional and above. In Deep Learning, there's plenty of high dimensional data such as language models word embeddings.

POSTED ON MAY 2, 2018 TO AI RESEARCH, OPEN SOURCE: "PyTorch provides Tensors that can live either on the CPU or the GPU, and accelerates the computation by a huge amount.""... provide a wide variety of tensor routines to accelerate and fit your scientific computation needs such as slicing, indexing, math operations, linear algebra, reductions. And they are fast! [Source 2]"

Initiate a Model

What is a sequential? A sequential can be seen as a container that contains a stack of deep learning layers. It's similar for Keras and Pytorch. Pytorch Official Documentation on Sequential The explanation on Pytorch doc is a bit dense. It mentions several important characteristics : ... Sequential Model flash card - Uniqtech [Public, Free] ... Now check out the sample code snippet Code snippet Pytorch Sequential

There's the a objected-oriented programming OOP way of initating a model.

Pytorch Forward Function

Another name is for the forward function is the forward pass.

Pytorch training loop

sklearn (scikitlearn) and keras just call .fit() to train. The format is model.fit(X_train, y_train). In Pytorch you will have to do a bit DIY your own custom training loop.

You will have to write a bit of custom training loop to train your deep learning model in libraries like Pytorch. In Tensorflow you might be able to just call .fit() high level API. Classical machine learning models in Scikit-Learn is also abstracted and organized behind the high level API. Here's a code snippet example of the training loop, illustrated line by line. Anatomy of Pytorch Training Loop - Pytorch training loop line by line walkthrough

What is model.eval()? What is evaluation mode.

Pytorch classes

Pytorch ImageFolder Pytorch Dataloader

Pytorch libraries

torchvision for computer vision. torchtext . PyTorch Geometric .

torchvision, pytorch library for computer vision, includes pre-trained models and package datasets (buildin datasets)

Auto Grad, Auto Gradient, Auto Differentiation

"Pytorch can compute gradient automatically for common formula common architecture." Most modern deep learning library can do that.

Transfer Learning

Training large image and natural language models cost time, resources, human resources, compute power and requires collection of large training dataset. Often, we use transfer learning to resolve this costly problem. Transfer learning is when we use models trained on other related datasets, use it to make prediction on our own dataset, often with a bit of adjustment and fine tuning. It is common practice to train the final one or few layers before using. It is also common we have to change a few parameters such as out_features. For example, if a model is trained on imagenet dataset, then it can predict 1000 classes, in reality we need it to predict a smaller number of classes 10 for example. Transfer Learning using Pytorch [Medium] Uniqtech Guide

Additional Reading

Pytorch cheat sheet [pro, paid member]

How Tesla uses Pytorch