The ML workflow
Why This Matters
In a conventional project, testers often arrive when there is code to test. In an ML project, most of the decisions that determine quality are made before there is a model: which metric counts, which data is used, how it is split. A tester who knows the workflow knows where to be, and when.
What you will learn
- The eleven steps of the ML workflow, from objectives to monitoring
- Which steps form model generation, and why the workflow is iterative
- Where a tester can influence quality in each step
Syllabus: AI-3.1.2.
The eleven steps
1 Understand the objectives
2 Select a framework
3 Select and build the algorithm
4 Prepare and test the data <───────────────────────┐
5 Train the model ┐ │
6 Evaluate the model ├ model generation <──┐ │
7 Tune the model ┘ │ │
8 Test the model ─── much worse? ────────────┴─────────┘
9 Deploy the model
10 Use the model
11 Monitor and tune the model ─── drift? retrain ───> back to 4 or 5
1. Understand the objectives
Stakeholders agree what the model is for. The acceptance criteria and the ML functional performance metrics are defined here: which metric counts (recall? precision?), and what threshold is good enough (lesson 8).
2. Select a framework
Based on the objectives, the acceptance criteria and business priorities (lesson 3).
3. Select and build the algorithm
Chosen from the objectives, the acceptance criteria and the data available: a decision tree, a neural network, a random forest. Usually taken from the framework’s library rather than written from scratch.
4. Prepare and test the data
Data acquisition, preprocessing and feature engineering (lesson 15), often with exploratory data analysis (EDA) alongside. The training, evaluation and test data must represent the data the model will see in operation. The data pipeline is tested here (module 6).
5. Train the model
The algorithm learns from the training data. Two kinds of settings, called hyperparameters, are chosen by people, not learned:
- model hyperparameters define the structure of the model: the number of layers of a network, the maximum depth of a tree (
max_depth=2in lesson 13) - algorithm hyperparameters control the training: the number of iterations, the learning rate
6. Evaluate the model
The trained model is evaluated on the validation data against the agreed metrics. Usually several algorithms and datasets are compared.
7. Tune the model
Adjust the hyperparameters based on the evaluation and train again.
Steps 5, 6 and 7 together are called model generation. They repeat until the model is good enough on the validation data.
8. Test the model
The tuned model is tested on an independent test dataset that played no part in training or tuning. If the results are much worse than in evaluation, go back to model generation or to data preparation. Non-functional tests, such as the time a prediction takes, can be run here too.
9. Deploy the model
The model is re-engineered together with its data pipeline for the target environment, often with tools from the framework: an embedded system, a web API in the cloud. The deployed model is tested again against the acceptance criteria, because conversion and the new environment can change its behaviour (lesson 52).
10. Use the model
The model is part of a larger system, and makes predictions in batches on a schedule (every night, score all new applications) or in real time (score this application now).
11. Monitor and tune the model
The world changes, and the model’s performance with it: drift (lesson 47). The model is retrained with new data or new hyperparameters, and a new version can be compared with the current one using A/B testing (lesson 49).
Two things to remember
The workflow is iterative. Steps loop back: a bad test result sends you back to the data, drift sends you back to training. It is not a waterfall.
The model is only part of the system. It is integrated with non-ML components (user interface, databases, other services), so the familiar test levels still apply: integration, system and acceptance testing (lesson 31).
Where testers add value
| Step | What a tester does |
|---|---|
| 1 Objectives | Make the acceptance criteria measurable; ask “which errors are expensive?” |
| 2-3 Framework, algorithm | Review the choice: explainability, known weaknesses, framework maturity |
| 4 Data | Test the data and the pipeline: constraints, bias, representativeness, labels |
| 5-7 Model generation | Check that validation and test data are kept apart; watch for overfitting |
| 8 Test | Independent testing on an independent test set; statistical evaluation; robustness, bias, metamorphic testing |
| 9 Deploy | Retest after conversion; deployment, API and performance testing |
| 10 Use | Integration, system and acceptance testing |
| 11 Monitor | Drift detection, A/B testing of new versions, regression testing |
Exercise
Put these activities in workflow order, give the step number, and mark the ones that are part of model generation.
a. The model is converted to run on a phone, and accuracy is measured again on the phone. b. The team agrees that recall on fraud must be at least 90%. c. The tree depth is increased from 3 to 5 because validation accuracy was low. d. Duplicate records are removed and missing incomes are filled in. e. After three months, the share of approvals has dropped and the model is retrained. f. The final model is run once on 2,000 transactions it has never seen. g. A random forest is trained on the training data. h. Accuracy on the validation set is calculated for three candidate models.
Show suggested answer
b (1 objectives), d (4 data), g (5 train), h (6 evaluate), c (7 tune), f (8 test), a (9 deploy), e (11 monitor and tune).
Model generation: g, h and c (steps 5-7). Note that c is a model hyperparameter change. Step f uses the test set only once, after tuning is finished; if the result is much worse than on validation, you go back to model generation or data preparation.
Exam-style questions
1. Which steps of the ML workflow together form model generation?
- A. Prepare data, train, evaluate
- B. Train, evaluate, tune
- C. Evaluate, tune, test
- D. Test, deploy, use
Show answer
2. The test results of a tuned model are much worse than its evaluation results. What should happen?
- A. Deploy the model, because the evaluation results were good
- B. Return to model generation or data preparation
- C. Use the test data for further tuning until the test results improve
- D. Report the evaluation results instead
Show answer
3. Which is an example of a model hyperparameter?
- A. The weights learned during training
- B. The number of layers in a neural network
- C. The number of training iterations
- D. The accuracy on the test set