Search code examples
pythonazureazure-sdkazure-sdk-python

How to store multiple log parameters using AzureML SDK for Python?


I use AzureML SDK for Python to define a Run and assign log parameters as shown below.

run = Run.get_context()

run.parent.log("param1", 25)
run.parent.log("param2", 100)
run.parent.log("param3", 10)
run.parent.log("param4", 40)

The problem is that I can only see param1 and param2 in Machine Learning Service Workspace. Is there any limitation on the number of variables?


Solution

  • The short answer is NO after I reviewed the source code of azureml-core which I got via decompress the azureml_core-1.0.85-py2.py3-none-any.whl file.

    The key source codes are here.

    # azureml_core-1.0.85-py2.py3-none-any.whl\azureml\core\run.py
    class Run(_RunBase):
        .......
    
        @classmethod
        def get_context(cls, allow_offline=True, used_for_context_manager=False, **kwargs):
            """Return current service context.
    
            Use this method to retrieve the current service context for logging metrics and uploading files. If
            ``allow_offline`` is True (the default), actions against the Run object will be printed to standard
            out.
    
            .. remarks::
    
                This function is commonly used to retrieve the authenticated Run object
                inside of a script to be submitted for execution via experiment.submit(). This run object is both
                an authenticated context to communicate with Azure Machine Learning services and a conceptual container
                within which metrics, files (artifacts), and models are contained.
    
                .. code-block:: python
    
                    run = Run.get_context() # allow_offline=True by default, so can be run locally as well
                    ...
                    run.log("Accuracy", 0.98)
                    run.log_row("Performance", epoch=e, error=err)
    
            :param cls: Indicates class method.
            :param allow_offline: Allow the service context to fall back to offline mode so that the training script
                can be tested locally without submitting a job with the SDK. True by default.
            :type allow_offline: bool
            :param kwargs: A dictionary of additional parameters.
            :type kwargs: dict
            :return: The submitted run.
            :rtype: azureml.core.run.Run
            """
            try:
                experiment, run_id = cls._load_scope()
    
                # Querying for the run instead of initializing to load current state
                if used_for_context_manager:
                    return _SubmittedRun(experiment, run_id, **kwargs)
                return _SubmittedRun._get_instance(experiment, run_id, **kwargs)
            except RunEnvironmentException as ex:
                module_logger.debug("Could not load run context %s, switching offline: %s", ex, allow_offline)
                if allow_offline:
                    module_logger.info("Could not load the run context. Logging offline")
                    return _OfflineRun(**kwargs)
                else:
                    module_logger.debug("Could not load the run context and allow_offline set to False")
                    raise RunEnvironmentException(inner_exception=ex)
    
    class _OfflineRun(ChainedIdentity):
        def __init__(self, parent_logger=None, run_id=None, **kwargs):
            self._run_id = "OfflineRun_{}".format(uuid4()) if run_id is None else run_id
            super(_OfflineRun, self).__init__(
                _ident=self._run_id,
                _parent_logger=parent_logger if parent_logger is not None else module_logger)
    
        ....
    
        def log(self, name, value, description=""):
            self._emit("scalar", name, value)
    
        ....
    
        def _emit(self, type, name, value):
            print("Attempted to log {0} metric {1}:\n{2}".format(type, name, value))
    

    The run object got from get_context function is running on Offline mode, so its log function just be an alias of print.