Experiments — sagemaker 2.199.0 documentation (original) (raw)

sagemaker

Run

class sagemaker.experiments. Run(experiment_name, run_name=None, experiment_display_name=None, run_display_name=None, tags=None, sagemaker_session=None, artifact_bucket=None, artifact_prefix=None)

A collection of parameters, metrics, and artifacts to create a ML model.

Construct a Run instance.

SageMaker Experiments automatically tracks the inputs, parameters, configurations, and results of your iterations as runs. You can assign, group, and organize these runs into experiments. You can also create, compare, and evaluate runs.

The code sample below shows how to initialize a run, log parameters to the Run object and invoke a training job under the context of this Run object, which automatically passes the run’s experiment_config (including the experiment name, run name etc.) to the training job.

Note

All log methods (e.g. log_parameter, log_metric, etc.) have to be called within the run context (i.e. the with statement). Otherwise, a RuntimeError is thrown.

with Run(experiment_name="my-exp", run_name="my-run", ...) as run: run.log_parameter(...) ... estimator.fit(job_name="my-job") # Create a training job

In order to reuse an existing run to log extra data, load_run is recommended. For example, instead of the Run constructor, the load_run is recommended to use in a job script to load the existing run created before the job launch. Otherwise, a new run may be created each time you launch a job.

The code snippet below displays how to load the run initialized above in a custom training job script, where no run_name or experiment_nameis presented as they are automatically retrieved from the experiment config in the job environment.

with load_run(sagemaker_session=sagemaker_session) as run: run.log_metric(...) ...

Parameters

property experiment_config

Get experiment config from run attributes.

log_parameter(name, value)

Record a single parameter value for this run.

Overwrites any previous value recorded for the specified parameter name.

Parameters

log_parameters(parameters)

Record a collection of parameter values for this run.

Parameters

parameters (dict[_str,_ str or int or float]) – The parameters to record.

log_metric(name, value, timestamp=None, step=None)

Record a custom scalar metric value for this run.

Note

This method is for manual custom metrics, for automatic metrics see theenable_sagemaker_metrics parameter on the estimator class.

Parameters

log_precision_recall(y_true, predicted_probabilities, positive_label=None, title=None, is_output=True, no_skill=None)

Create and log a precision recall graph artifact for Studio UI to render.

The artifact is stored in S3 and represented as a lineage artifact with an association with the run.

You can view the artifact in the UI. If your job is created by a pipeline execution you can view the artifact by selecting the corresponding step in the pipelines UI. See also SageMaker Pipelines

This method requires sklearn library.

Parameters

log_roc_curve(y_true, y_score, title=None, is_output=True)

Create and log a receiver operating characteristic (ROC curve) artifact.

The artifact is stored in S3 and represented as a lineage artifact with an association with the run.

You can view the artifact in the UI. If your job is created by a pipeline execution you can view the artifact by selecting the corresponding step in the pipelines UI. See also SageMaker Pipelines

This method requires sklearn library.

Parameters

log_confusion_matrix(y_true, y_pred, title=None, is_output=True)

Create and log a confusion matrix artifact.

The artifact is stored in S3 and represented as a lineage artifact with an association with the run.

You can view the artifact in the UI. If your job is created by a pipeline execution you can view the artifact by selecting the corresponding step in the pipelines UI. See also SageMaker PipelinesThis method requires sklearn library.

Parameters

log_artifact(name, value, media_type=None, is_output=True)

Record a single artifact for this run.

Overwrites any previous value recorded for the specified name.

Parameters

log_file(file_path, name=None, media_type=None, is_output=True)

Upload a file to s3 and store it as an input/output artifact in this run.

Parameters

close()

Persist any data saved locally.

run. load_run(experiment_name=None, sagemaker_session=None, artifact_bucket=None, artifact_prefix=None)

Load an existing run.

In order to reuse an existing run to log extra data, load_run is recommended. It can be used in several ways:

  1. Use load_run by explicitly passing in run_name and experiment_name.

If run_name and experiment_name are passed in, they are honored over the default experiment config in the job environment or the run context (i.e. within the with block).

Note

Both run_name and experiment_name should be supplied to make this usage work. Otherwise, you may get a ValueError.

with load_run(experiment_name="my-exp", run_name="my-run") as run: run.log_metric(...) ...

  1. Use the load_run in a job script without supplying run_name and experiment_name.

In this case, the default experiment config (specified when creating the job) is fetched from the job environment to load the run.

In a job script

with load_run() as run: run.log_metric(...) ...

3. Use the load_run in a notebook within a run context (i.e. the with block) but without supplying run_name and experiment_name.

Every time we call with Run(...) as run1:, the initialized run1 is tracked in the run context. Then when we call load_run() under this with statement, the run1in the context is loaded by default.

In a notebook

with Run(experiment_name="my-exp", run_name="my-run", ...) as run1: run1.log_parameter(...)

with load_run() as run2: # run2 is the same object as run1
    run2.log_metric(...)
    ...

Parameters

Returns

The loaded Run object.

Return type

Run

experiments. list_runs(created_before=None, created_after=None, sagemaker_session=None, max_results=None, next_token=None, sort_by=<SortByType.CREATION_TIME: 'CreationTime'>, sort_order=<SortOrderType.DESCENDING: 'Descending'>)

Return a list of Run objects matching the given criteria.

Parameters

Returns

A list of Run objects.

Return type

list

Experiment

class sagemaker.experiments. Experiment(sagemaker_session=None, **kwargs)

An Amazon SageMaker experiment, which is a collection of related trials.

New experiments are created by calling experiments.experiment.Experiment.create. Existing experiments can be reloaded by calling experiments.experiment.Experiment.load.

experiment_name

The name of the experiment. The name must be unique within an account.

Type

str

display_name

Name of the experiment that will appear in UI, such as SageMaker Studio.

Type

str

description

A description of the experiment.

Type

str

tags

A list of tags to associate with the experiment.

Type

List[Dict[str, str]]

Init Record.

save()

Save the state of this Experiment to SageMaker.

Returns

Update experiment API response.

Return type

dict

delete()

Delete this Experiment from SageMaker.

Deleting an Experiment does not delete associated Trials and their Trial Components. It requires that each Trial in the Experiment is first deleted.

Returns

Delete experiment API response.

Return type

dict

classmethod load(experiment_name, sagemaker_session=None)

Load an existing experiment and return an Experiment object representing it.

Parameters

Returns

A SageMaker Experiment object

Return type

experiments.experiment.Experiment

classmethod create(experiment_name, display_name=None, description=None, tags=None, sagemaker_session=None)

Create a new experiment in SageMaker and return an Experiment object.

Parameters

Returns

A SageMaker Experiment object

Return type

experiments.experiment.Experiment

list_trials(created_before=None, created_after=None, sort_by=None, sort_order=None)

List trials in this experiment matching the specified criteria.

Parameters

Returns

An iterator over trials matching the criteria.

Return type

collections.Iterator[experiments._api_types.TrialSummary]

Other

class sagemaker.experiments. SortByType(value)

The type of property by which to sort the list_runs results.

CREATION_TIME = 'CreationTime'

NAME = 'Name'

class sagemaker.experiments. SortOrderType(value)

The type of order to sort the list or search results.

ASCENDING = 'Ascending'

DESCENDING = 'Descending'