Deployment Basics
- This lesson explains how machine learning models are deployed into production systems and used in real applications.
Model Saving
Once training is complete, we save the model.
Save Model (TensorFlow / Keras)
Recommended Format (.keras)
model.save("my_model.keras")
This saves:
Architecture
Weights
Optimizer state
Older Format (.h5)
model.save("my_model.h5")
Load Model Later
from tensorflow.keras.models import load_model
model = load_model("my_model.keras")
Now model is ready for predictions.
Basic Deployment Idea
Deployment Architecture:
User → Web/App Interface → Backend Server → Model → Prediction → User
Step-by-Step Deployment Flow
User enters input (form/image/text)
Backend receives data
Preprocess input
Load trained model
Generate prediction
Return result to userSimple Local Deployment Idea (Streamlit)
Example: Deploy ANN Model
Step 1: Install Streamlit
Build a Customer Churn Prediction Web App using Streamlit and TensorFlow Keras
This Python example demonstrates how to create a web application for predicting customer churn using Streamlit and a pre-trained TensorFlow Keras model. The code loads the trained model, takes user inputs through the Streamlit interface, makes predictions, and displays whether the customer is likely to churn or stay. The app can be run locally using streamlit run app.py.
pip install streamlit
Step 2: Create app.py
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
# Load model
model = load_model("my_model.keras")
st.title("Churn Prediction App")
# Example inputs
feature1 = st.number_input("Tenure")
feature2 = st.number_input("Monthly Charges")
if st.button("Predict"):
data = np.array([[feature1, feature2]])
prediction = model.predict(data)
if prediction > 0.5:
st.write("Customer will Churn")
else:
st.write("Customer will Stay")
Step 3: Run App
streamlit run app.py
Browser opens automatically.
Types of Deployment
Basic Deployment Architecture
Local Deployment
Model runs on your laptop.
API Deployment
Frontend → API (Flask/FastAPI) → Model → JSON Response
Important Deployment Considerations
Save preprocessing steps (scaler, encoder)
Use same feature order
Handle missing values
Secure API
Monitor performanceExample: Saving Scaler
import joblib
joblib.dump(scaler, "scaler.pkl")
Load later:
scaler = joblib.load("scaler.pkl")
Real-World Deployment Flow
Train Model
↓
Save Model + Scaler
↓
Create API
↓
Test Locally
↓
Deploy to Cloud
↓
Monitor Performance