{"id":22709,"date":"2021-03-26T07:25:00","date_gmt":"2021-03-26T07:25:00","guid":{"rendered":"https:\/\/www.experfy.com\/blog\/how-to-deploy-machine-learning-models\/"},"modified":"2023-08-29T07:57:44","modified_gmt":"2023-08-29T07:57:44","slug":"how-to-deploy-machine-learning-models","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/ai-ml\/how-to-deploy-machine-learning-models\/","title":{"rendered":"How To Deploy Machine Learning Models"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"22709\" class=\"elementor elementor-22709\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-4cf7f4e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4cf7f4e\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-818a618\" data-id=\"818a618\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-c1350af elementor-widget elementor-widget-text-editor\" data-id=\"c1350af\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p class=\"has-medium-font-size\">A step-by-step overview<\/p>\n<p>Jupyter notebooks are where machine learning models go to die.<\/p>\n<p>Wait\u2014 what?<\/p>\n<p id=\"0bad\">Unlike what you probably learned in University, building models in a Jupyter notebook or R Studio script is just the very beginning of the process. If your process ends with a model sitting in a notebook, those models almost certainly didn&#8217;t create value for your company (some exceptions might be it was only for analytics or you work at Netflix).<\/p>\n<p id=\"083b\">That doesn\u2019t mean that your&nbsp;models aren\u2019t excellent. I\u2019m sure they are. But it probably does mean the people paying you are not super excited by the outcome.<\/p>\n<p id=\"6a20\">In general, companies don\u2019t care about state-of-the-art models, they care about machine learning models that actually create value for their customers.<\/p>\n<p id=\"219e\">That process of going from a great model in a notebook to a model that can be integrated into part of a product or accessed by non-technical users is what I want to talk about in this post.<\/p>\n<p id=\"05e8\">So, if you want your models to stop dying in notebooks and actually create value, read on.<\/p>\n\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e17ae28 elementor-widget elementor-widget-heading\" data-id=\"e17ae28\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Models are Code and Code Needs to be Trusted<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-907d41c elementor-widget elementor-widget-text-editor\" data-id=\"907d41c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"870a\">Your first step will be getting your code to a quality level that you can trust to put into production. I want to highlight 3 important things that should happen in this step: have a good README, write clean functions, and test your code.<\/p>\n<p id=\"c713\">Good READMEs are so important. They should allow anyone (including yourself) to easily see why this code was written, what assumptions were made, what constraints existed, and how to use the code. For machine learning code you should also describe and\/or link to experiments that were run so people can view the process of creating your models. Don\u2019t be afraid to include images, GIFs, and videos in your READMEs. They can help make it much easier to understand.<\/p>\n<p id=\"0380\">This post will not teach you everything you need to know to write clean functions. But some important items to remember:<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e653767 elementor-widget elementor-widget-text-editor\" data-id=\"e653767\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<ul><li>Use easy to understand function and variable names<\/li><li>Functions should only do one thing<\/li><li>Include documentation and use types<\/li><li>Use a style guide such as PEP8<\/li><\/ul>\n<p id=\"03cf\">Last, but not least, you need to test your code! Tests allow you to have confidence that your code functions as expected and that when you make changes, you didn&#8217;t actually break anything. The simplest place to start is with unit tests. For Python, look at the pytest and unittest libraries to manage your tests.<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7d496ed elementor-widget elementor-widget-heading\" data-id=\"7d496ed\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Containerize Your Code<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f0067c3 elementor-widget elementor-widget-text-editor\" data-id=\"f0067c3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"5846\">You\u2019ve probably heard of Docker. Docker is by far the most popular way developers containerized their code. But what is Docker? On their website, Docker states:<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f61ab8c elementor-widget elementor-widget-text-editor\" data-id=\"f61ab8c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<blockquote class=\"wp-block-quote\"><p>Containers are a standardized unit of software that allows developers to isolate their app from its environment, solving the \u201cit works on my machine\u201d headache. For millions of developers today, Docker is the de facto standard to build and share containerized apps.<\/p><\/blockquote>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c27096a elementor-widget elementor-widget-text-editor\" data-id=\"c27096a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"1b20\">So \u2014 Docker is a way to containerize your applications and containerization isolates your app from its environment. In simple terms that means that if you can get your code running inside Docker on your local machine, it should then run anywhere. This is increasingly important as many turn to <a href=\"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/three-strategies-for-securing-containerized-applications-in-the-cloud\/\" target=\"_blank\" rel=\"noreferrer noopener\">cloud<\/a> providers such as AWS to deploy their applications.<\/p>\n<p id=\"ff7e\">To get started, download Docker, create a DockerFile which tells Docker how to build the environment for your application, build your Docker image from your DockerFile, and finally run your Docker image.<\/p>\n<p id=\"40e1\">Each of those steps has some complexity to it, but just take a look at the Getting Started tutorial on Docker\u2019s website and you\u2019ll be well on your way to containerizing your code inside Docker.<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8f1c67e elementor-widget elementor-widget-heading\" data-id=\"8f1c67e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Make Your Model Easy to Access<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-84be3e5 elementor-widget elementor-widget-image\" data-id=\"84be3e5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw.jpeg\" class=\"attachment-large size-large wp-image-19033\" alt=\"How To Deploy Machine Learning Models&quot;\/&gt;Image Created by Author\" srcset=\"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw.jpeg 960w, https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw-300x169.jpeg 300w, https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw-768x432.jpeg 768w, https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw-610x343.jpeg 610w, https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/1K-xR4Rzoi5gTxMTojeNcSw-750x422.jpeg 750w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-132ad60 elementor-widget elementor-widget-text-editor\" data-id=\"132ad60\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"1589\">You now have clean, containerized code, but no easy way for your users to interact with your machine learning model.<\/p>\n<p id=\"9423\">The simplest way to fix this problem is to put your model behind an API and expose it via a webpage.<\/p>\n<p id=\"0689\">An application program interface or API is a way you can allow applications to interface with your code, or in our case, models. For example, if you create an API for a classification model, your API would take in the necessary data to make predictions (usually in JSON format) and then return the prediction based on those data. APIs are useful for many reasons, but a big one is it creates separation between your code and your user interface. You generally interact with APIs via web requests.<\/p>\n<p id=\"c880\">Python has two awesome libraries to make this process pretty simple. First, FastAPI makes it straightforward to create an API for your model. Second, the requests library in Python makes it easy to communicate with your APIs.<\/p>\n<p id=\"77df\">Once you have an API for your model, you then need to create a front-end application for your users. Many data scientists fear this step because they have limited or no knowledge of how to create websites. Fortunately, there is a solution! The Streamlit Python library. Streamlit is a library that allows you to create web applications in Python without having to know anything about creating websites. They also have great documentation, so go check it out to get started building your web app for your model.<\/p>\n<p id=\"05b3\">At this point, you now have your backend API code, your front-end web application, and web requests which connect the two. Treat these as separate applications to be containerized using Docker.<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-72b87ac elementor-widget elementor-widget-heading\" data-id=\"72b87ac\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Deploy to the Cloud<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0388f07 elementor-widget elementor-widget-text-editor\" data-id=\"0388f07\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"2f25\">Up to this point, all of your work has been on your local machine. If things have gone well you have a front-end web app running on your machine that allows you to access your machine learning model predictions.<\/p>\n<p id=\"6e1e\">The problem, though, is that only&nbsp;<em>you&nbsp;<\/em>have access to this web app!<\/p>\n<p id=\"a805\">To fix this, we will deploy your application to the cloud and because you have already containerized your code, this process is actually pretty easy.<\/p>\n<p id=\"9af8\">The three main cloud providers are AWS, Google Cloud, and Microsoft Azure. They all provide pretty similar functionality and for learning, you can\u2019t really pick wrong. I would suggest choosing the cloud which currently has the most free credits for you to use.<\/p>\n<p id=\"beea\">No matter which cloud you select, I would start by looking at their platform as a service offerings. On Google you have Google App Engine, for Amazon, you have Elastic Beanstalk and Microsoft has Azure App Services. All of these services make it easy for you to deploy a Docker container to the cloud.<\/p>\n<p id=\"bfcf\">Once your web app and API containers are deployed to these platforms, you will then have a public URL you or anyone can access to interact with your machine learning model. Congratulations!<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0eaf55c elementor-widget elementor-widget-heading\" data-id=\"0eaf55c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">How to Learn More<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cf8dbed elementor-widget elementor-widget-text-editor\" data-id=\"cf8dbed\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"842d\">I hope this overview on how to deploy machine learning models helped you understand the basic steps to deploying your models. There is almost infinite complexity when it comes to this process and if you\u2019d like to get an even deeper overview, check out this&nbsp;<a href=\"https:\/\/bit.ly\/3ltZA4s\" rel=\"noopener\">free course<\/a>.<\/p>\n<p>This article was published at <a href=\"https:\/\/towardsdatascience.com\/how-to-deploy-machine-learning-models-ec50d3daf365\" target=\"_blank\" rel=\"noreferrer noopener\">Towards Data Science<\/a>.<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>A step-by-step overview Jupyter notebooks are where machine learning models go to die. Wait\u2014 what? Unlike what you probably learned in University, building models in a Jupyter notebook or R Studio script is just the very beginning of the process. If your process ends with a model sitting in a notebook, those models almost certainly<\/p>\n","protected":false},"author":1090,"featured_media":19034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[183],"tags":[97,92,1029,408],"ppma_author":[3882],"class_list":["post-22709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai-ml","tag-artificial-intelligence","tag-machine-learning","tag-model-deployment","tag-programming"],"authors":[{"term_id":3882,"user_id":1090,"is_guest":0,"slug":"tyler-folkman","display_name":"Tyler Folkman","avatar_url":"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2021\/05\/Tyler-Folkman-150x150.jpeg","user_url":"https:\/\/learningwithdata.com\/","last_name":"Folkman","first_name":"Tyler","job_title":"","description":"Tyler Folkman is Head of Artificial Intelligence at Branded Entertainment Network."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/22709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/1090"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=22709"}],"version-history":[{"count":4,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/22709\/revisions"}],"predecessor-version":[{"id":31779,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/22709\/revisions\/31779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/19034"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=22709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=22709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=22709"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=22709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}