MLflow is library-agnostic. You can use it with any machine learning library, and in any programming language, since all functions are accessible through a REST API and CLI.
The code and examples shown here are exclusively in python.
The MLflow Tracking component is an API and UI for logging parameters, code versions, metrics, and output files when running your machine learning code and for later visualizing the results. MLflow Tracking lets you log and query experiments using Python, REST, R API, and Java API APIs. The MLflow Python API logs runs locally to files in an mlruns directory wherever you ran your program. You can then run mlflow ui to see the logged runs.
mlflow ui
Following are the standard logging functions which are used.
mlflow.start_run() #returns the currently active run (if one exists), or starts a new run.
You do not need to call start_run explicitly: calling one of the logging functions with no active run automatically starts a new one.
mlflow.end_run() #ends the currently active run.
if any, taking an optional run status.
mlflow.log_param() #logs a single key-value param in the currently active run.
The key and value are both strings. Use mlflow.log_params() to log multiple params at once.
mlflow.log_metric() #logs a single key-value metric.
The value must always be a number. MLflow remembers the history of values for each metric. Use mlflow.log_metrics() to log multiple metrics at once.
mlflow.log_artifact() #logs a local file or directory as an artifact, optionally taking an artifact_path to place it in within the run's artifact URI.
Run artifacts can be organized into directories, so you can place the artifact in a directory this way.
An MLflow Project is a format for packaging data science code in a reusable and reproducible way, based primarily on conventions. In addition, the Projects component includes an API and command-line tools for running projects, making it possible to chain together projects into workflows. Each project is simply a directory of files, or a Git repository, containing your code.
It mainly contains two .yaml files named:
A conda.yaml file looks like this
name: name
channels:
- defaults
dependencies:
- python = 3.6
- scikit-learn
- pip:
- mlflow>=1.0
Where,
And MLproject looks like this
name: tutorial
conda_env: conda.yaml
entry_points:
main:
parameters:
alpha: {type: float, default: 0.5}
l1_ratio: {type: float, default: 0.1}
command: "python train.py {alpha} {l1_ratio}"
(Note: Not all Python packages are available as Conda packages. Some might only available through PyPI, or may be released there first. By including pip in the dependencies, that Python-specific package manager will be included. Listing packages below pip in the hierarchy, indicates that pip should be used to install those packages.)
An MLflow Model is a standard format for packaging machine learning models that can be used in a variety of downstream tools—for example, real-time serving through a REST API or batch inference on Apache Spark. (Like a python pickle module!)
MLflow includes integrations with several common libraries. For example,
This dataset surveys four areas of the Roosevelt National Forest in Colorado and includes information on tree type, shadow coverage, distance to nearby landmarks (roads etcetera), soil type, and local topography. The forest cover type is the classification problem.
TensorFlow allows developers to create dataflow graphs(for language and hardware portability) where each node represents a mathematical operation, and each edge between nodes is a 3 or higher n-dimensional data array, or tensor. These are directed, acyclic graphs (DAG). Useful APIs include:
Python APIs - used to build the DAG.
Simply put, build a DAG (a.k.a the model), create a session to run the model, feed model values using feed-dict(in tf.run()), run the model in the session. Variables are trainable in TF(eg. Weight vector for neural network). But in order to have formal parameters initialised at runtime, placeholders are used that can be initialised by feed-dict. train_and_evaluate function can be used for data parallelism.
Use cases of Tensorflow include: Voice/Sound/Image Recognition, Text based applications, Time Series, Video Detection.
A random forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset and uses averaging to improve the predictive accuracy and control over-fitting. We use the Random Forest classifier from Scikit-learn on the Forest Cover Type Dataset.
Both the programs are configured to run any classification dataset.
This program can be ran by manually tuning the following Parameters
Input the following parameters in a following way
python file.py mini_batch_size no_of_epochs total_data_size test_size(in format %total_data_size)
eg. python mlflow_tensor.py 10 1000 2000 0.4
Running on default params right now
Code Snippet'''For logging tensorflow model'''
with mlflow.start_run():
mlflow.log_artifact(sa_fig)
for value in range(len(tot_cost)):
mlflow.log_metric('loss',tot_cost[value])
mlflow.log_metric("Accuracy",fin_acc)
mlflow.log_param("Input Features",ip_features)
mlflow.log_param("Output Labels", op_features)
mlflow.log_param("Training_Size",len(training_set))
mlflow.log_param("Test_Size",len(test_set))
mlflow.log_param("Learning_rate",lr)
mlflow.end_run()
zsh
mlflow run projectname parameters(if any) --no-conda