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)
- 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:
- 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.
- 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.
- property log_id¶
Return the uniquely assigned id of the run.
Ensures the log_id to be immutable.
- Return type:
- Returns:
The id 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.