There are some good answers given, which have some downsides – mainly that you need to set up your own MLflow server for it to work.
TL;DR:
I would summarize by saying you have 2 options:
Option 1: Do everything yourself
For this option, I'm taking some of the code from desertnaut's answer (credit to dmatrix). Basically, if we use ngrok, we can divide the process into 3 steps:
- Setting up an MLflow server:
Either locally, on Colab, or somewhere else.
pip install mlflow --quiet
mlflow ui --port 5000
or when running in a notebook:
!pip install mlflow --quiet
get_ipython().system_raw("mlflow ui --port 5000 &")
This will initialize an MLflow server. The downside of doing this in Colab, is that your runtime there is ephemeral, which means that when you close your session, all the experiment information will be lost. You can run the command locally, but then tunneling in with ngrok might be more complex.
- Make it accessible for Colab and optionally add authentication
This can be done with ngrok. The code is as following:
!pip install pyngrok --quiet
from pyngrok import ngrok
from getpass import getpass
# Terminate open tunnels if exist
ngrok.kill()
# Setting the authtoken (optional)
# Get your authtoken from https://dashboard.ngrok.com/auth
NGROK_AUTH_TOKEN = getpass('Enter the ngrok authtoken: ')
ngrok.set_auth_token(NGROK_AUTH_TOKEN)
# Open an HTTPs tunnel on port 5000 for http://localhost:5000
ngrok_tunnel = ngrok.connect(addr="5000", proto="http", bind_tls=True)
print("MLflow Tracking UI:", ngrok_tunnel.public_url)
Here I modified the code to use getpass since plaintext access tokens are not recommended.
- Log the experiment details
Finally, I'm assuming you already have the code to log with MLflow, but the example above is a simple showcase of how to create an experiment:
import mlflow
with mlflow.start_run(run_name="MLflow on Colab"):
mlflow.log_metric("m1", 2.0)
mlflow.log_param("p1", "mlflow-colab")
This has also been tested with the current version of MLflow – 1.15.0
Option 2: Use a hosted server
This option saves the setup of ngrok and the tunneling. It also provides the benefits of team access controls and an improved UI. There are 2 main options for this that I'm aware of: Databricks, and DAGsHub.
Databricks is the hosted enterprise solution for this, while DAGsHub is the free community option.
In the case where you use DAGsHub, you skip step 1, and step 2 becomes much simpler. The snippet above becomes the following (after creating an account and a project on the relevant platform):
!pip install mlflow --quiet
import mlflow
import os
from getpass import getpass
os.environ['MLFLOW_TRACKING_USERNAME'] = input('Enter your DAGsHub username: ')
os.environ['MLFLOW_TRACKING_PASSWORD'] = getpass('Enter your DAGsHub access token: ')
os.environ['MLFLOW_TRACKING_PROJECTNAME'] = input('Enter your DAGsHub project name: ')
mlflow.set_tracking_uri(f'https://dagshub.com/' + os.environ['MLFLOW_TRACKING_USERNAME'] + '/' + os.environ['MLFLOW_TRACKING_PROJECTNAME'] + '.mlflow')
with mlflow.start_run(run_name="MLflow on Colab"):
mlflow.log_metric("m1", 2.0)
mlflow.log_param("p1", "mlflow-colab")
As you can see, this is significantly shorter. It also has the benefit of being persistent.