我经常使用Google Colab来训练TF/PyTorch模型,因为Colab为我提供了GPU/TPU运行时。此外,我喜欢使用MLflow来存储和比较经过训练的模型,跟踪进度,共享等。在Google Colab中使用MLflow有哪些可用的解决方案?
发布于 2020-09-09 20:59:35
这上面有一个Github issue,尽管在写这篇文章的时候它仍然是开放编辑的:刚刚关闭,贡献者dmatrix利用pyngrok为notebook提供了一个完整的解决方案。
下面是代码(打算在Colab notebook上运行),此处使用隐式permission of the author重发
!pip install mlflow --quiet
!pip install pyngrok --quiet
import mlflow
with mlflow.start_run(run_name="MLflow on Colab"):
mlflow.log_metric("m1", 2.0)
mlflow.log_param("p1", "mlflow-colab")
# run tracking UI in the background
get_ipython().system_raw("mlflow ui --port 5000 &") # run tracking UI in the background
# create remote tunnel using ngrok.com to allow local port access
# borrowed from https://colab.research.google.com/github/alfozan/MLflow-GBRT-demo/blob/master/MLflow-GBRT-demo.ipynb#scrollTo=4h3bKHMYUIG6
from pyngrok import ngrok
# Terminate open tunnels if exist
ngrok.kill()
# Setting the authtoken (optional)
# Get your authtoken from https://dashboard.ngrok.com/auth
NGROK_AUTH_TOKEN = ""
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)它的输出将是一个pyngrok-generated URL,如下所示:
MLflow Tracking UI: https://0a23d7a7d0c4.ngrok.io单击该选项将转到MLfLow图形用户界面屏幕。
(由于pyngrok创建者Alex Laird的帮助,对原始代码进行了轻微的修改)
使用MLflow版本1.10.0和1.11.0进行了测试。
发布于 2021-06-16 16:34:42
您可以使用databricks-community提供的免费MLflow跟踪服务器,并将其与Google Colab一起使用。下面的gif详细介绍了如何在databricks上设置MLflow跟踪服务器:

现在,关于从Google Colab访问上面的设置MLflow。只需遵循下面的代码片段:
代码片段#1
!pip install mlflow
!databricks configure --host https://community.cloud.databricks.com/运行上面的代码片段后,它会提示您输入刚刚创建的databricks帐户的用户名和密码。请这样做。
代码片段#2
import mlflow
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment("<Enter your copied experiment name here>")如果你遵循上面附加的gif文件,我会在它的末尾复制experiment_name。请执行相同的操作并将您的experiment_name传递给set_experiment()函数。
通过执行上述步骤,您可以确保在Google Colab!上配置了MLflow!
顺便说一句,我在上面写了一个中级故事,请务必查看:Intro to MLflow — With Colab — Part 1/2
发布于 2021-04-13 21:53:57
这里给出了一些很好的答案,它们也有一些缺点,您需要设置自己的- mainly服务器才能使其正常工作。
TL;DR:
我总结一下,你有两个选择:
选项1: Do everything yourself
对于这个选项,我从desertnaut的答案中提取了一些代码(归功于dmatrix)。
pip install mlflow --quiet
mlflow ui --port 5000或者在笔记本中运行时:
!pip install mlflow --quiet
get_ipython().system_raw("mlflow ui --port 5000 &")这将初始化MLflow服务器。在Colab中这样做的缺点是,您的运行时是短暂的,这意味着当您关闭会话时,所有的实验信息都将丢失。您可以在本地运行该命令,但是使用ngrok进行隧道传输可能会更加复杂。
!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)在这里,我修改了代码以使用getpass,因为不推荐使用明文访问令牌。
import mlflow
with mlflow.start_run(run_name="MLflow on Colab"):
mlflow.log_metric("m1", 2.0)
mlflow.log_param("p1", "mlflow-colab")这也已经在当前版本的MLflow - 1.15.0上进行了测试
选项2:使用托管服务器
此选项可保存ngrok的设置和隧道。它还提供了团队访问控制和改进的UI的好处。据我所知,有两个主要的选项:Databricks和DAGsHub。
Databricks是这方面的托管企业解决方案,而DAGsHub是免费的社区选项。
在使用DAGsHub的情况下,您可以跳过步骤1,步骤2将变得简单得多。上面的代码片段如下所示(在相关平台上创建帐户和项目后):
!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")如您所见,这大大缩短了时间。它也有持久化的好处。
https://stackoverflow.com/questions/61615818
复制相似问题