Ben Chuanlong Du's Blog

It is never too late to learn.

Make Your Model Training Reproducible in PyTorch

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

The PyTorch doc Reproducibility has very detailed instructions on how to make your model training reproducible. Basically, you need the following code.

torch.manual_seed(args.seed)
np.random.seed(args.seed …

Delete a Layer in a Pretrained Model in PyTorch

It is common to customize a pretrained model by delete the output layer or replace it to the output layer that suits your use case. There are several ways to achieve this in PyTorch.

Replace the Fully Connected Layer with an Identity Layer

  1. Define an identity layer.

  2. Replace the fully connected layer with an Identity Layer (using ResNet18 as an example).

Save and Load PyTorch Models

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

  1. PyTorch uses pickle to serialize and deserialize objects.

  2. The PyTorch convention is to use the file extension .pt or .pth for saving model (or its parameters) and use the file extension …

Tips on Deep Graph Learning

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

https://github.com/dmlc/dgl

Convert a Tensor to a Numpy Array or List in PyTorch

Tips

There are multiple ways to convert a Tensor to a numpy array in PyTorch. First, you can call the method Tensor.numpy.

my_tensor.numpy()

Second, you can use the function numpy.array.

import numpy as np
np.array(my_tensor)

It is suggested that you use the function numpy.array to convert a Tensor to a numpy array. The reason is that numpy.array is more generic. You can also use it to convert other objects (e.g., PIL.Image) to numpy arrays while those objects might not have a method named numpy