Source: Deep Learning on Medium
AWS Lambda for machine learning engineers
( This tutorial covers the steps in the deployment of machine learning model in Lambda )
Deployment of the machine learning model is a tedious task but with services like AWS lambda, it does not have to be anymore. AWS lambda falls under the concept of serverless computing which means that we can deploy our model easily without the headache of managing servers, runtimes, and infrastructures. Lambda provides runtimes for programming languages like Python, Ruby and more.
Utilizing this benefit of serverless computing, we can easily deploy our machine learning models like classification model, regression model and much more at ease quickly and efficiently. We get to choose the processing power of our own choice and runtimes that apt requirements of our machine learning model.
One drawback of AWS lambda is that there is a limit in size of dependencies our model can have. For example libraries like spacy with the English language, the model can take up to a GB of RAM. But most of the models like classification models can easily be deployed since they have dependencies like SkLearn, NumPy or Pandas. And the good news is that lambda provides this stack of data science libraries out of the box.
Wait! If you want to take a quick refresher in pandas, don’t forget to read this short pandas for busy data scientists tutorial.
In this tutorial, I will cover the concepts of how to set up a machine learning model in Lambda. I will not take a walkthrough of each step building the model but, I will try to give to concepts so that you can try on your own. The benefit of this is that actual learning and understanding comes from doing yourself.
Concepts of Layers:
Lambda has the concept of layers, which means that we can add layers into our lambda function. Each layer can be a dependency or group of dependencies depending upon our necessities. For example, pandas and numpy can be one layer or they can be different layers on their own. While creating a lambda function, we get to opt which layer we want to add in our lambda function and it helps to add the same layer in different lambda functions.
Steps in creating and using these layers in lambda functions:
- Create a virtual environment in the local computer
- Pip install all required libraries
- Get inside site-packages directories of the local virtual environment
- Zip all the dependencies
- If you know what you are doing then, you can also remove unnecessary modules to reduce dependency sizes
- Upload dependencies to S3
- Create a layer from lambda dashboard
- Create a new lambda function and provide ARN to that layer to add it as a layer.
- In your Lambda function, add “/opt/” as a path
- Import the libraries
- Perform Classification
And that’s it. If you want your model to be accessible from outside the world then,
- Create a new API gateway
- Create a new resource
- Add HTTP method, most probably POST request with some parameters
- Point that route to Lambda function
Furthermore, if you are actually deploying a machine learning model in production, then you should add Cloudwatch to see logs of your application so that you will know if you encounter some errors.
If you want to deploy your machine learning model in conventional way, don’t forget to also check out my deploy your first machine learning model using flask tutorial.
I also write about other topics like recommendation systems, natural language processing and many more. If you are also exploring these topics, don’t forget to visit my profile and see if there are any posts that interest you.