- Notifications
You must be signed in to change notification settings - Fork12
Tutorial for a new versioning Machine Learning pipeline
License
peopledoc/mlvtools-tutorial
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The aim of this repository is to show a way to handlepipelining andversioningof aMachine Learning project.
Processes exposed during this tutorial are based on 3 tools:
Use cases are based on a text classification task on 20newsgroup dataset. Adummytutorial is also available to show tools mechanisms.
Prerequisites
For this tutorial, you must be familiar with the following tools:
- virtualenv or condaenv
- make
- git
- python
DVC is an open-source version control system for Machine Learning projects. It is usedfor versioning and sharing Machine Learning data, and reproducing Machine Learningexperiments and pipeline stages.
mlvtools provides tools to generate Python scripts and DVC commands from JupyterNotebooks.
Please have a look at thepresentation.
- Notebook parametrized conversion (mlvtools)
- Pipelining (DVC andmlvtools)
- Data x Code x Hyperparameters versioning (DVC andmlvtools)
Goal: find a way to version code, data and pipelines.
Starting from an existing project composed of multiple Python modules and a set ofJupyter notebooks, we want to create an automated pipeline in order to version, shareand reproduce experiments.
│── classifier│ ├── aggregate_classif.py│ ├── __init__.py│ ├── extract.py│ └── ...│── notebooks│ ├── Augment train data.ipynb│ ├── Check data and split and train.ipynb│ ├── Extract data.ipynb│ ├── Learn text classifier.ipynb│ ├── Learn aggregated model.ipynb│ ├── Preprocess image data.ipynb│ └── Train CNN classifier on image data.ipynb│── README.md│── requirements.yml│── setup.cfg│── setup.py
The data flow is processed by applying steps and intermediary results are versionedusing metadata files. These steps are defined in Jupyter notebooks, which are thenconverted to Python scripts.
Keep in mind that:
- The reference for the code of the step remains in the Jupyter notebook
- Pipelines are structured according to their inputs and outputs
- Hyperparameters are pipeline inputs
│── classifier│ ├── aggregate_classif.py│ ├── __init__.py│ ├── extract.py│ └── ...│── notebooks│ ├── Augment train data.ipynb│ ├── Check data and split and train.ipynb│ ├── Extract data.ipynb│ ├── Learn text classifier.ipynb│ ├── Learn aggregated model.ipynb│ ├── Preprocess image data.ipynb│ └── Train CNN classifier on image data.ipynb│── pipeline│ ├── dvc ** DVC pipeline steps│ │ ├─ mlvtools_augment_train_data_dvc│ │ ├─ ..│ ├── scripts ** Notebooks converted into Python configurable scripts│ │ ├─ mlvtools_augment_train_data.py│ │ ├─ ..│── README.md│── requirements.yml│── setup.cfg│── setup.py
For each Jupyter notebook a Python parameterizable and executable script is generated.This script makes it easier to version code and automate pipeline executions.
Pipelines are composed of DVC steps. Those steps can be generated directly from theJupyter notebook based on parameters described in the Docstring. (notebook -> pythonscript -> DVC command)
Each time a DVC step is run a DVC meta file ([normalized_notebook_name].dvc
) iscreated. This metadata file represents a pipeline step, it is the DVC result of a stepexecution. Those files must be tracked using Git. They are used to reproducea pipeline.
Application:
For each step in the tutorial the process remain the same.
Write a Jupyter notebook which corresponds to a pipeline step. (See Jupyter notebooksyntax section inmlvtools documentation)
Test your Jupyter notebook.
Add it under git.
Convert the Jupyter notebook into a configurable and executable Python scriptusing
ipynb_to_python
.ipynb_to_python -n ./pipeline/notebooks/[notebook_name] -o ./pipeline/steps/[python_script_name]
Ensure Python executable and configurable script is well created into
./pipeline/steps/[python_script_name]
../pipeline/steps/[python_script_name] -h
Create a DVC commands to run the Python script using DVC.
gen_dvc -i ./pipeline/steps/[python_script_name] \ --out-dvc-cmd ./scripts/cmd/[dvc_cmd_name]
Ensure DVC command is well created.
Add generated command and Python script under git.
Add step inputs under DVC.
Run DVC command
./scripts/cmd/[dvc_cmd_name]
.Check DVC meta file is created
./[normalized notebook _name].dvc
Add DVC meta file under git
Need | Feature |
---|---|
Ignore notebook cell | # No effect |
DVC input and ouptuts | :dvc-in ,:dvc-out |
Add extra parameters | :dvc-extra |
Write DVC whole command | :dvc-cmd |
Convert Jupiter Notebook to Python script | ipynb_to_python |
Generate DVC command | gen_dvc |
Create a pipeline step from a Jupiter Notebook | ipynb_to_python ,gen_dvc |
Add a pipeline step with different IO | Copy DVC step then edit inputs, outputs and meta file name |
Reproduce a pipeline | dvc repro [metafile] |
Reproduce a pipeline with no cache | dvc repro -f [metafile] |
Reproduce a pipeline after an algo change | dvc repro -f [metafile] or run impacted step individually then complete the pipeline. |
It is allowed to modify or duplicate a DVC command to change an hyperparameter or runa same step twice with different parameters.
It is a bad idea to modify generated Python scripts. They are generated from Jupyternotebooks, so changes should be done in Jupyter notebooks and then scripts should bere-generated.
To complete this tutorial clone this repository:
git clone https://github.com/peopledoc/mlvtools-tutorial
Create and a Python virtual environment, and activate it:
virtualenv --python python3 venvsource venv/bin/activate
Install requirements:
make develop
All other steps are explained in each use case.
- How DVC works
- mlvtools pipeline features (on simple cases)
- Going further with more realistic use cases: