#!/usr/bin/env python # coding: utf-8 # # Trial History and Replay # # This guide will give a breif overview of the history returned by a `Trial` from a call to fit or evaluate. # # **Note**: The easiest way to use this tutorial is as a colab notebook, which allows you to dive in with no setup. # # ## Install Torchbearer # # First we install torchbearer if needed. # In[1]: try: import torchbearer except: get_ipython().system('pip install -q torchbearer') import torchbearer print(torchbearer.__version__) # ## A Simple Example # # We first create some data and a simple model to train on. # In[2]: import torch import torch.nn as nn class BasicModel(nn.Module): def __init__(self): super(BasicModel, self).__init__() self.linear1 = nn.Linear(100, 25) self.linear2 = nn.Linear(25, 1) def forward(self, x): x = self.linear1(x) x = self.linear2(x) return torch.sigmoid(x).squeeze(1) from torch.utils.data import TensorDataset, DataLoader import numpy as np from sklearn.datasets.samples_generator import make_blobs X, Y = make_blobs(n_samples=2048, n_features=100, centers=2, cluster_std=10, random_state=1) X = (X - X.mean()) / X.std() Y[np.where(Y == 0)] = -1 X, Y = torch.FloatTensor(X), torch.FloatTensor(Y) traingen = DataLoader(TensorDataset(X, Y), batch_size=128) # Next, we'll run the model for a few epochs to obtain a history. # In[3]: import torch.optim as optim import torch.nn.functional as F from torchbearer import Trial model = BasicModel() optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001) trial = Trial(model, optimizer=optimizer, criterion=F.binary_cross_entropy, metrics=['mse', 'acc', 'loss']) trial.with_train_generator(traingen) history = trial.run(epochs=20, verbose=1) # ## The History # # The history is a list of metric dictionaries from each epoch of training. History also includes the number of training and validation steps from each epoch. Let's take a look # In[4]: print(len(history)) print(history[5]) # ### With Pandas # Suppose that we wanted to use pandas to plot our training progress or similar, we could do that with the following # In[5]: import pandas as pd frame = pd.DataFrame.from_records(history) print(frame) # We can now use all of the built-in pandas functions, such as plotting # In[6]: get_ipython().run_line_magic('matplotlib', 'inline') frame.reset_index().plot('index', 'binary_acc') # ## Replay a Trial # # One of the perks of history is the ability to replay a trial. We'll look at two of the replay options here. First we can just replay the whole training process, this time with a different verbosity. # In[7]: _ = trial.replay(verbose=2) # This may be more output than we desire, and so we can instead use the `one_batch` flag to just simulate one batch per epoch. # In[8]: _ = trial.replay(verbose=2, one_batch=True) # So that's history and replaying in `torchbearer`. Be sure to have a look at our other examples at [pytorchbearer.org](http://www.pytorchbearer.org/). # In[ ]: