Training Your First Model

Introduction #

In this tutorial, we describe the recommended way to train a simple machine learning model on Neuro Platform. As our ML engineers prefer PyTorch over other ML frameworks, we show the training and evaluation of one of the basic PyTorch examples.

We assume that you have already signed up to the platform, installed the Neuro CLI, and logged into the platform (see Getting started).

We base our example on the Classifying Names with a Character-Level RNN tutorial.

Initializing a new project #

To simplify working with Neuro Platform and to help to establish the best practices in the ML environment, we provide a project template. This template consists of the recommended directories and files. It is designed to operate smoothly with our base environment.

Let’s initialize a new project from this template:

neuro project init

This command asks several questions about your project:

project_name [Name of the project]: Neuro Tutorial project_slug [neuro-tutorial]: code_directory [modules]: rnn

Project structure #

After you execute the command mentioned above, you get the following structure:

neuro-tutorial ├── data/ <- training and testing datasets (we do not keep it under source control) ├── notebooks/ <- Jupyter notebooks ├── rnn/ <- source code of models ├── .gitignore <- default .gitignore for a Python project ├── Makefile <- commands for manipulating training environment (see `make help`) ├── README.md <- auto-generated informational file ├── apt.txt <- list of system packages to be installed in the training environment ├── requirements.txt <- list Python dependencies to be installed in the training environment └── setup.cfg <- linter settings (Python code quality checking)

When you run a job (for example, via make jupyter), the directories are mounted to the job as follows:

Mount PointDescriptionStorage URI
/project/data/Training / testing datastorage:neuro-tutorial/data/
/project/rnn/User's Python codestorage:neuro-tutorial/rnn/
/project/notebooks/User's Jupyter notebooksstorage:neuro-tutorial/notebooks/
/project/results/Logs and resultsstorage:neuro-tutorial/results/

This mapping is defined as variables in the top of Makefile and can be adjusted if needed.

Filling the project #

Now we need to fill newly created project with the content:

  • Change working directory:
cd neuro-tutorial
curl https://raw.githubusercontent.com/pytorch/tutorials/master/intermediate_source/char_rnn_classification_tutorial.py -o rnn/char_rnn_classification_tutorial.py
  • Add requirements.txt in your project root folder with this file:
curl https://raw.githubusercontent.com/pytorch/tutorials/master/requirements.txt -o requirements.txt
  • Download data from here, extract ZIP’s content and put it in your data folder:
curl https://download.pytorch.org/tutorial/data.zip -o data/data.zip && unzip data/data.zip && rm data/data.zip

Training and evaluating the model #

When you start working with a project on Neuro Platform, the basic flow looks as follows: you set up the remote environment, upload data and code to your storage, run training, and evaluate the results.

To set up the remote environment, run

make setup

This command will run a lightweight job (via neuro run), upload the files containing your dependencies apt.txt and requirements.txt (via neuro cp), install the dependencies (using neuro exec), do other preparatory steps, and then create the base image from this job and push it to the platform (via neuro save, which works similarly to docker commit).

To upload data and code to your storage, run

make upload-all

To run training, you need to run specify the training command in Makefile, and then run make train:

  • open Makefile in editor,
  • find the following line:
TRAINING_COMMAND?='echo "Replace this placeholder with a training script execution"'
  • and replace it with the following line:
TRAINING_COMMAND?="bash -c 'cd $(PROJECT_PATH_ENV) && python -u $(CODE_DIR)/char_rnn_classification_tutorial.py'"

Now, you can run

make train

and observe the output. You will see how some checks are made at the beginning of the script, and then the model is being trained and evaluated:

['data/names/German.txt'undefined 'data/names/Polish.txt'undefined 'data/names/Irish.txt'undefined 'data/names/Vietnamese.txt'undefined 'data/names/French.txt'undefined 'data/names/Japanese.txt'undefined 'data/names/Spanish.txt'undefined 'data/names/Chinese.txt'undefined 'data/names/Korean.txt'undefined 'data/names/Czech.txt'undefined 'data/names/Arabic.txt'undefined 'data/names/Portuguese.txt'undefined 'data/names/English.txt'undefined 'data/names/Italian.txt'undefined 'data/names/Russian.txt'undefined 'data/names/Dutch.txt'undefined 'data/names/Scottish.txt'undefined 'data/names/Greek.txt'] Slusarski ['Abandonato'undefined 'Abatangelo'undefined 'Abatantuono'undefined 'Abate'undefined 'Abategiovanni'] tensor([[0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 1.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.undefined 0.]]) torch.Size([5undefined 1undefined 57]) tensor([[-2.8248undefined -2.9118undefined -2.8999undefined -2.9170undefined -2.8916undefined -2.9699undefined -2.8785undefined -2.9273undefined -2.8397undefined -2.8539undefined -2.8764undefined -2.9278undefined -2.8638undefined -2.9310undefined -2.9546undefined -2.9008undefined -2.8295undefined -2.8441]]undefined grad_fn=<LogSoftmaxBackward>) ('German'undefined 0) category = Vietnamese / line = Vu category = Chinese / line = Che category = Scottish / line = Fraser category = Arabic / line = Abadi category = Russian / line = Adabash category = Vietnamese / line = Cao category = Greek / line = Horiatis category = Portuguese / line = Pinho category = Vietnamese / line = To category = Scottish / line = Mcintosh 5000 5% (0m 19s) 2.7360 Ho / Portuguese ✗ (Vietnamese) 10000 10% (0m 38s) 2.0606 Anderson / Russian ✗ (Scottish) 15000 15% (0m 58s) 3.5110 Marqueringh / Russian ✗ (Dutch) 20000 20% (1m 17s) 3.6223 Talambum / Arabic ✗ (Russian) 25000 25% (1m 35s) 2.9651 Jollenbeck / Dutch ✗ (German) 30000 30% (1m 54s) 0.9014 Finnegan / Irish ✓ 35000 35% (2m 13s) 0.8603 Taverna / Italian ✓ 40000 40% (2m 32s) 0.1065 Vysokosov / Russian ✓ 45000 45% (2m 52s) 3.6136 Blanxart / French ✗ (Spanish) 50000 50% (3m 11s) 0.0969 Bellincioni / Italian ✓ 55000 55% (3m 30s) 3.1383 Roosa / Spanish ✗ (Dutch) 60000 60% (3m 49s) 0.6585 O'Kane / Irish ✓ 65000 65% (4m 8s) 4.7300 Satorie / French ✗ (Czech) 70000 70% (4m 27s) 0.9765 Mueller / German ✓ 75000 75% (4m 46s) 0.7882 Attia / Arabic ✓ 80000 80% (5m 5s) 2.1131 Till / Irish ✗ (Czech) 85000 85% (5m 25s) 0.5304 Wei / Chinese ✓ 90000 90% (5m 44s) 1.6258 Newman / Polish ✗ (English) 95000 95% (6m 2s) 3.2015 Eberhardt / Irish ✗ (German) 100000 100% (6m 21s) 0.2639 Vamvakidis / Greek ✓ > Dovesky (-0.77) Czech (-1.11) Russian (-2.03) English > Jackson (-0.92) English (-1.65) Czech (-1.85) Scottish > Satoshi (-1.32) Italian (-1.81) Arabic (-2.14) Japanese