Sub topic 1
From a trained model to a service
Prerequisites
This chapter assumes you know the following:
- Basic Python, including functions and classes.
- How to train and call a scikit-learn style model with
fitandpredict. - What an HTTP request, a response, and JSON are.
- How to run commands in a terminal.
Training versus serving
Training is the work of fitting a model to data. It is slow, it reads a large dataset, and it usually runs once, or on a schedule. Serving is the work of using a finished model to answer requests. It is fast per request, it handles one input or a small batch, and it runs for as long as other programs need predictions. Serving is also called inference.
The two jobs have different needs:
| Concern | Training | Serving |
|---|---|---|
| Input | A full dataset | One request at a time, or a small batch |
| Duration | Minutes to days | Milliseconds per request |
| Lifetime | Finishes and exits | Stays running |
| Main risk | Poor accuracy | Slow responses, bad input, downtime |
Because of these differences, training code and serving code live in separate programs. Training produces a file. Serving loads that file and answers requests.
Why an HTTP service
A model inside a notebook helps only the person running the notebook. Other programs, such as a web backend or a mobile app, need a stable way to ask for predictions. An HTTP service gives them one. They send JSON to a URL and get JSON back. They need no Python, no scikit-learn, and no copy of the model.
This also keeps the model in one place. When you ship a better model, you replace it in the service, and every caller benefits.
The shape of the service
The service you build in this tutorial has four parts:
- A model file written by the training step.
- A request and response contract that says what callers send and receive.
- Validation that rejects bad input before it reaches the model.
- An endpoint that loads the model once and returns predictions.
The following diagram shows how a request moves through them.
The model you will serve
The examples use the iris dataset, which has four flower measurements per row and three classes. The model is a small random forest. It is a toy, but the serving steps are the same for a larger model.
The training script below fits the model and saves it. The next chapter explains the saving step.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import joblib
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier(n_estimators=100, random_state=0)
model.fit(X, y)
joblib.dump(model, "model.joblib")Run it once. It creates model.joblib in the current directory. From now on, the service reads this file and never retrains.
Common mistakes at this stage
- Training inside the service. Fitting at startup makes the service slow to start and can give a different model each time.
- Serving from a notebook. State in a notebook is hard to reproduce. Keep serving code in a plain
.pyfile. - Mismatched environments. A model saved with one scikit-learn version may fail or behave differently under another. Record the library versions used in training.
Exercise
Give two differences between training and serving, and explain why the service loads a saved file instead of training at startup.
Show answer
Training reads a full dataset and finishes, while serving handles individual requests and keeps running. Training is slow and serving must answer in milliseconds. The service loads a saved file because training at startup would delay startup and could produce a different model on each start. A saved file gives the same model every time.