Introduction to a numpy API for ONNX: CustomClassifier

This notebook shows how to write python classifier using similar functions as numpy offers and get a class which can be inserted into a pipeline and still be converted into ONNX.

A custom binary classifier

Let's imagine a classifier not that simple about simple but not that complex about predictions. It does the following:

Some data first...

Split into train and test as usual.

The model...

Let's compare the model a single logistic regression. It shouuld be better. The same logistic regression applied on both sides is equivalent a single logistic regression and both half logistic regression is better on its side.

However, this is true in average but not necessarily true for one particular datasets. But that's not the point of this notebook.

Let's draw the model predictions. Colored zones indicate the predicted class, green line indicates the hyperplan splitting the features into two. A different logistic regression is applied on each side.

Conversion to ONNX = second implementation

The conversion fails as expected because there is no registered converter for this new model.

Writing a converter means implementing the prediction methods with ONNX operators. That's very similar to learning a new mathematical language even if this language is very close to numpy. Instead of having a second implementation of the predictions, why not having a single one based on ONNX? That way the conversion to ONNX would be obvious. Well do you know ONNX operators? Not really... Why not using then numpy functions implemented with ONNX operators? Ok! But how?

A single implementation with ONNX operators

A classifier needs two pethods, predict and predict_proba and one graph is going to produce both of them. The user need to implement the function producing this graph, a decorator adds the two methods based on this graph.

It works with double too.

And now the conversion to ONNX.

Let's check the output.