Model API¶
The Plums Model Format main API is in the Model class which allows loading, saving and dynamic
creation through a high-level python API. Moreover, it implement python representations of common Model components
objects like Training, Producer, CheckpointCollection and Checkpoint.
See also
The Model developer API for more information on the Model API inner workings.
-
class
plums.model.model.Model(producer_name, producer_version_format, producer_version_string, model_name, model_id, configuration, build_parameters)[source]¶ Bases:
objectDefine a Python representation of a PMF model.
- Parameters
producer_name (str) – The name of the package that produced the model
producer_version_format (str) – The version format of the package that produced the model
producer_version_string (str) – The version string representation of the package that produced the model
model_name (str) – The model name
model_id (str) – The model identification string
configuration (Pathlike) – A path to the producer configuration file used to produce the model
build_parameters (dict) – A dictionary containing values necessary to instantiate the model from its weights.
-
build_parameters¶ A dictionary containing values necessary to instantiate the model from its weights.
- Type
-
checkpoint_collection¶ The
Modelattached collection ofCheckpoint.- Type
-
property
initialisation¶ The model initialisation network if any,
Noneotherwise.- Type
-
property
path¶ If the model was loaded from disk, the
Pathit was loaded from.Noneotherwise.- Type
PathLike
-
property
checkpoint¶ If the model is a
Modelinitialisation, the checkpoint reference used.Noneotherwise.- Type
hashable
-
add_checkpoint(*checkpoints)[source]¶ Add a
Checkpointto theModelcheckpoint_collection.- Parameters
*checkpoints (
Checkpoint) – One ore severalCheckpointto add to the modelCheckpointCollection.
-
register_epoch(epoch=None)[source]¶ Register a given epoch as being the latest epoch to the
trainingmetadata.- Parameters
epoch (int) – Optional. Default to
latest_epoch+ 1. The epoch to be registered as latest.
-
register_training_end(success)[source]¶ Register a training end or a training failure to the
trainingmetadata.
-
register_initialisation(path, checkpoint_reference=None, name=None)[source]¶ Register a valid PMF model initialisation from a
Pathtoinitialisation.- Parameters
path (PathLike) – A valid
Pathto the model initialisation.checkpoint_reference (hashable) – If initialising from a PMF model, one needs to provided a reference to the actual model
Checkpointused to construct a valid initialisation.name (str) – If initialising from a file, one needs to provide a name used to identify the initial network to construct a valid initialisation.
- Raises
OSError – If the path provided does not exists on the filesystem or points to neither a file nor a PMF model.
ValueError – If the arguments provided are incompatibles: e.g. using a PMF initialisation without a checkpoint_reference.
SchemaError – If the path provided point to an invalid PMF model.
See also
The
initialisationfunction for more information on theinitialisationformat.
-
save(path, force=False, **kwargs)[source]¶ Save a
ModeltoPath.- Parameters
- Raises
ValueError – If
pathpoints to a file.OSError – If
pathpoints to: * A non-empty directory which does not contains a PMF model andforceisFalse. * A non-empty directory which contains a PMF model with the sameidandforceisFalse. * A non-empty directory which contains a PMF model with a differentid. * A non-empty directory which contains a PMF model with an invalid metadata file.
-
classmethod
load(path, strict=True, verbose=False, **kwargs)[source]¶ Load a
Modelfrom aPath.- Parameters
path (PathLike) – The path to a PMF model to load.
strict (bool) – Whether to perform strict validation (see
validate()).verbose (bool) – Optional. Default to
False. Toggle verbose offence reporting.**kwargs (Any) – Additional arguments are passed to the
validate()function.
- Returns
A loaded
Modelinstance.- Return type
- Raises
PlumsModelTreeValidationError – If the path provided point to an invalid PMF model.
PlumsModelMetadataValidationError – If the path provided point to a PMF model with an invalid metadata.
See also
The
validate()function for more information on the PMF validation process.
-
plums.model.model.initialisation(path, checkpoint_reference=None, name=None)[source]¶ Construct a valid PMF model initialisation from a
Path.The return type depends on the initialisation type:
If the model was initialised from a PMF model, the function will return a
Modelinstance.If the model was initialised from a single file, the function will return a
Checkpoint.
- Parameters
path (PathLike) – A valid
Pathto the model initialisation.checkpoint_reference (hashable) – If initialising from a PMF model, one needs to provided a reference to the actual model
Checkpointused to construct a valid initialisation.name (str) – If initialising from a file, one needs to provide a name used to identify the initial network to construct a valid initialisation.
- Returns
The retrieved model initialisation.
- Return type
- Raises
OSError – If the path provided does not exists on the filesystem or points to neither a file nor a PMF model.
ValueError – If the arguments provided are incompatibles: e.g. using a PMF initialisation without a checkpoint_reference.
PlumsModelTreeValidationError – If the path provided point to an invalid PMF model.
PlumsModelMetadataValidationError – If the path provided point to a PMF model with an invalid metadata.
Components¶
-
class
plums.model.model.Model(producer_name, producer_version_format, producer_version_string, model_name, model_id, configuration, build_parameters)[source]¶ Bases:
objectDefine a Python representation of a PMF model.
- Parameters
producer_name (str) – The name of the package that produced the model
producer_version_format (str) – The version format of the package that produced the model
producer_version_string (str) – The version string representation of the package that produced the model
model_name (str) – The model name
model_id (str) – The model identification string
configuration (Pathlike) – A path to the producer configuration file used to produce the model
build_parameters (dict) – A dictionary containing values necessary to instantiate the model from its weights.
-
build_parameters¶ A dictionary containing values necessary to instantiate the model from its weights.
- Type
-
checkpoint_collection¶ The
Modelattached collection ofCheckpoint.- Type
-
property
initialisation¶ The model initialisation network if any,
Noneotherwise.- Type
-
property
path¶ If the model was loaded from disk, the
Pathit was loaded from.Noneotherwise.- Type
PathLike
-
property
checkpoint¶ If the model is a
Modelinitialisation, the checkpoint reference used.Noneotherwise.- Type
hashable
-
add_checkpoint(*checkpoints)[source]¶ Add a
Checkpointto theModelcheckpoint_collection.- Parameters
*checkpoints (
Checkpoint) – One ore severalCheckpointto add to the modelCheckpointCollection.
-
register_epoch(epoch=None)[source]¶ Register a given epoch as being the latest epoch to the
trainingmetadata.- Parameters
epoch (int) – Optional. Default to
latest_epoch+ 1. The epoch to be registered as latest.
-
register_training_end(success)[source]¶ Register a training end or a training failure to the
trainingmetadata.
-
register_initialisation(path, checkpoint_reference=None, name=None)[source]¶ Register a valid PMF model initialisation from a
Pathtoinitialisation.- Parameters
path (PathLike) – A valid
Pathto the model initialisation.checkpoint_reference (hashable) – If initialising from a PMF model, one needs to provided a reference to the actual model
Checkpointused to construct a valid initialisation.name (str) – If initialising from a file, one needs to provide a name used to identify the initial network to construct a valid initialisation.
- Raises
OSError – If the path provided does not exists on the filesystem or points to neither a file nor a PMF model.
ValueError – If the arguments provided are incompatibles: e.g. using a PMF initialisation without a checkpoint_reference.
SchemaError – If the path provided point to an invalid PMF model.
See also
The
initialisationfunction for more information on theinitialisationformat.
-
save(path, force=False, **kwargs)[source]¶ Save a
ModeltoPath.- Parameters
- Raises
ValueError – If
pathpoints to a file.OSError – If
pathpoints to: * A non-empty directory which does not contains a PMF model andforceisFalse. * A non-empty directory which contains a PMF model with the sameidandforceisFalse. * A non-empty directory which contains a PMF model with a differentid. * A non-empty directory which contains a PMF model with an invalid metadata file.
-
classmethod
load(path, strict=True, verbose=False, **kwargs)[source]¶ Load a
Modelfrom aPath.- Parameters
path (PathLike) – The path to a PMF model to load.
strict (bool) – Whether to perform strict validation (see
validate()).verbose (bool) – Optional. Default to
False. Toggle verbose offence reporting.**kwargs (Any) – Additional arguments are passed to the
validate()function.
- Returns
A loaded
Modelinstance.- Return type
- Raises
PlumsModelTreeValidationError – If the path provided point to an invalid PMF model.
PlumsModelMetadataValidationError – If the path provided point to a PMF model with an invalid metadata.
See also
The
validate()function for more information on the PMF validation process.
-
plums.model.model.initialisation(path, checkpoint_reference=None, name=None)[source]¶ Construct a valid PMF model initialisation from a
Path.The return type depends on the initialisation type:
If the model was initialised from a PMF model, the function will return a
Modelinstance.If the model was initialised from a single file, the function will return a
Checkpoint.
- Parameters
path (PathLike) – A valid
Pathto the model initialisation.checkpoint_reference (hashable) – If initialising from a PMF model, one needs to provided a reference to the actual model
Checkpointused to construct a valid initialisation.name (str) – If initialising from a file, one needs to provide a name used to identify the initial network to construct a valid initialisation.
- Returns
The retrieved model initialisation.
- Return type
- Raises
OSError – If the path provided does not exists on the filesystem or points to neither a file nor a PMF model.
ValueError – If the arguments provided are incompatibles: e.g. using a PMF initialisation without a checkpoint_reference.
PlumsModelTreeValidationError – If the path provided point to an invalid PMF model.
PlumsModelMetadataValidationError – If the path provided point to a PMF model with an invalid metadata.
-
class
plums.model.components.components.Training(start_time=None, start_epoch=None, latest_time=None, latest_epoch=None, end_time=None, end_epoch=None, status='pending')[source]¶ Bases:
objectDefine a Python representation of a model training state.
- Parameters
start_time (float) – The training stating time, or
Noneif non existentstart_epoch (int) – The training stating epoch, or
Noneif non existentlatest_epoch (int) – The training latest epoch, or
Noneif non existentlatest_time (float) – The training latest epoch time, or
Noneif non existentend_time (float) – The training ending time, or
Noneif non existentend_epoch (int) – The training ending epoch, or
Noneif non existentstatus (str) – The training status, or
'pending'if non existent
-
start(epoch)[source]¶ Start the training and register the starting epoch and timestamp.
- Parameters
epoch (int) – The starting epoch.
-
interrupt()[source]¶ Interrupt the training in a non-standard way and register the failing epoch and timestamp as the latest.
-
end()[source]¶ Interrupt the training in a standard way and register the ending epoch and timestamp as the latest.
-
register_epoch(epoch=None)[source]¶ Register a given epoch as being the latest epoch in the training along with its timestamp.
- Parameters
epoch (int) – Optional. Default to
latest_epoch+ 1. The epoch to be registered as latest.
-
class
plums.model.components.components.CheckpointCollection(*checkpoints)[source]¶ Bases:
objectDefine a checkpoint collection Python representation.
CheckpointCollection may be added through
__setitem__or throughadd().- Parameters
checkpoints (
Checkpoint) – ACheckpointto be added to the collection.
-
property
latest¶ A reference to the latest
Checkpointadded to the collection.- Type
hashable
-
property
eloc¶ Retrieve a
CheckpointCollectionfrom an epoch number.- Type
-
property
iloc¶ Retrieve a
Checkpointfrom its insertion index.- Type
Checkpoint, (Checkpoint, )
-
add(checkpoint)[source]¶ Add a
Checkpointto the collection.- Parameters
checkpoint (
Checkpoint) – ACheckpointto add to the collection.- Raises
KeyError – If the
Checkpointname was already registered as a reference.ValueError – If the
Checkpointepoch isNone.
-
get(reference, default=None)[source]¶ Retrieve a
Checkpointby its unique reference (its name) or adefaultif reference is not found.- Parameters
reference (hashable) – A
Checkpointunique reference.default (Any) – The default to return if reference is not a valid
Checkpointreference.
- Returns
The corresponding
Checkpointordefault.- Return type
Checkpoint, Any
-
keys()[source]¶ Iterate through the collection’s references as in a dict, in insertion order.
- Yields
hashable – A reference of a
Checkpoint.
-
values()[source]¶ Iterate through the collection’s
Checkpointas in a dict, in insertion order.- Yields
Checkpoint– ACheckpoint.
-
items()[source]¶ Iterate through the collection as in a dict, in insertion order.
- Yields
(hashable,
Checkpoint) – Reference andCheckpoint.
-
class
plums.model.components.components.Producer(name, version_format, version_string, configuration)[source]¶ Bases:
objectDefine a Python representation of a PMF model producer with its configuration.
- Parameters
name (str) – The name of the package that produced the model.
version_format (str) – The version format of the package that produced the model.
version_string (str) – The version representation string of the package that produced the model.
configuration (Pathlike) – A path to the producer configuration file used to produce the model.
-
configuration¶ A path to the producer configuration file used to produce the model.
- Type
Pathlike
-
class
plums.model.components.utils.Checkpoint(name, path=None, epoch=None, hash=None)[source]¶ Bases:
objectDefine a checkpoint Python representation.
A
Checkpointmight be defined by the following parameters:Note that although the
epochis never needed to strictly define aCheckpoint, it is compulsory to inject it into aCheckpointCollection.- Parameters
name (hashable) – The
Checkpointunique identifier.path (Pathlike) – Optional. default to
None. The path to theCheckpointdata file.epoch (int) – Optional. default to
None. TheCheckpointepoch, if known.hash (str) – Optional. default to
None. TheCheckpointdata file checksum.
-
name¶ The
Checkpointunique identifier.- Type
hashable
-
path¶ The path to the
Checkpointdata file.- Type
Pathlike
-
epoch¶ The
Checkpointepoch, if known.- Type
-
hash¶ The
Checkpointdata file checksum.- Type
Exceptions¶
-
exception
plums.model.exception.PlumsError[source]¶ Bases:
ExceptionBase exception for all Plums specific errors.
-
exception
plums.model.exception.PlumsValidationError(autos, errors=None)[source]¶ Bases:
schema.SchemaError,plums.model.exception.PlumsErrorBase exception for all Plums schema validation specific errors.
It inherits from
schema.SchemaError.
-
exception
plums.model.exception.PlumsModelError[source]¶ Bases:
plums.model.exception.PlumsErrorBase exception for all Plums Model specific errors.
-
exception
plums.model.exception.PlumsModelTreeValidationError(autos, errors=None)[source]¶ Bases:
plums.model.exception.PlumsValidationError,plums.model.exception.PlumsModelErrorBase exception for all Plums Model validation errors specific to filesystem trees validations.
-
exception
plums.model.exception.PlumsModelMetadataValidationError(autos, errors=None)[source]¶ Bases:
plums.model.exception.PlumsValidationError,plums.model.exception.PlumsModelErrorBase exception for all Plums Model validation errors specific to filesystem trees validations.