Logger

The Logger Class

The logger can saves the configs and outputs of an experiment.

class mlxp.logger.Logger(parent_log_dir, forced_log_id=-1, log_streams_to_file=False)[source]

Bases: ABC

A logger that allows saving outputs of the run in a uniquely assigned directory for the specific run.

The logger creates a directory with a default file structure:

parent_log_dir/log_id:
├── metadata/
│   └── metadata.yaml : Contains the configs of the run
├── metrics/
│   ├── 'file_name'.json : Contains a the outputs stored
│   │                   when running the method log_metrics(metrics_dict, file_name)
│   └── .keys/ Directory of yaml files containing the keys of dictionaries saved using log_metrics.
│            Each file 'file_name'.yaml corresponds to a json file 'file_name'.json containing the dictionaries.
├── artifacts/ : A directory where each subdirectory contains objects of the same subclass of Artifact saved using the method log_artifacts.
├── log.stderr: Contains error logs (Only if job is submitted in bacth mode to a scheduler)
├── log.stdout: Contains output logs (Only if job is submitted in bacth mode to a scheduler)
└── script.sh: Contains the script for running the job (Only if job is submitted using a job scheduler)
parent_log_dir: str

The parent directory where the directory of the run is created.

get_info() None[source]

Return a dictionary containing information about the logger settings used for the run.

Returns:

Dictionary containing information about the logger settings used for the run.

Return type:

Dict[str, Any]

log_metrics(metrics_dict, log_name)[source]

Save a dictionary of scalars to a json file named log_name+’.json’ in the directory log_dir/metrics.

If the file exists already, the dictionary is appended at the end of the file.

Parameters:
  • metrics_dict (Dict[str, Union[int, float, str]]) – Dictonary of scalar values to be saved, the values can be either int, float of string.

  • log_name (str) – Name of the json file where to save the metric_dict.

Returns:

None

Raises:

InvalidKeyError – if one of the keys in the metrics_dict is protected: the key must be different from “info”, “config”, “mlxp” and “artifacts”.

log_artifacts(artifact: object, artifact_name: str, artifact_type: str) None[source]

Save an artifact object into a destination file: ‘log_dir/artifacts/artifact_name’,

The directory ‘artifact_class_name’ is named after the child class inheriting from Artifact.

Parameters:
  • artifact (object) – An object to save.

  • artifact_name (str) – Name of the file where the artifact is saved.

  • artifact_type (str) – Type of the artifact to save.

Returns:

None

Raises:

InvalidArtifactError – if artifact type does not correspond to any existing type.

load_artifacts(artifact_name: str, artifact_type: str | None = None, root: str | None = None) Any[source]

Restore an artifact from ‘log_dir/artifacts/artifact_type/artifact_name’ or a user defined directory root.

Raises an error if it fails to do so.

Parameters:
  • artifact_name (str (default 'checkpoint')) – Name of the file where the checkpoint is saved.

  • root (Union[str,None] (default 'None')) – Absolute path to the log directory (log_dir) (assuming it was created using a logger object). If set to None, then the logger in its own log_dir.

  • artifact_type (Union[str,None] (default 'None')) – Type of the artifact to save. If set to None, the method will try to infer the artifact type from the artifact_name.

return: Any serializable object stored in ‘root/artifacts/artifact_type/artifact_name’. rtype: Any

register_artifact_type(name: str, save: Callable[[object, str], None], load: Callable[[str], object]) None[source]

Register a new artifact type with a save and load method.

Use this method when you want to save and load a custom artifact type. This method should be called before using log_artifacts with the new artifact type.

Parameters:
  • name (str) – Name of the artifact type.

  • save (Callable[[object, str], None]) – User-defined function to save the artifact.

  • load (Callable[[str], object]) – User-defined function to load the artifact.

Returns:

None

property log_id

Return the uniquely assigned id of the run.

Ensures the log_id to be immutable.

Return type:

int

Returns:

The id of the run.

property log_dir

Return the path to the directory where outputs of the run are saved.

Ensures the log_dir to be immutable.

Return type:

str

Returns:

The path to the output directory of the run.

class mlxp.logger.DefaultLogger(parent_log_dir, forced_log_id, log_streams_to_file=False)[source]

Bases: Logger

A logger that provides methods for logging checkpoints and loading them.

log_checkpoint(checkpoint: Any, log_name: str = 'checkpoint') None[source]

Save a checkpoint for later use, this can be any serializable object.

This method is intended for saving the latest state of the run, thus, by default, the checkpoint name is set to ‘last.pkl’. For custom checkpointing please use the method log_artifacts

Parameters:
  • checkpoint (Any) – Any serializable object to be stored in ‘run_dir/artifacts/pickle/last.pkl’.

  • log_name (str (default 'checkpoint')) – Name of the file where the checkpoint is saved.

load_checkpoint(log_name, root=None) Any[source]

Restore a checkpoint from ‘run_dir/artifacts/pickle/log_name.pkl’ or a user defined directory root.

Raises an error if it fails to do so.

Parameters:
  • log_name (str (default 'checkpoint')) – Name of the file where the checkpoint is saved.

  • root (Union[str,None] (default 'None')) – Absolute path to the checkpoint. If set to None, the logger looks for the checkpoint in ‘run_dir/artifacts/pickle’.

return: Any serializable object stored in ‘run_dir/artifacts/pickle/last.pkl’. rtype: Any