objax.io package

Checkpoint(logdir, keep_ckpts[, makedir, ...])

Helper class which performs saving and restoring of the variables.

load_var_collection(file, vc[, renamer])

Loads values of all variables in the given variables collection from file.

save_var_collection(file, vc)

Saves variables collection into file.

class objax.io.Checkpoint(logdir, keep_ckpts, makedir=True, verbose=True)[source]

Helper class which performs saving and restoring of the variables.

Variables are stored in the checkpoint files. One checkpoint file stores a single snapshot of the variables. Different checkpoint files store different snapshots of the variables (for example at different training step). Each checkpoint has associated index, which is used to identify time when snapshot of the variables was made. Typically training step or training epoch are used as an index.

Parameters
  • logdir (str) –

  • keep_ckpts (int) –

  • makedir (bool) –

  • verbose (bool) –

DIR_NAME: str = 'ckpt'

Name of the subdirectory of model directory where checkpoints will be saved.

FILE_MATCH: str = '*.npz'

File pattern which is used to search for checkpoint files.

FILE_FORMAT: str = '%010d.npz'

Format of the filename of one checkpoint file.

static LOAD_FN(file, vc, renamer=None)

Load function, which loads variables collection from given file.

Parameters
static SAVE_FN(file, vc)

Save function, which saves variables collection into given file.

Parameters
__init__(logdir, keep_ckpts, makedir=True, verbose=True)[source]

Creates instance of the Checkpoint class.

Parameters
  • logdir (str) – model directory. Checkpoints will be saved in the subdirectory of model directory.

  • keep_ckpts (int) – maximum number of checkpoints to keep.

  • makedir (bool) – if True then directory for checkpoints will be created, otherwise it’s expected that directory already exists.

  • verbose (bool) – if True then print when data is restored from checkpoint.

static checkpoint_idx(filename)[source]

Returns index of checkpoint from given checkpoint filename.

Parameters

filename (str) – checkpoint filename.

Returns

checkpoint index.

restore(vc, idx=None)[source]

Restores values of all variables of given variables collection from the checkpoint.

Old values from the variables collection will be replaced with the new values read from checkpoint. If variable does not exist in the variables collection, it won’t be restored from checkpoint.

Parameters
  • vc (objax.variable.VarCollection) – variables collection to restore.

  • idx (Optional[int]) – if provided then checkpoint index to use, if None then latest checkpoint will be restored.

Returns

index of the restored checkpoint. ckpt: full path to the restored checkpoint.

Return type

idx

save(vc, idx)[source]

Saves variables collection to checkpoint with given index.

Parameters
  • vc (objax.variable.VarCollection) – variables collection to save.

  • idx (int) – index of the new checkpoint where variables should be saved.

objax.io.load_var_collection(file, vc, renamer=None)[source]

Loads values of all variables in the given variables collection from file.

Values loaded from file will replace old values in the variables collection. If variable exists in the file, but does not exist in the variables collection it will be ignored. If variable exists in the variables collection, but not found in the file then exception will be raised.

Parameters
  • file (Union[str, IO[BinaryIO]]) – filename or python file handle of the input file.

  • vc (objax.variable.VarCollection) – variables collection which will be loaded from file.

  • renamer (Optional[objax.util.util.Renamer]) – optional renamer to pre-process variables names from the file being read.

Raises

ValueError – if variable from variables collection is not found in the input file.

objax.io.save_var_collection(file, vc)[source]

Saves variables collection into file.

Parameters
  • file (Union[str, IO[BinaryIO]]) – filename or python file handle of the file where variables will be saved.

  • vc (objax.variable.VarCollection) – variables collection which will be saved into file.