All writing

Street fighting machine learning (part 2)

what do you do when your ML model breaks, and you don't know why.

Another story from back in the day: I was on a small team developing a classifier for an NLP problem. The output would show up in a client-facing interface.

The evaluation procedure is plain CV/stratified CV. It’s the standard bible, what could go wrong, right ? The CV result was good, even after adjusting for a highly imbalanced dataset, I could feel confident in the solution.

But I kept running into this problem, that from time to time, a client complained that they kept seeing the same-same-but-different texts misclassified, even though we had explicitly added them to the training set.

Missing out on a totally new case that does not look like anything we’ve seen is at least an acceptable error, epistemologically speaking (how do we know what we don’t know, right?). But in this case, we were missing out on something similar to what we’ve seen. Now that is serious, as over time it will erode the client’s trust in the classified result.

It became crucial to find a way to QA the result of the classifier. It was then that we realized a single testset, even CV, was not going to give us enough insight into how the model was performing in production. We needed to know exactly how the model would perform in different situations, then decide based on that whether or not it could be released.

Does that ring a bell to you ?

Machine learning models as software

In vanilla software development, we write code to perform certain tasks in certain ways. With ML, we kinda write a piece of code that writes code. This saves us a large amount of time in figuring out the logic of such software, by telling the machine to learn by examples.

After seeing the data, the model logic is now fully specified. It becomes a deterministic piece of software.

However, not all the things that the machine learns are what we intended. The models will learn a bunch of random bullshit if we force them. This brings the need to control the behavior of such models. We now want the piece of code (that the machine writes) to do things in certain ways before we release it. Or in other words, we want tests.

Testing ML models like we test software

From traditional development testing, we want our piece of code to perform in a certain, known way. In order to do that, we think about cases when it may break, and test the piece of code against those cases. We gain confidence that this piece will not break when operated in the real world, because we have already seen how it performs in certain scenarios.

This is what I lacked in the example above, when all I used to control the quality was CV scores. Now, there is nothing wrong with testing models like this. This is the standard bible of testing, if it’s confined to academic purposes, when we want to compare algorithms head on.

Testing and QA as a part of the software dev process has a different purpose compared to algorithm testing. When we write a piece of code, we want to know exactly what it does and how.

Constructing test cases for ML models

Given that there are so many approaches to modelling, there is no one size fits all when it comes to testing different models. In many domains, we can construct synthetic samples that simulate certain scenarios. This is quite popular in the finance sector for example.

In other domains, synthetic datasets are harder to come by. That forces us to look more carefully into our data in order to come up with scenario-based test sets. Some algorithms work by finding similar samples and grouping them into the same regions.

Those groups can be used to construct tactful real world testing datasets. Each sample from such a group could be used as a representative of such a group of cases. We can later test other models against such test sets, and see how they perform in different cases. This kind of compact test set also helps us find leads when a model constantly fails a test case.

Kernel methods and similarity based methods are good examples. This also extends to neural networks, where we can think about them as linear models built on top of a kernel, and trained jointly. If we take the output of the layers before the final linear layers, most often we can find that similar samples are located close to each other.

Just like we have different testing paradigms for different kinds of software, constructing test sets will also be different when we choose different ML algorithms. Testing for classification models will be different from that of regression.

We’ll explore the technical details in the next post, when we’ll go into an example of constructing such test cases.