skorch.history

Contains history class and helper functions.

class skorch.history.History[source]

History contains the information about the training history of a NeuralNet, facilitating some of the more common tasks that are occur during training.

When you want to log certain information during training (say, a particular score or the norm of the gradients), you should write them to the net’s history object.

It is basically a list of dicts for each epoch, that, again, contains a list of dicts for each batch. For convenience, it has enhanced slicing notation and some methods to write new items.

To access items from history, you may pass a tuple of up to four items:

  1. Slices along the epochs.
  2. Selects columns from history epochs, may be a single one or a tuple of column names.
  3. Slices along the batches.
  4. Selects columns from history batchs, may be a single one or a tuple of column names.

You may use a combination of the four items.

If you select columns that are not present in all epochs/batches, only those epochs/batches are chosen that contain said columns. If this set is empty, a KeyError is raised.

Examples

>>> # ACCESSING ITEMS
>>> # history of a fitted neural net
>>> history = net.history
>>> # get current epoch, a dict
>>> history[-1]
>>> # get train losses from all epochs, a list of floats
>>> history[:, 'train_loss']
>>> # get train and valid losses from all epochs, a list of tuples
>>> history[:, ('train_loss', 'valid_loss')]
>>> # get current batches, a list of dicts
>>> history[-1, 'batches']
>>> # get latest batch, a dict
>>> history[-1, 'batches', -1]
>>> # get train losses from current batch, a list of floats
>>> history[-1, 'batches', :, 'train_loss']
>>> # get train and valid losses from current batch, a list of tuples
>>> history[-1, 'batches', :, ('train_loss', 'valid_loss')]
>>> # WRITING ITEMS
>>> # add new epoch row
>>> history.new_epoch()
>>> # add an entry to current epoch
>>> history.record('my-score', 123)
>>> # add a batch row to the current epoch
>>> history.new_batch()
>>> # add an entry to the current batch
>>> history.record_batch('my-batch-score', 456)
>>> # overwrite entry of current batch
>>> history.record_batch('my-batch-score', 789)

Methods

append($self, object, /) Append object to the end of the list.
clear($self, /) Remove all items from list.
copy($self, /) Return a shallow copy of the list.
count($self, value, /) Return number of occurrences of value.
extend($self, iterable, /) Extend list by appending elements from the iterable.
from_file(f) Load the history of a NeuralNet from a json file.
index($self, value[, start, stop]) Return first index of value.
insert($self, index, object, /) Insert object before index.
new_batch() Register a new batch row for the current epoch.
new_epoch() Register a new epoch row.
pop($self[, index]) Remove and return item at index (default last).
record(attr, value) Add a new value to the given column for the current epoch.
record_batch(attr, value) Add a new value to the given column for the current batch.
remove($self, value, /) Remove first occurrence of value.
reverse($self, /) Reverse IN PLACE.
sort($self, /, *[, key, reverse]) Stable sort IN PLACE.
to_file(f) Saves the history as a json file.
to_list() Return history object as a list.
classmethod from_file(f)[source]

Load the history of a NeuralNet from a json file.

Parameters:
f : file-like object or str
new_batch()[source]

Register a new batch row for the current epoch.

new_epoch()[source]

Register a new epoch row.

record(attr, value)[source]

Add a new value to the given column for the current epoch.

record_batch(attr, value)[source]

Add a new value to the given column for the current batch.

to_file(f)[source]

Saves the history as a json file. In order to use this feature, the history must only contain JSON encodable Python data structures. Numpy and PyTorch types should not be in the history.

Parameters:
f : file-like object or str
to_list()[source]

Return history object as a list.