objax.io package¶
|
Helper class which performs saving and restoring of the variables. |
|
Loads values of all variables in the given variables collection from file. |
|
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.
-
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)¶ Load function, which loads variables collection from given file.
- Parameters
file (Union[str, IO[BinaryIO]]) –
vc (objax.variable.VarCollection) –
-
static
SAVE_FN
(file, vc)¶ Save function, which saves variables collection into given file.
- Parameters
file (Union[str, IO[BinaryIO]]) –
vc (objax.variable.VarCollection) –
-
__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
-
-
objax.io.
load_var_collection
(file, vc)[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.
- Raises
ValueError – if variable from variables collection is not found in the input file.