When we work on a machine learning project, we quite often need to experiment with multiple alternatives. Some features in Python allow us to try out different options without much effort. In this tutorial, we are going to see some tips to make our experiments faster.
After finishing this tutorial, you will learn:
- How to leverage a duck-typing feature to easily swap functions and objects
- How making components into drop-in replacements for each other can help experiments run fasterOverview
This tutorial is in three parts; they are:
- Workflow of a machine learning project
- Functions as objects
- Caveats
Workflow of a Machine Learning Project
Consider a very simple machine learning project as follows:
This is a typical machine learning project workflow. We have a stage of preprocessing the data, then training a model, and afterward, evaluating our result. But in each step, we may want to try something different. For example, we may wonder if normalizing the data would make it better. So we may rewrite the code above into the following:
So far, so good. But what if we keep experimenting with different datasets, different models, or different score functions? Each time, we keep flipping between using a scaler and not would mean a lot of code change, and it would be quite easy to make mistakes.
Because Python supports duck typing, we can see that the following two classifier models implemented the same interface:
Therefore, we can simply select between these two version and keep everything intact. We can say these two models are drop-in replacements for each other.
Making use of this property, we can create a toggle variable to control the design choice we make:
By toggling the variable USE_SCALER between True and False, we can select whether a scaler should be applied. A more complex example would be to select among different scaler and the classifier models, such as:
A complete example is as follows:
If you go one step further, you may even skip the toggle variable and use a string directly for a quick experiment:
Functions as Objects
In Python, functions are first-class citizens. You can assign functions to a variable. Indeed, functions are objects in Python, as are classes (the classes themselves, not only incarnations of classes). Therefore, we can use the same technique as above to experiment with similar functions.
The above is similar to calling np.random.normal(size=(10,5)), but we hold the function in a variable for the convenience of swapping one function with another. Note that since we call the functions with the same argument, we have to make sure all variations will accept it. In case it is not, we may need some additional lines of code to make a wrapper. For example, in the case of generating Student’s t distribution, we need an additional parameter for the degree of freedom:
This works because in the above, np.random.normal, np.random.uniform, and t_wrapper as we defined, are all drop-in replacements of each other.
Caveats
Machine learning differs from other programming projects because there are more uncertainties in the workflow. When you build a web page or build a game, you have a picture in your mind of what to achieve. But there is some exploratory work in machine learning projects.
You will probably use some source code control system like git or Mercurial to manage your source code development history in other projects. In machine learning projects, however, we are trying out different combinations of many steps. Using git to manage the different variations may not fit, not to say sometimes may be overkill. Therefore, using a toggle variable to control the flow should allow us to try out different things faster. This is especially handy when we are working on our projects in Jupyter notebooks.
However, as we put multiple versions of code together, we made the program clumsy and less readable. It is better to do some clean-up after we confirm what to do. This will help us with maintenance in the future.
Further reading
This section provides more resources on the topic if you are looking to go deeper.
Books
- Fluent Python, second edition, by Luciano Ramalho, https://www.amazon.com/dp/1492056359/
Summary
In this tutorial, you’ve seen how the duck typing property in Python helps us create drop-in replacements. Specifically, you learned:
- Duck typing can help us switch between alternatives easily in a machine learning workflow
- We can make use of a toggle variable to experiment among alternatives

No comments:
Post a Comment