[PyTorch] 5. Pytorch Visualization, Splitting dataset, Save and Load a Model

jun94
jun-devpBlog
Published in
2 min readApr 7, 2020

1. PyTorch Visualization with Tensorboard

Tensor, image, figures that are used in PyTorch can be visualized via Tensorboard. The Tensorboard can be installed and launched with the following commands.

In order to visualize, firstly, we need to write scalar value, images, figures in the log file with the help of the SummaryWriter class in PyTorch as is shown in the code below.

In the code, the scalar value ‘training loss’ and ‘training accuracy’, and image are recorded in the log file by the SummaryWriter. By launching the tensorboard with the path(where the log file lies, it is ‘log_dir’ in the case of the above code), we can get the figures and trace its values.

Figure 1. Tensorboard

2. Splitting dataset

Once we complete collecting data to build the dataset, we need to divide it into three subsets, which are train, validation and test set.

The code below is what I used to split the dataset by giving the path where the dataset lies and the ratio of training and validation set.

In order to split train set and validation set, PyTorch supports much more convenient ways. One way is to use the SubsetRandomSampler as in the following codes. It simply takes the indices and extracts mini-batchs only in the range of given indices.

Another way is to use ‘torch.utils.data.random_split()’.

It does similar work as the SubsetRandomSampler but has a slight difference. For those differences, please refer here.

3. Save and Load a model

From PyTorch, codes to save and load a model

A common PyTorch convention is to save models using either a .pt or .pth file extension.

4. Reference

[1] PyTorch, Tensorboard tutorial

[2] PyTorch, official document

[3] SubsetRandomsampler1

[4] SubsetRandomsampler2

[5] DataCamp, SubsetRandomSampler

Any corrections, suggestions, and comments are welcome

--

--