PyTorch Recipe

Performance Measurement

Main Issues of Time Measurement GPU Execution Mechanism: Asynchronous Execution In multithreaded or multi-device programming, two blocks of code that are independent can be executed in parallel. This means that the second block may be executed before the first is finished.

2021-05-24

TorchScript

TorchScript A PyTorch model’s journey from Python to C++ is enabled by Torch Script, a representation of a PyTorch model that can be understood, compiled and serialized by the Torch Script compiler.

2021-04-21

Data Augmentation

What is data augmentation? To solve the problem that it’s hard to get enough data for training neural networks, image augmentation is a process of creating new training examples from the existing ones.

2021-01-17

Saving and Loading Models

Three core functions for saving and loading models: torch.save Saves a serialized object to disk. This function uses Python’s pickle utility for serialization. Models, tensors, and dictionaries of all kinds of objects can be saved using this function.

2021-01-17

🔥🧾 General Training Steps Using PyTorch

Open in Google Colab General steps: Set device Set Dataset and DataLoader Define network model Build network model Define loss function and optimizer Define training process Train the model Store/Load weights

2020-11-26

🔥 Custom Datasets and Transforms

Custom Dataset In order to use our custom dataset, we need to inherit torch.utils.data.Dataset , an abstract class representing a dataset. override __len__ so that len(dataset) returns the size of the dataset.

2020-11-26

nn ModuleList vs. Sequential

import torch import torch.nn as nn import torch.nn.functional as F nn.Module Defines the base class for all neural network We MUST subclass it Example class Net(nn.Module): def __init__(self, in_c, n_classes): super().

2020-11-09

Saving and Loading Checkpoints

Motivation Saving and loading a general checkpoint model for inference or resuming training can be helpful for picking up where we last left off. When saving a general checkpoint, you must save more than just the model’s state_dict.

2020-11-06

🔥 Transfer Learning for Computer Vision

Handling settings for training and valiadtion phase flexibly 💡 Use Python dictionary Phase ('train' or 'val') as key For example: data_transforms = { # For training: data augmentation and normalization 'train': transforms.

2020-11-03

‼️ Issues & Gotchas

Issues and gotchas which may occur in practice.

2020-09-07