Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Machine Learning Model Serving Patterns and Best Practices

You're reading from   Machine Learning Model Serving Patterns and Best Practices A definitive guide to deploying, monitoring, and providing accessibility to ML models in production

Arrow left icon
Product type Paperback
Published in Dec 2022
Publisher Packt
ISBN-13 9781803249902
Length 336 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Md Johirul Islam Md Johirul Islam
Author Profile Icon Md Johirul Islam
Md Johirul Islam
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1:Introduction to Model Serving
2. Chapter 1: Introducing Model Serving FREE CHAPTER 3. Chapter 2: Introducing Model Serving Patterns 4. Part 2:Patterns and Best Practices of Model Serving
5. Chapter 3: Stateless Model Serving 6. Chapter 4: Continuous Model Evaluation 7. Chapter 5: Keyed Prediction 8. Chapter 6: Batch Model Serving 9. Chapter 7: Online Learning Model Serving 10. Chapter 8: Two-Phase Model Serving 11. Chapter 9: Pipeline Pattern Model Serving 12. Chapter 10: Ensemble Model Serving Pattern 13. Chapter 11: Business Logic Pattern 14. Part 3:Introduction to Tools for Model Serving
15. Chapter 12: Exploring TensorFlow Serving 16. Chapter 13: Using Ray Serve 17. Chapter 14: Using BentoML 18. Part 4:Exploring Cloud Solutions
19. Chapter 15: Serving ML Models using a Fully Managed AWS Sagemaker Cloud Solution 20. Index 21. Other Books You May Enjoy

What are models?

There are a lot of definitions of models from the perspective of various domains. When we define a model or use the term model in this book, we will consider it in the context of ML. A model in ML can be seen as a function that has been fine-tuned through training, using some well-engineered data so that the function can recognize and distinguish patterns in unseen data.

Depending on the problem and business goal, a different model can be used, for example, a linear regression model, a logistic regression model, a naive Bayes model, or a decision tree model. These models’ underlying logical representations are different from each other. However, we use the generic term model and the problem domain and name of the ML algorithm to give us a clear picture of what the model is, how it was trained, and how the model is represented. For example, if we are told that the model is for linear regression, then we know that it was trained by minimizing a cost function iteratively using the training data, and it can be saved by storing the regression parameters’ coefficients and intercepts. Similarly, other models will have different algorithms for training and storing. For a deep learning model, we might have to use forward propagation and backward propagation for training, and for storing we might have to store the weights and biases of all the layers.

The trained model can be stored in different formats to load later for serving and inference. Some popular formats in which to save a model are as follows:

  • ONNX
  • YAML
  • Protobuf
  • Pickle
  • JSON
  • H5
  • TFJS
  • Joblib

However, model-serving tools usually require the models to be saved in a particular format. So, they provide a function to save the model in its desired format. There are also tools and libraries to convert models from one format to another. For example, in Figure 1.2, we see that an AlexNet model that is pre-trained in PyTorch is loaded and exported to ONNX format in a file named alexnet.onnx.

PyTorch files

It’s worth knowing that PyTorch saves the model using the Python pickle (https://docs.python.org/3/library/pickle.html) library. For further reading on PyTorch strategies for saving and loading models, please check out their official documentation: https://pytorch.org/tutorials/beginner/saving_loading_models.html.

Figure 1.2 – Example code converting a PyTorch pre-trained AlexNet model to ONNX format

Figure 1.2 – Example code converting a PyTorch pre-trained AlexNet model to ONNX format

Note

Figure 1.2 is an example from the PyTorch official website: https://pytorch.org/docs/stable/onnx.html#example-alexnet-from-pytorch-to-onnx.

Now we should have a good idea about models and how each model is represented and stored. The following section will introduce us to model serving.

You have been reading a chapter from
Machine Learning Model Serving Patterns and Best Practices
Published in: Dec 2022
Publisher: Packt
ISBN-13: 9781803249902
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime