Sitemap
Ivan Nardini

Ivan Nardini

AI/ML DevRel Engineer at @GoogleCloud. Helping developers to build AI and ML applications using their preferred libraries, models, and tools on Vertex AI

Back to the Machine Learning fundamentals: How to write Pipeline for Model deployment (Part 2/3)

7 min readJul 19, 2020

--

Press enter or click to view image in full size
Figure 1: Let’s look at a pipeline

Update 3/9/2022: You can find the repository on the project on Github.

In the previous episode…

In Part One, I develop an example of Machine Learning Pipeline using Procedural Programming approach starting from a Kaggle’s Fraud Claim Detection notebook.

Even though several advantages this approach provides (for example, “soft” software developer skills required and the code that becomes your pipeline functions), I face several challenges.

Indeed,

  • I spend a lot of time on debugging “get_dummies” effect on the feature inputs.

But even worse,

  • I had to define a very detailed configuration for transformation parameters

For those reasons, in this article I use the same example to explore the alternative programming approach to build machine learning pipeline: Object-oriented programming (OOP).

But, before to jump into the code, I think it’s important to dive deep some main concepts I’m going to use later on.

Object-Oriented Programming for Data Scientists

Press enter or click to view image in full size
Figure 2: The classic ‘CAR’ OOP example

Imagine you have to explain how your Machine Learning Pipeline looks like and what it does to Machine Learning Engineer. You might say

My pipeline is a “sequence of code snippets” I have in cells that generates some output given some “data input” and “parameters”

Well, you describe a Class where

  • “sequence of code snippets” represent its Methods
  • “data input” and “parameters” represent its Attributes

Of course, each time you change these Attributes (data input and parameters in your code) you can build or “construct” a different Pipeline Object from a Class calling Methods.

Notice I use “construct” term because one of the special methods is the Constructor. It's the function which uses the __init__ keyword and it allows to create the object from a class instantiating the attributes of that class.

Below the high-level anatomy of the pipeline.

Press enter or click to view image in full size
Figure 3: The Anatomy of a Pipeline

As you can see I design the Pipeline in order to have

The processing steps as objects. The Pipeline as a sequence of objects (OOP).

A couple of considerations about the picture:

  • In this case, I only use Instance Variables ie variables which becomes accessible after the object is instantiated. And they differ from Class Variables which are always accessible. Below the random_state variable if I had define it like a class variable.
  • Missing_Imputer is a component of the Pipeline. This is because the Composition property. In the example the fitmethod in thePipelineclass contains theMissing_Imputer in the Preprocessing class. The idea here is that every Pipeline “has a” Data Preprocessing step. Then It has to access to that class.
  • Another important difference is between an Instance Method and a Class Method. Essentially, an Instance method allows us to access instance variables (“self” variables) while Class method allows us to access class variables and consequently modify the class itself.

Of course, this is not an article about Object Oriented Programming. There are many other concepts a Data Scientist needs to know (for example Data Encapsulation, Inheritance, Polymorphism and soon).

But it is important to have an idea of these concepts because they justify OOP methods for building Machine Learning Pipeline.

So the question is

What about the benefits of “Custom” Object Oriented Machine Learning Pipelines?

“Custom” Object Oriented Machine Learning Pipelines: Pros and Cons

Based on this experience, one of the greatest advantages of this approach is related at Development stage.

Indeed, because the Pipeline becomes just a representation of a series of entities which maybe tested, versioned, tracked and controlled easily. And notice this is possible because the nature of class.

Because its nature, Pipeline class becomes a representation of a series of entities which maybe tested, versioned, tracked and controlled easily.

Indeed, this Pipeline promotes independence between entities of the entire process. In my case, I thought it was great to have several Component Classes (Preprocessing, Models, PostProcessing) and then build a Pipeline methods based on them. It made me feel like a mason with bricks and mortar.

Object Oriented Pipelines promotes independence between steps of the entire process.

Related to what I mentioned in the previous article, I can imagine how it could improves coding best practices in a development team and it could implies time and financial cost savings.

They improve Software Development Practices

Also because this independence I could imagine to build more articulated processes those could satisfy business needs better. In my case I’ve just apply that to build a composite predictmethod.

But think about the business uplift a cascade pipeline could generates because you can build a sequences of independent models each of them attempts to classify what the previous one misses.

You can build more articulated processes those could satisfy business needs better.

So, at that time, you may imagine the “Custom” Object Oriented Machine Learning Pipelines like one of the most powerful power ups of Super Mario World: the Star Power Up. It would allow you to run faster and kick out the model deployment challenges.

“Custom” OOP Pipelines are not a Star Power Ups!

Figure 3: The Star Power Up

But, let me to show you my code

Even if I described it, It may looks like a bit “complex” right? Especially if you are a Research Data Scientist.

Building a Object Oriented Pipeline implies Software complexity

Then if we deep dive to the code, we can see how it strongly depends on the data the pipeline ingests and on the model.

Each methods that deal with data may need to be code again. Also a new model may needs a new pipeline.

Below you can see that we decide to replace ‘?’ character with ‘missing’. But what if the data that come from the field have a different unknown character?

The same is for the model. Look at the model attributes of the pipeline. They work just for Sklearn’s RandomForestClassifier.

Of course, complexity and data-model pipeline dependency have several consequences we can summarize saying that you need software developers to build and maintain these pipelines. Otherwise, they slow down Data Scientists those would have to familiarize with the software (and not just the code).

That is, if you throw the Full-Stack Data Scientists ;)

To build a good Object Oriented Pipeline, you may need a Software Development Team.

Final Consideration and… a side talk

The end…

Once again, we are at the end of the episode.

Figure 4: The End

Although the complexities software implies, I was able to achieve a satisfactory result as you can see below.

Press enter or click to view image in full size

What I discuss so far it’s related to a tiny experimental context. Challenges like data-model pipeline dependency may be tackle with a more structured and well-developed approach.

But, all this is useful for knowledge. Indeed I think it’s important to build, test and make small things works. Even if it does not mean that It would be easier to scale them, at least you will able to tackle issues are not dependent to the size.

At least I hope so.

Also it produces feedback.

The Side Talk

In the previous article, I mentioned the get_dummies bug I faced.

It seems not so uncommon…doesn’t it, Jimmy?

Press enter or click to view image in full size

Also, I underline the importance of setting the seed.

The first rule of “Model Deployment” Club is: Always set the random seed!

And after few hours, I had this great talk with Elle O’Brien, Data Scientist at Iterative.ai and Laszlo Sragner, the Co-Founder / Head of Data Science at Arkera about Randomization and Model Reproducibility.

She just underlines that

Press enter or click to view image in full size

Here an interesting stack exchange conversation she shared. At the same time, he suggests don’t use random number generator at all. Below his argumentation.

Press enter or click to view image in full size

Summary

To sum up, in this Part Two of the Back to the Machine Learning fundamentals series, I explore the alternative approach for building Machine Learning Pipeline: Object-oriented programming (OOP).

In particular, I provide some of the main concepts of Object-Oriented Programming to allows who reads this article to understand the code I present.

Then I cover advantages and disadvantages of this approach based on the hands-on

Finally, I provide some personal consideration and I mentioned I really interesting talk I had about the Randomization and Model Reproducibility

As always, I hope you enjoy the article and if you’re interested you can find the project at link below

and the Part 3 of the series here

Enjoy yourself and feel free to reach me out on Linkedin.

References

  1. https://www.udemy.com/course/deployment-of-machine-learning-models/
  2. https://levelup.gitconnected.com/designing-a-machine-learning-micro-service-dbac65c3b9fe
  3. https://en.wikipedia.org/wiki/Object-oriented_programming
  4. https://docs.python.org/3/tutorial/classes.html
  5. https://towardsdatascience.com/a-simple-guide-to-object-oriented-programming-for-data-scientist-56224fee8f67#:~:text=Being%20a%20data%20scientist%2C%20you,like%20a%20developer%20would%20do.&text=Key%20data%20science%20libraries%2C%20such,%2C%20methods%2C%20attributes%2C%20etc.
  6. https://towardsdatascience.com/a-data-scientist-should-know-at-least-this-much-python-oop-d63f37eaac4d
  7. https://www.listendata.com/2019/08/python-object-oriented-programming.html#Object-and-Class
  8. https://www.makeuseof.com/tag/python-instance-static-class-methods/

--

--