Using the SDK to Track Metrics
The SAP Data Intelligence SDK helps you to track your data science experiments and report metrics.
Importing the Tracking Module
To get started with the ML Tracking SDK, import the tracking module using the following command.
from sapdi import tracking
Using the Tracking Functionality
The SDK works with a wrapper, in which you create an instance of MLTrackingSDK. We recommend that you surround your experiment with start_run() and end_run() at the beginning and end respectively. When you use start_run(), you have the option to specify the name of a run collection, which allows you to group different runs of your experiment, and you can specify the name of the run that is being started. To do so, use:
-
run_collection_name='<default>' and pass in a string value. The default is None.
-
run_name='<default>' and pass in a string value. The default is None.
If you don't specify a name for your run collection, the system creates a default one for each source (that is, notebook or pipeline). The name of the default run collection is the same as that of the source. These default run collections are shown in the metrics explorer with the suffix (Default). If you don't specify a name for a run, the system sets the run ID as the name. You can update the association between the run and the run collection, or update the run name at any time using update_run_info.
When you create a run collection, we recommend that you include runs from one source only (that is, from one notebook or one pipeline).
If you want to log metrics after end_run(), log_metric can be called with an associated run ID as an additional parameter but it will be logged with the context that was used to initialize the SDK.
Within your MLTrackingSDK instance, you can call the following functions:
|
Function |
Description |
|---|---|
|
log_parameters |
You call this function to log parameters or configuration variables. It requires the parameters name and value. The type of each parameter must be string (if another type is passed, it will be automatically converted to string). name has a limit of 256 characters and value has a limit of 5000 characters. |
|
log_metric |
You call this function to log metrics. It requires the parameters name and value, whereby value must be numeric. You can log multiple values to the same name by logging multiple metric records (each for one value) and using the same name for each metric. If you want to log metrics after end_run(), you can call the function with the associated run object as an additional parameter. The metrics are logged with the context used to initialize the SDK. name has a limit of 256 characters and is case-sensitive. value must be in the range between -2.2250738585072014E-308 and 1.7976931348623157E308. If more than one value is given to the same parameter name, only the last value is stored. |
|
log_metrics |
You call this function to pass a list of metric items that are to be captured. Each item in the list is a dictionary that has the same parameters as the log_metric function. You can log multiple values to the same name by logging multiple metric records (each for one value) and using the same name for each metric. |
|
persist_run |
You can call this function to explicitly persist the logged metrics or parameters. In the default configuration, the metrics and parameters are persisted at the end of the run. |
|
set_tags |
You call this function to set tags under the current run. Tags are user-defined properties that are associated with a run, and are case-sensitive. Each tag is a key(string)-value(string) pair. Tags can also be used to filter runs. If more than one value is given to the same tag key, only the last value is stored. |
|
set_labels |
You call this function to log labels for the current run. Labels are a set of key-value pairs that are associated with a run. Label keys are case sensitive. Labels can be used to store semantic data or UI formatting data. Runs cannot be filtered by label. If more than one value is given to the same label key, only the last value is stored. |
|
delete_runs |
You call this function to delete any persisted metrics. It requires one or more of the following parameters: scenario, scenario_version, pipeline, execution, notebook, and run. If you specify notebook, you must also specify scenario or run. scenario also enables bulk deletion, in which case the other parameters cannot be specified. To delete multiple scenarios, pass the IDs of each scenario as comma-separated values. For example, scenario='<id>,<id>,<id>'. |
|
get_runs |
You call this function to fetch the run object that contains the metrics, parameters, tags, and so on. It requires one or more of the parameters scenario, scenario_version, pipeline, execution, notebook, and run. The SDK creates an ID for the run and returns it as a run object when start_run() is called. You can use this object to retrieve the metrics and parameters recorded in this run. If you specify notebook, you must also specify scenario or run. Notebook returns the Notebook object. It requires id as an argument, where id represents the notebook ID. For example, tracking.Notebook(id=<NOTEBOOK_ID>). RUN returns the Run object. It requires id as an argument, where id represents the run ID. For example, tracking.Run(id=<RUN_ID>). This method returns the two system-defined parameters and the system-defined tag for each run, along with user-defined entries. |
|
get_metrics_history |
You call this function to fetch a specific metric or metrics, along with the corresponding values for a run. A maximum of 1000 values can be returned for each metric. You call this function to fetch a list of persisted metrics that correspond to all values logged for a given metric. Only 1000 entries are allowed per metric. get_metrics_history expects two parameters: run and metrics (the list of metric names).
|
|
update_run_info |
You call this function to update the run name, run collection name, or tags for a given run ID. It can be called using the arguments run_name, run_collection_name, and tags before the run becomes active or after it has ended. If a run obejct is not specified, the run that is currently active is updated. |
See the Tracking SDK in Action
The video below shows an example of how you can track the metrics for predicting house prices using lasso regression and random forest algorithms.