avatarCharu Makhijani

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6130

Abstract

nobody else saw, not even me when reacquainted with the pulses of the everyday world.</p><p id="9c95">It may have been wandering, or boredom, or self-destructive behavior that led me into the bathroom at the blue level of Madison Square Garden with Keegan, a friend from boarding school. I was getting ready to drop two windowpanes of a four-way hit. Whatever I was seeking, I knew that, at the very least, this dose would provide a counter-irritant to douse the peat-fire of boredom and wanting burning in my guts.</p><p id="4657">Keegan asked me how many panes I wanted to take.</p><p id="c93a">I asked, “how many are you going to take”?</p><p id="9068">He said, “two”.</p><p id="46ca">I said, “I’ll take two, then.”</p><p id="b8c7">Soon after I took them he told me that he had dosed on this same acid two days before, which would mean that he had some resistance to the batch and was doubling his intake to make sure he got high. What it meant for me was that if the acid was any good I was going to get very high, and the acid was good.</p><p id="a94e">Whether what Keegan had done was “mean”, “unfair”, “stupid”, “dangerous”, “funny”, or “not a big deal”, wasn’t a question I asked at the time. It was two decades before I began to realize that some of my “friends” were not my friends. It may be of no surprise to people older than fifty that many of the people I once called “friends” were really transactional acquaintances forged in a furnace of boredom and need. At some point in my life (like, about age forty) there was the awful realization that some of the people I referred to as “friends” were simply people that let me hang out with them. They didn’t actively humiliate or shun me, and so, to my adolescent pollywog brain, they were “friends”, even if there was no reciprocity in our relationship.</p><p id="ba48">Keegan was not someone I hung onto. In the brutal pecking order of boarding school hierarchy he and I were roughly equal, though we shouldn’t have been. Keegan was smart and funny, but he was also overweight, messy, and occasionally obstinate, in the kind of peevish way that eventually stops making sense. In a milieu where sports, good looks, and emotional control counted in the calculation of your social credit score, I could fairly count Keegan as an “equal” despite the fact that he was a more compelling, charismatic, and engaging character than I was.</p><p id="9ded">Keegan had another strike against him that may be hard to explain nowadays. His parents were divorced and he was being raised by a single mother. Why that information reflected poorly on him is a topic for another serving of dreck, but there is no question that my dysfunctional, alcohol-soaked, “in-tact” nuclear family gave me a lift. Keegan’s mother, who was smart but stranded economically (though not so stranded that she couldn’t afford boarding school for her son) came to parent’s day alone, or didn’t come at all. While that information seems like it would be the last thing that teenage boys would care about, somehow it factored into the equation, and, in ways that confuse both logic and analysis, made it easier for us to take Keegan himself less seriously.</p><p id="6a81">As I look back at it, I don’t think Keegan was being a dick when he gave me the double dose. We can explain it away by using the euphemism, “he was being mischievous”. He saw it as a prank. Had he not died of a drug overdose when we were in our twenties, I am certain that today he would be willing to either apologize or explain to me why he didn’t need to apologize. I’m sorry he can’t do that.</p><h2 id="ba57">Part II: The Trip</h2><p id="4aa8">After dropping the acid in the bathroom, a metallic flush began on my tongue and filled my entire mouth while we were walking on the concourse towards our seats. I was seeing vivid color trails before any music started. When the Grateful Dead came out, I couldn’t quite fathom what was happening. All I saw was Gerry Garcia’s great gray set of hair mushrooming and breathing as he took the stage. His hair kept expanding until it filled more than a third of the Garden. Then then band began to play.</p><p id="6d9d">Here is the a recording in the concert. There is a crash at the beginning of the opening number, <i>Mississippi Half-Step</i>, which I clearly remember, though at the time, I couldn’t make any sense of it.</p> <figure id="f154"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fs_PakceAHxs%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Ds_PakceAHxs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fs_PakceAHxs%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><p id="06c9">Throughout the concert Keegan and I stayed in our seats. At one point a Deadhead “twirler” came up to our tier and spent what seemed like hours Grateful Dead dancing.</p> <figure id="9bcf"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FtmBIgvOYfLw&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DtmBIgvOYfLw&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FtmBIgvOYfLw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><p id="ef40">I must have been smoking. I didn’t really smoke much as a kid, but I didn’t “not smoke” and since almost everyone in the world smoked, I sometimes did. Two girls came up to our seats and asked to bum a cigarette from me. I ha

Options

d a pack of Marlboros, but I couldn’t find them in the Vietnam era army jacket I was wearing. I had taken the jacket off, so I just kept turning it over and over looking through various pockets, it began to look like a carnival ride of pickle green cubby-holes. The girls stared expectantly, Keegan kept up a running commentary under his breath that they couldn’t hear:</p><p id="c961">“They’re <i>still</i> waiting. The two girls are waiting patiently while the stoned kid paws at his jacket pockets and grunts. No, that’s a lighter, Gutbloom. A lighter is not a pack of cigarettes, even if you stare at it for a long, long, time. What’s this? Hurray! You found something. A ticket! which is also not a pack of cigarettes….” etc., etc.</p><p id="46a0">After I gave the girls cigarettes, they walked away, and then the ceiling of Madison Square Garden touched the floor.</p><p id="90b4">Forty years ago I might have been able to tell you the peculiar hallucinations that accompanied individual songs. Some of those visions still color my emotional reaction to those tunes if I listen to them now, which I seldom do.</p><p id="1111">More memorable is the image of Keegan and his younger brother, who met us after the concert, standing on a New York City street trying to figure out which way was east. I was quite certain I knew, and I pointed north and said, “That’s uptown”, then pointed south and said, “that’s downtown, so that,” pointing east, “must be east.” I don’t remember if they agreed.</p><p id="d787">We went into an arcade in Times Square named Playland.</p><figure id="4e7d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MPnG0QZ1e9-LcTsYgF3z0g.jpeg"><figcaption><a href="https://weber-street-photography.com/2015/08/01/playland-times-sq-1985/">“Playland” Times Sq. 1985, ©Matt Weber</a>. Used without permission.</figcaption></figure><p id="d46d">When I told my brother about my adventure a few weeks after the fact, he told me that Playland was one of the “crusiest places on the planet and I was lucky I wasn’t swarmed by <a href="https://en.wikipedia.org/wiki/Chickenhawk_(gay_slang)">chickenhawks</a>.” I wasn’t. No chickenhawks that I remember. No people. There were people, but I don’t remember them. I just remember the green lines of the video game and the sound that the tanks made when they materialized.</p><figure id="e2bc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wvY5F25mQqrqBt2iPefnlQ.gif"><figcaption></figcaption></figure><p id="bdd2">There was nothing epic about my trip to the land of Nod. All of the epic was inside my head. From the outside, we were just messy stoned kids wandering around the city.</p><p id="a983">We made it Grand Central Station before the last New Haven Line commuter train had departed for the suburbs. On it, we joined a group of Deadheads from Rye that Keegan knew. They were another dirty lot. One of them was even wearing a top hat. Someone had a tape recorder, and they were playing the concert we had just attended.</p><p id="5c29">An argument broke out between Keegan and someone else about whether the Dead had played the “Weather Report Suite” at the concert (they hadn’t).</p><p id="834d">I wasn’t a Deadhead and had no interest in the argument. At the time I wasn’t impressed by the Rye kids. My ignorance was so complete that I could arrogantly dismiss that which I knew nothing about on the thinnest shred of misunderstood and badly reasoned evidence. I only knew what I knew, which was painfully little, but I was certain <a href="https://readmedium.com/there-s-no-place-like-home-a218b7891be3">that my beloved suburb</a> was in every way superior to Rye, and, so, by the deductive process that renders simple ignorance into mindnumbingly cocksure adolescent arrogance, I figured that the kids from Rye were somehow “wanting” and I shouldn’t waste my time on them.</p><p id="c296">Little did I know that Rye was the town where Ogden Nash lived, where the Dick Van Dyke Show was set, and that gave us Nick Kroll. I thought it was simply the backdrop for <a href="https://playlandpark.org/">Rye Playland</a>. There was plenty I could have enjoyed in Rye.</p><p id="91f2">Some time in the morning we tumbled out onto the station platform and, still as a group, went to a downtown diner that was open. I had a plate of eggs that wiggled, breathed, and grew hairs. My mouth was full of the chemical taste of speedy acid and I knew that I would be awake for at least eight more hours.</p><p id="d68b">We left the wandering pack of Deadheads and made it back to Keegan’s house as dawn arrived. His mother was awake.</p><p id="d19a">Keegan went immediately downstairs.</p><p id="6e00">His mother and I talked for a long time in the kitchen. Mrs. Keegan was kind and interesting… interesting because she seemed genuinely interested in me. She, like my mother, was a Westover graduate, and I had the realization that she was just like one of my aunts… could be one of my aunts… sitting at the kitchen table and making deceptively sophisticated small talk. I didn’t know much, but I knew she was shrouding her concern for both me and her son in her subtle and psychologically-sophisticated set of questions. Her rejoinders to my answers were sagacious. I wish I could remember them.</p><p id="c82b">For all the Koans I could recite (“Why does the Buddha come from the East?”) or snippets of the Tao Te Ching I could burp out (“The name that can be named..”) I didn’t recognize one of the Masters even while she was instructing me. Of course I couldn’t see her. If I had, I would have had to recognize her sister rabbi who was in the kitchen at my house. These boddhisattvas, who understood, endured, and knew so much, were willing to put their own “desires” aside in an attempt to feed and care for pupa hell bent on fucking up their yet-to-be spun cocoons.</p><p id="d2f6">I wish I knew then what I know now. I had met the goddess on my non-ayahuasca trip.</p><p id="5733">But I didn’t know. I went downstairs into Keegan’s basement bedroom to smoke pot, listen to Jethro Tull, and watch the walls swim.</p></article></body>

Productionizing Machine Learning Models

Deploying models into production is a complex affair

Photo by Stephen Dawson on Unsplash

The biggest issue in the life-cycle of ML project isn’t to create a good algorithm or to generalize the results or to get good predictions or better accuracy. The biggest issue is to put ML systems into production. One of the known truth of Machine Learning world is that only a small part of real-world ML system is composed of ML code and a big part is model deployment, model retraining, maintenance, on-going updates and experiments, auditing, versioning and monitoring. And these steps take a huge part in ML systems technical debt as it exists at the system/platform level rather than the code/development level. Hence the model deployment strategy becomes a very crucial step in designing the ML platform.

Introduction

The first step in determining how to deploy a model is understanding the system with these questions-

  1. how the end user interacts with the model predictions.
  2. how frequently you should generate predictions.
  3. whether predictions should be generated for a single instance or a batch of instances at a time.
  4. what is the number of applications that will access this model?
  5. what are the latency requirements of these applications?

It’s indicative of the complexity of machine learning systems that many large technology companies that depend heavily on machine learning have dedicated teams and platforms that focus on building, training, deploying, and maintaining ML models. Here are some examples:

  • Databricks has MLFlow
  • Google has TensorFlow Extended (TFX)
  • Uber has Michelangelo
  • Facebook has FBLearner Flow
  • Microsoft has AI Lab
  • Amazon has Amazon ML
  • Airbnb has BigHead
  • JPMC has Omni AI

Machine Learning System vs Traditional Software System

1. Unlike Traditional Software Systems, ML systems deployment isn’t the same as deploying a trained ML model as a service. ML systems require a multi-step automated deployment pipeline for retraining, validation, and deployment of models — which adds complexity.

2. Testing an ML system involves model validation, model training, etc — in addition to the software tests such as unit testing and integration testing.

3. Machine Learning Systems are much more dynamic in terms of performance of the system due to varying data profiles and the model has to be retrained/refreshed often which leads to more iterations in the pipeline. This is not the case with Traditional Software Systems.

Model Portability (From Model Development to Production)

Writing code to predict/score data, is most often done in Jupyter notebooks or an IDE. Taking this model development code to the production environment requires converting language-specific code to some exchange format (compressed & serialized) that is language neutral and lightweight. Hence portability of the model is also a key requirement.

Below are the wide use formats for ML model portability-

1. Pickle — The pickle file is the binary version of a Python object which is used for serializing and de-serializing a Python object structure. Conversion of a python object hierarchy into a byte stream is called “pickling”. When the byte stream is converted back to object hierarchy this operation is called “unpickling”.

2. ONNX (Open Neural Network Exchange) — ONNX is an open-source format for machine learning models. ONNX has a common set of operators and file formats to use with models on a variety of frameworks and tools.

3. PMML (The Predictive Model Markup Language) — PMML is an XML-based predictive model interchange format. With PMML, you can develop a model on one system on an application and deploy the model on another system with another application, only by transmitting an XML configuration file.

4. PFA (Portable Format for Analytics) — PFA is an emerging standard for statistical models and data transformation engines. PFA has the ease of portability across different systems and models, pre-processing, and post-processing functions can be chained and built into complex workflows. PFA can be a simple raw data transformation or a sophisticated suite of concurrent data mining models, with a JSON or YAML configuration file.

5. NNEF (Neural Network Exchange Format) — NNEF is useful in reducing the pains in the machine learning deployment process by enabling a rich mix of neural network training tools for applications to be used across a range of devices and platforms.

There are some framework-specific formats as well, like — Spark MLWritable (Spark specific) and POJO / MOJO (H2O.ai specific).

CI/CD in Machine Learning

In traditional software systems, Continuous Integration & Delivery is the approach that provides automation, quality, and discipline for creating a reliable, predictable and repeatable process to release software into production. The same should be applied to ML Systems? Yes, but the process is not simple. The reason is in case of ML systems, changes to ML model and the data used for training also needs to be managed along with the code into the ML delivery process.

So unlike traditional DevOps, MLOps has 2 more steps every time CI/CD runs.

Continuous integration in machine learning means that each time you update your code or data, the machine learning pipeline reruns, which kick off builds and test cases. If all the tests are successful then Continuous Deployment begins that deploy the changes to the environment.

Within ML Systems, there is one more term for MLOps called CT (Continuous Training) which comes into the picture if you need to automate the training process.

Although the market has some reliable tools for ML Ops and new tools are also coming up, it's still new to predict the ML model outcome in the production environment.

New tools like, Gradient and MLflow are becoming popular for building robust CI/CD pipelines in ML systems. Tools such as Quilt, and Pachyderm are leading the way for forward-looking data science/ML workflows but they have not yet had widespread adoption. Some other alternatives include dat, DVC, and gitLFS; but the space is still new and relatively unexplored.

Deployment Strategies

There are many different approaches when it comes to deploying machine learning models into production and an entire book could be written on this topic. In fact, I am not sure if it exists already. The choice of deployment strategy depends totally on the business requirement and how we plan to consume the output prediction. On a very high level, it can be categorized as below-

Batch Prediction

Batch Prediction is the simplest form of machine learning deployment strategy which is used in online competitions and academics. In this strategy, you schedule the predictions to run at a particular time and output them to database/file systems.

Implementation

Below approaches can be used to implement batch predictions-

  • The simplest way is to write a program in Python and schedule it using Cron, but it requires extra effort to introduce functionalities for validating, auditing, and monitoring. However, nowadays we have many tools/approaches that can make this task simpler.
  • Writing a Spark Batch job and scheduling it in yarn and introducing logging for monitoring and retry functionalities.
  • Using tools like Perfect and Airflow which provide UI capabilities for scheduling, monitoring, and alert notifications in case of failures.
  • Platforms like Kubeflow, MLFlow, and Amazon Sagemaker also provide batch deployment and scheduling capabilities.

Web Service

The most common and widely used machine learning deployment strategy is a simple web service. It is easy to build and deploy. The web service takes input parameters and outputs the model predictions. The predictions are almost real-time and don’t require lots of resources also as they will predict one record at a time, unlike batch prediction which processes all the records at once.

Implementation

  • To implement the predictions as a web service, the simplest way is to write a service and put it in a docker container to integrate with existing products. Though this is not the sexiest solution but probably the cheapest.
  • The most common framework to implement an ML model as a service is using Flask. You can then deploy your flask application on Heroku or Azure or AWS or Google Cloud or just deploy using PythonAnywhere.
  • Another common way to implement the ML model as a service is using Django app and deploying it using Heroku/AWS/Azure/Google Cloud platforms.
  • There are a few new options like Falcon, Starlette, Sanic, FastAPI, and Tornado also talking space in this area. FastAPI along with Uvicorn server is becoming famous these days because of minimal code requirements and it automatically creates both OpenAPI (Swagger) and ReDoc documentation.

Why Online/Real-Time Predictions?

Above two approaches are widely used and almost 90% of the time you will be using either of two strategies to build and deploy your ML pipelines. However, there are a few concerns with both of these approaches-

1. Performance tuning of bulk size for batch partitioning.

2. Service exhaustion, Client starvation, Handling failures, and retries are common issues with web services. If model calls are asynchronous, this approach fails to trigger back pressure in case there is a burst of data such as during restarts. This can lead to Out of Memory failures in the model servers.

The answer to the above issues lies in the next two approaches.

Real-Time Streaming Analytics

In the last few years, the world of software has moved from Restful services to Streaming APIs, and so should the world of ML.

Hence another ML workflow that’s emerging nowadays is real-time streaming analytics, which is also known as Hot Path Analytics.

In this approach, the requests to the model/data load come as a stream (commonly as Kafka stream) of events, the model is placed right in the firehose, to run on the data as it enters the system. This creates a system that is asynchronous, fault-tolerant, replayable, and highly scalable.

The ML system in this approach is event-driven and hence it allows us to gain better model computing performance.

Implementation

  • To implement an ML system using this strategy, the most common way is to use Apache Spark or Apache Flink (both provide Python API). Both allow for easy integration of ML models written using Scikit-Learn or TensorFlow other than Spark MLlib or Flink ML.
  • If you are not comfortable with python or if there is already an existing data pipeline that is written in Java or Scala, then you can use TensorFlow Java API or third-party libraries such as MLeap or JPMML.

Automated Machine Learning

If we just train a model once and never touch it again, we’re missing out on the information more/new data could provide us.

This is especially important in environments where behaviors change quickly, so you need an ML model that can learn from new examples in something closer to real-time.

With Automated ML, you should both predict and learn in real-time.

A lot of engineering is involved in building an ML model that learns online, but the most important factor is the architecture/deployment of the model. As the model can, and will, change every second, you can’t instantiate several instances. Also, it’s not horizontally scalable and you are forced to have a single model instance that eats new data as fast as it can, spitting out sets of learned parameters behind an API. The most important part of the process (the model) is only vertically scalable. It may not even be feasible to distribute between threads.

Real-time examples of this strategy are — Uber Eats delivery estimation, LinkedIn’s connections suggestions, Airbnb’s search engines, augmented reality, virtual reality, human-computer interfaces, and self-driving cars.

Implementation

  • Sklearn library has a few algorithms that support online incremental learning using the partial_fit method, like SGDClassifier, SGDRegressor, MultinomialNB, MiniBatchKMeans, and MiniBatchDictionaryLearning.
  • Spark MLlib doesn’t have much support for online learning and has 2 ML algorithms to support online learning — StreamingLinearRegressionWithSGD and StreamingKMeans.
  • Creme also has good APIs for Online Learning.

Challenges

Online training also has some issues associated with it. As data is changing often, your ML model can be sensitive to the new data and change its behavior. Hence mandatory on the fly monitoring is required and if the change threshold is more than a certain percentage; then data behavior has to be managed properly.

For example in any recommendation engine, if one user is liking or disliking a category of data in bulk; then this behavior, if not taken care of properly can influence the results for other users. Also, chances are that this data can be a scam, so it should be removed from the training data.

Taking care of these issues/patterns in batch training is relatively easy and the misleading data patterns and outliers can be removed from training data very easily. But in Online learning, it's much harder, and creating a monitoring pipeline for such data behavior can be a big hit on performance as well due to the size of training data.

Other Variants in Deployment Strategies

There are a few other variants in deployment strategies, like ad-hoc predictions via SQL, model server (RPCs) and embedded model deployments, tiered storage without any Data Storage, and Database as model storage. All these are combinations/variants of the above four strategies. Each strategy itself is a chapter, so it's beyond the scope of this article. But the essence is that deployment strategies can be combined/molded as per the business need. For example, if data is changing frequently but you do not have the platform/environment to do online learning, then you can do batch learning (every hour/day, depending on need) parallel to the online prediction.

Monitoring ML Model Performance

Once a model is deployed and running successfully in a production environment, it is necessary to monitor how well the model is performing. Monitoring should be designed to provide early warnings of the myriad of things that can go wrong in a production environment.

Model Drift

Model Drift is described as the change in the predictive power of the ML model. In a dynamic data system where new data is being acquired very regularly, the data can change significantly over a short period of time. Therefore the data that we used to train the model in the research or production environment does not represent the data that we actually get in our live system.

Model Staleness

If we use historic data to train the models, we need to anticipate that the population, consumer behavior, economy, and its effects may not be the same in current times. So the features that were used to train the model will also change.

Negative Feedback Loops

One of the key features of live ML systems is that they tend to influence their self-behavior when they update over time which may lead to a form of analysis debt. This in turn makes it difficult to predict the behavior of an ML model before it is released into the system. These feedback loops are difficult to detect and address especially if they occur gradually over time, which may be the case when models are not updated frequently.

To avoid/treat the above issues in the Production system, there needs to be a process that measures the model’s performance against new data. If the model falls below an acceptable performance threshold, then a new process has been initiated to retrain the model with new/updated data, and that newly trained model should be deployed.

Conclusion

At the end, there is no generic strategy that fits every problem and every organization. Deciding what practices to use, and implementing them, is at the heart of what machine learning engineering is all about.

You will often see when starting with any ML project; the primary focus is given to the data and ML algorithms, but looking at how much work is involved in deciding ML infrastructure and deployment, the focus should be given to these factors as well.

If you are looking for model deployment using batch and real-time, please refer-

Thanks for the read. I hope you liked the article!! As always, please reach out for any questions/comments/feedback.

Github: https://github.com/charumakhijani LinkedIn: https://www.linkedin.com/in/charumakhijani/

Data Science
Machine Learning
Ml Model Deployment
Ml Pipeline
Real Time Analytics
Recommended from ReadMedium