Product

Tuesday, 20 February 2024

Easier Experimenting in Python

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.normalnp.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

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

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...