Python mlflow.log_param() Examples

The following are 17 code examples of mlflow.log_param(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mlflow , or try the search function .
Example #1
Source File: tensorflow.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def on_train_begin(self, logs=None):  # pylint: disable=unused-argument
        config = self.model.optimizer.get_config()
        for attribute in config:
            try_mlflow_log(mlflow.log_param, "opt_" + attribute, config[attribute])

        sum_list = []
        self.model.summary(print_fn=sum_list.append)
        summary = '\n'.join(sum_list)
        tempdir = tempfile.mkdtemp()
        try:
            summary_file = os.path.join(tempdir, "model_summary.txt")
            with open(summary_file, 'w') as f:
                f.write(summary)
            try_mlflow_log(mlflow.log_artifact, local_path=summary_file)
        finally:
            shutil.rmtree(tempdir) 
Example #2
Source File: experiment.py    From nyaggle with MIT License 6 votes vote down vote up
def log_param(self, key, value):
        """
        Logs a key-value pair for the experiment.

        Args:
            key: parameter name
            value: parameter value
        """
        key = _sanitize(key)
        value = _sanitize(value)
        self.params[key] = value

        if self.with_mlflow:
            import mlflow
            from mlflow.exceptions import MlflowException

            key_mlflow = _sanitize_mlflow_param(key, MLFLOW_KEY_LENGTH_LIMIT)
            value_mlflow = _sanitize_mlflow_param(value, MLFLOW_VALUE_LENGTH_LIMIT)

            try:
                mlflow.log_param(key_mlflow, value_mlflow)
            except MlflowException as e:
                warnings.warn('Error in logging parameter {} to mlflow. Skipped. {}'.format(key, e)) 
Example #3
Source File: test_run.py    From nyaggle with MIT License 6 votes vote down vote up
def test_inherit_outer_scope_run(tmpdir_name):
    mlflow.start_run()
    mlflow.log_param('foo', 1)

    params = {
        'objective': 'binary',
        'max_depth': 8
    }
    X, y = make_classification_df()

    run_experiment(params, X, y, with_mlflow=True, logging_directory=tmpdir_name)

    assert mlflow.active_run() is not None  # still valid

    client = mlflow.tracking.MlflowClient()
    data = client.get_run(mlflow.active_run().info.run_id).data

    assert data.metrics['Overall'] > 0  # recorded

    mlflow.end_run() 
Example #4
Source File: test_run.py    From nyaggle with MIT License 6 votes vote down vote up
def test_ignore_errors_in_mlflow_params(tmpdir_name):
    mlflow.start_run()
    mlflow.log_param('features', 'ABC')
    mlflow.log_metric('Overall', -99)

    params = {
        'objective': 'binary',
        'max_depth': 8
    }
    X, y = make_classification_df()

    result = run_experiment(params, X, y, with_mlflow=True, logging_directory=tmpdir_name, feature_list=[])

    client = mlflow.tracking.MlflowClient()
    data = client.get_run(mlflow.active_run().info.run_id).data

    assert data.metrics['Overall'] == result.metrics[-1]
    assert data.params['features'] == 'ABC'  # params cannot be overwritten

    mlflow.end_run() 
Example #5
Source File: generate_ui_test_data.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def log_params(parameters):
    for k, v in parameters.items():
        mlflow.log_param(k, v) 
Example #6
Source File: mlflow.py    From tf-yarn with Apache License 2.0 5 votes vote down vote up
def log_param(key: str, value: Any):
    mlflow.log_param(key, value) 
Example #7
Source File: random_forest.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def train_random_forest(ntrees):
    with mlflow.start_run():
        rf = H2ORandomForestEstimator(ntrees=ntrees)
        train_cols = [n for n in wine.col_names if n != "quality"]
        rf.train(train_cols, "quality", training_frame=train, validation_frame=test)

        mlflow.log_param("ntrees", ntrees)

        mlflow.log_metric("rmse", rf.rmse())
        mlflow.log_metric("r2", rf.r2())
        mlflow.log_metric("mae", rf.mae())

        mlflow.h2o.log_model(rf, "model") 
Example #8
Source File: tensorflow.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def on_train_begin(self, logs=None):  # pylint: disable=unused-argument
        opt = self.model.optimizer
        if hasattr(opt, '_name'):
            try_mlflow_log(mlflow.log_param, 'optimizer_name', opt._name)
        # Elif checks are if the optimizer is a TensorFlow optimizer rather than a Keras one.
        elif hasattr(opt, 'optimizer'):
            # TensorFlow optimizer parameters are associated with the inner optimizer variable.
            # Therefore, we assign opt to be opt.optimizer for logging parameters.
            opt = opt.optimizer
            try_mlflow_log(mlflow.log_param, 'optimizer_name', type(opt).__name__)
        if hasattr(opt, 'lr'):
            lr = opt.lr if type(opt.lr) is float else tensorflow.keras.backend.eval(opt.lr)
            try_mlflow_log(mlflow.log_param, 'learning_rate', lr)
        elif hasattr(opt, '_lr'):
            lr = opt._lr if type(opt._lr) is float else tensorflow.keras.backend.eval(opt._lr)
            try_mlflow_log(mlflow.log_param, 'learning_rate', lr)
        if hasattr(opt, 'epsilon'):
            epsilon = opt.epsilon if type(opt.epsilon) is float \
                else tensorflow.keras.backend.eval(opt.epsilon)
            try_mlflow_log(mlflow.log_param, 'epsilon', epsilon)
        elif hasattr(opt, '_epsilon'):
            epsilon = opt._epsilon if type(opt._epsilon) is float \
                else tensorflow.keras.backend.eval(opt._epsilon)
            try_mlflow_log(mlflow.log_param, 'epsilon', epsilon)

        sum_list = []
        self.model.summary(print_fn=sum_list.append)
        summary = '\n'.join(sum_list)
        tempdir = tempfile.mkdtemp()
        try:
            summary_file = os.path.join(tempdir, "model_summary.txt")
            with open(summary_file, 'w') as f:
                f.write(summary)
            try_mlflow_log(mlflow.log_artifact, local_path=summary_file)
        finally:
            shutil.rmtree(tempdir) 
Example #9
Source File: autologging_utils.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def log_fn_args_as_params(fn, args, kwargs, unlogged=[]):  # pylint: disable=W0102
    """
    Log parameters explicitly passed to a function.
    :param fn: function whose parameters are to be logged
    :param args: arguments explicitly passed into fn
    :param kwargs: kwargs explicitly passed into fn
    :param unlogged: parameters not to be logged
    :return: None
    """
    # all_default_values has length n, corresponding to values of the
    # last n elements in all_param_names
    pos_params, _, _, pos_defaults, kw_params, kw_defaults, _ = inspect.getfullargspec(fn)

    kw_params = list(kw_params) if kw_params else []
    pos_defaults = list(pos_defaults) if pos_defaults else []
    all_param_names = pos_params + kw_params
    all_default_values = pos_defaults + [kw_defaults[param] for param in kw_params]

    # Checking if default values are present for logging. Known bug that getargspec will return an
    # empty argspec for certain functions, despite the functions having an argspec.
    if all_default_values is not None and len(all_default_values) > 0:
        # Logging the default arguments not passed by the user
        defaults = get_unspecified_default_args(args, kwargs, all_param_names, all_default_values)

        for name in [name for name in defaults.keys() if name in unlogged]:
            del defaults[name]
        try_mlflow_log(mlflow.log_params, defaults)

    # Logging the arguments passed by the user
    args_dict = dict((param_name, param_val) for param_name, param_val
                     in zip(all_param_names, args)
                     if param_name not in unlogged)

    if args_dict:
        try_mlflow_log(mlflow.log_params, args_dict)

    # Logging the kwargs passed by the user
    for param_name in kwargs:
        if param_name not in unlogged:
            try_mlflow_log(mlflow.log_param, param_name, kwargs[param_name]) 
Example #10
Source File: experiment.py    From LaSO with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def start(self):
        """Start the whole thing"""

        self._setup_logging()

        if self.generate_config:
            self.write_config()

        #
        # Setup mlflow
        #
        import mlflow
        mlflow.set_tracking_uri(self.mlflow_server)
        experiment_id = mlflow.set_experiment(self.name)

        #
        # Run the script under mlflow
        #
        with mlflow.start_run(experiment_id=experiment_id):
            #
            # Log the run parametres to mlflow.
            #
            mlflow.log_param("results_path", self.results_path)

            cls = self.__class__
            for k, trait in sorted(cls.class_own_traits(config=True).items()):
                mlflow.log_param(trait.name, repr(trait.get(self)))

            self.run() 
Example #11
Source File: experiment.py    From nyaggle with MIT License 5 votes vote down vote up
def log_dict(self, name: str, value: Dict, separator: str = '.'):
        """
        Logs a dictionary as parameter with flatten format.

        Args:
            name: Parameter name
            value: Parameter value
            separator: Separating character used to concatanate keys
        Examples:
            >>> with Experiment('./') as e:
            >>>     e.log_dict('a', {'b': 1, 'c': 'd'})
            >>>     print(e.params)
            { 'a.b': 1, 'a.c': 'd' }
        """

        if value is None:
            self.log_param(name, value)
            return

        def _flatten(d: Dict, prefix: str, separator: str) -> Dict:
            items = []
            for k, v in d.items():
                child_key = prefix + separator + str(k) if prefix else str(k)
                if isinstance(v, Dict) and v:
                    items.extend(_flatten(v, child_key, separator).items())
                else:
                    items.append((child_key, v))
            return dict(items)

        value = _flatten(value, name, separator)
        self.log_params(value) 
Example #12
Source File: experiment.py    From nyaggle with MIT License 5 votes vote down vote up
def log_params(self, params: Dict):
        """
        Logs a batch of params for the experiments.

        Args:
            params: dictionary of parameters
        """
        for k, v in params.items():
            self.log_param(k, v) 
Example #13
Source File: loggers.py    From OpenKiwi with GNU Affero General Public License v3.0 5 votes vote down vote up
def log_param(key, value):
        mlflow.log_param(key, value) 
Example #14
Source File: loggers.py    From OpenKiwi with GNU Affero General Public License v3.0 5 votes vote down vote up
def log_param(key, value):
        pass 
Example #15
Source File: mlflow_utils.py    From nucleus7 with Mozilla Public License 2.0 5 votes vote down vote up
def log_config_parameter_to_mlflow(param_name: str, param_value: Any):
    """
    Log the parameter with its value to mlflow.

    If value is malformed, it will be ignored and logging notification will
    be added

    Parameters
    ----------
    param_name
        parameter name to log
    param_value
        value to log
    """
    logger = logging.getLogger(__name__)
    param_name = _replace_not_allowed_symbols(param_name)
    if isinstance(param_value, str) and os.path.exists(param_value):
        param_value = os.path.realpath(param_value)
    if mlflow.active_run() is None:
        _warn_about_no_run()
        return
    try:
        mlflow.log_param(param_name, param_value)
    # pylint: disable=invalid-name
    # is common practice to call exceptions as e
    # pylint: disable=broad-except
    # this is due to the mlflow itself, they raise this Exception
    except Exception as e:
        logger.warning("Parameter %s has malformed value %s and so will not "
                       "be logged to mlflow!", param_name, param_value)
        logger.warning("Original exception: %s", e) 
Example #16
Source File: evaluate.py    From orbyter-cookiecutter with MIT License 4 votes vote down vote up
def log_experiment(
    params={},
    metrics={},
    artifacts={},
    experiment_name="my_experiment",
    mlflow_tracking_uri="./experiments",
    mlflow_artifact_location=None,
):
    """
    Evaluate the model and log it with mlflow

    Args:
        params (dict): dictionary of parameters to log
        metrics (dict): dictionary of metrics to log
        artifacts (dict): dictionary of artifacts (path) to log
        experiment_name (str): experiment name
        mlflow_tracking_uri (str): path or sql url for mlflow logging
        mlflow_artifact_location (str): path or s3bucket url for artifact
            logging. If none, it will default to a standard.

    Returns:
        None
    """
    # Try to create an experiment if it doesn't exist
    try:
        exp_0 = mlflow.create_experiment(
            experiment_name, artifact_location=mlflow_artifact_location
        )
        # set uri
        mlflow.set_tracking_uri(mlflow_tracking_uri)
        logger.info(f"Created new experiment id: {exp_0}")
    except Exception as E:
        logger.info(f"{E}. Writing to same URI/artifact store")
    # Always set the experiment
    mlflow.set_experiment(experiment_name)
    logger.info(f"Running experiment {experiment_name}")
    with mlflow.start_run():
        # param logging
        for key, val in params.items():
            logger.info(f"Logging param {key}")
            mlflow.log_param(key, val)
        # metric logging
        for key, val in metrics.items():
            logger.info(f"Logging metric {key}")
            mlflow.log_metric(key, val)
        # artifact logging
        for key, val in artifacts.items():
            logger.info(f"Logging artifact {key}")
            mlflow.log_artifact(val) 
Example #17
Source File: yolo_image.py    From ai-platform with MIT License 4 votes vote down vote up
def process_image(keras_model_path, size,dataset = "coco", photo_name = "data/horses.jpg"):
    dataset = "data/{}.data".format(dataset)
    if not os.path.exists("outputs"): os.mkdir("outputs")
    
    warnings.filterwarnings("ignore")
    np.random.seed(40)
    model = load_model(keras_model_path)
    # define the expected input shape for the model
    input_w, input_h = size, size
    output_photo_name = 'outputs/objects_' + os.path.basename(photo_name)
    # load and prepare image
    image, image_w, image_h = load_image_pixels(photo_name, (input_w, input_h))
    # make prediction
    yhat = model.predict(image)
    # summarize the shape of the list of arrays
    print("Bounding box coordinates")
    print([a.shape for a in yhat])
    # define the anchors
    anchors = [[116,90, 156,198, 373,326], [30,61, 62,45, 59,119], [10,13, 16,30, 33,23]]
    # define the probability threshold for detected objects
    class_threshold = 0.5
    boxes = list()
    for i in range(len(yhat)):
        # decode the output of the network
        boxes += decode_netout(yhat[i][0], anchors[i], class_threshold, input_h, input_w)
    # correct the sizes of the bounding boxes for the shape of the image
    correct_yolo_boxes(boxes, image_h, image_w, input_h, input_w)
    # suppress non-maximal boxes
    do_nms(boxes, 0.4)
    # define the labels
    _,labels = dataset_process(dataset) 
    with mlflow.start_run(nested = True):
        mlflow.log_param("keras_model_path", keras_model_path)
        mlflow.log_param("photo_name", photo_name)
    
        # get the details of the detected objects
        v_boxes, v_labels, v_scores = get_boxes(boxes, labels, class_threshold)
        # summarize what we found
        for i in range(len(v_boxes)):
            print(v_labels[i], v_scores[i])
            mlflow.set_tag(str(i) + '_' + v_labels[i], v_scores[i])
        # draw what we found
        draw_boxes(photo_name, v_boxes, v_labels, v_scores, output_photo_name)
        print("Image created after object detection task and saved as:", output_photo_name)
    
        mlflow.log_artifact(output_photo_name, "output_photo_name")