ML Notes
HubAI vs ML vs Deep Learning — Machine LearningApplications of Machine LearningHistory of Machine LearningMachine Learning RoadmapWhat is Machine Learning?Calculus for Machine LearningData Preprocessing BasicsLinear Algebra for Machine LearningProbability Basics for Machine LearningPython for Machine LearningStatistics Basics for Machine LearningBias vs Variance TradeoffModel Evaluation OverviewOverfitting and UnderfittingReinforcement Learning Fundamentals — Machine LearningSupervised LearningTraining vs Testing DataTypes of Machine LearningUnsupervised LearningData Cleaning — Machine LearningData Collection for Machine LearningData Visualization for MLEncoding Categorical DataFeature EngineeringFeature ScalingHandling Missing ValuesTrain-Test SplitElastic Net RegressionLasso Regression (L1 Regularization)Multiple Linear RegressionPolynomial RegressionIntroduction to RegressionRegression ProjectsRidge Regression (L2 Regularization)Simple Linear RegressionIntroduction to ClassificationClassification ProjectsDecision TreesK-Nearest Neighbors (KNN)Logistic RegressionNaive Bayes ClassifierRandom Forest ClassificationSupport Vector Machine (SVM)XGBoost for ClassificationClustering in Machine Learning: Complete Guide to Unsupervised LearningClustering Projects: Real-World Applications and Implementation GuidesDBSCAN ClusteringGaussian Mixture Models (GMM): Probabilistic Clustering for Advanced Data AnalysisHierarchical ClusteringK-Means ClusteringDimensionality Reduction: Comprehensive Guide to Feature Reduction TechniquesFeature Selection Techniques: Choosing Informative Variables for ML ModelsLinear Discriminant Analysis (LDA): Supervised Dimensionality ReductionPrincipal Component Analysis (PCA)t-SNE: Non-Linear Dimensionality Reduction for Data VisualizationUMAP: Uniform Manifold Approximation and Projection for Modern Data VisualizationAdaBoost: Adaptive Boosting Algorithm Explained with ExamplesBagging: Bootstrap Aggregating for Reducing Model VarianceBoosting: Sequential Ensemble Learning for Bias and Variance ReductionEnsemble Learning: Complete Guide to Combining ML Models for Better PredictionsGradient Boosting: Building Powerful ML Models Through Sequential OptimizationRandom Forest: Complete Guide to Tree Ensembles and Feature ImportanceStacking: Advanced Ensemble Method Using Meta-Learners for Superior ML PredictionsActivation Functions in Neural NetworksBackpropagation: Complete Algorithm for Training Neural NetworksIntroduction to Deep Learning: Neural Networks, Architectures, and ApplicationsDeep Learning Projects: Hands-On Applications and Case StudiesNeural Networks Basics: Perceptron, Architecture, and TrainingPerceptron: Foundational Single-Neuron Classifier for Binary ClassificationPyTorch Basics: Building and Training Neural NetworksTensorFlow Basics: Building and Training Neural NetworksIntroduction to Natural Language Processing: Fundamentals and ApplicationsNLP Projects: Practical Applications from Sentiment Analysis to Machine TranslationStemming and Lemmatization: Reducing Words to Root Forms in NLPText Preprocessing: Essential Steps for NLP ApplicationsTF-IDF: Term Frequency-Inverse Document Frequency for Text Feature ExtractionTokenization in NLP: Breaking Text into Meaningful UnitsTransformers: The Architecture Behind Modern NLP ModelsWord Embeddings: Representing Words as Vectors for NLPIntroduction to Computer VisionComputer Vision ProjectsImage Classification: Building and Deploying CNN ModelsImage Processing BasicsImage Segmentation — Machine LearningObject DetectionOpenCV BasicsAgents and Environments in RLDeep Q-Networks (DQN)Policy Gradient Methods — Machine LearningQ-Learning AlgorithmReinforcement Learning: IntroductionReinforcement Learning ProjectsAccuracy, Precision, Recall: Classification Metrics Explained for ML InterviewConfusion Matrix: Complete Guide to Classification EvaluationCross-Validation: Comprehensive Guide to Robust Model EvaluationF1 Score: Balanced Classification Metric for Imbalanced DataGrid Search: Complete Guide to Hyperparameter Tuning in Machine LearningHyperparameter Tuning: Complete Guide to Optimizing Machine Learning ModelsModel Selection: Choosing the Best Algorithm for Your ML ProblemROC and AUC: Complete Guide to Classification Threshold EvaluationBuild an AI Assistant with LLMsBuild a Conversational ChatbotCustomer Churn PredictionFace Recognition System — Machine LearningHouse Price PredictionRecommendation System ProjectSentiment Analysis Project — Machine LearningSpam Email ClassifierML Case Study Questions for InterviewsML Coding Questions for Technical InterviewsMachine Learning Interview CheatsheetEssential Machine Learning Interview QuestionsStatistics for Machine Learning InterviewsDatasets for ML PracticeImportant ML FormulasMachine Learning GlossaryRecommended ML Books and CoursesCI/CD for Machine LearningDocker for Machine LearningFastAPI for Machine Learning APIsFlask API for Machine LearningMLOps: IntroductionModel Deployment StrategiesML Model Monitoring & Observability
Containerizing ML models and workflows using Docker for reproducibility, scalability, and deployment.
Docker enables reproducible, scalable ML deployments by packaging models, dependencies, and configurations into containers.
Why Docker for ML?
Problems Docker solves:
- Dependency hell: "Works on my machine" syndrome
- Reproducibility: Exact same environment everywhere
- Scalability: Easy to deploy multiple instances
- Isolation: Different projects don't interfere
- Consistency: Dev, staging, prod identical
Creating ML Docker Images
Basic Dockerfile
Example
# Use Python base image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy ML code
COPY src/ ./src/
COPY models/ ./models/
# Expose port for API
EXPOSE 8000
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV MODEL_PATH="/app/models/model.pkl"
# Entry point
CMD ["python", "src/serve.py"]
requirements.txt
Example
numpy==1.21.0
pandas==1.3.0
scikit-learn==0.24.2
xgboost==1.4.2
flask==2.0.1
gunicorn==20.1.0
Building & Running
bash example
# Build image
docker build -t ml-model:1.0 .
# Run container
docker run -p 8000:8000 ml-model:1.0
# Run with environment variables
docker run -p 8000:8000 \
-e MODEL_PATH="/models/custom.pkl" \
ml-model:1.0
# Interactive mode
docker run -it ml-model:1.0 bash
# Mount local directory
docker run -v /local/data:/app/data ml-model:1.0ML Model Serving with Flask
Example
# src/serve.py
from flask import Flask, request, jsonify
import pickle
import numpy as np
import logging
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load model at startup
MODEL_PATH = os.environ.get('MODEL_PATH', 'models/model.pkl')
with open(MODEL_PATH, 'rb') as f:
model = pickle.load(f)
logger.info(f"Loaded model from {MODEL_PATH}")
@app.route('/health', methods=['GET'])
def health():
"""Health check endpoint"""
return jsonify({'status': 'healthy'}), 200
@app.route('/predict', methods=['POST'])
def predict():
"""Prediction endpoint"""
try:
data = request.get_json()
if not data:
return jsonify({'error': 'Empty request body'}), 400
# Convert to array
X = np.array(data['features']).reshape(1, -1)
# Predict
prediction = model.predict(X)[0]
return jsonify({
'prediction': float(prediction),
'status': 'success'
}), 200
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/batch_predict', methods=['POST'])
def batch_predict():
"""Batch prediction endpoint"""
try:
data = request.get_json()
features = data['features'] # List of feature vectors
X = np.array(features)
predictions = model.predict(X)
return jsonify({
'predictions': predictions.tolist(),
'count': len(predictions)
}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=False)
Docker Compose for Complete ML Stack
yaml example
# docker-compose.yml
version: '3.8'
services:
# ML Model API
ml-api:
build: ./ml-service
ports:
- "8000:8000"
environment:
- MODEL_PATH=/models/model.pkl
- LOG_LEVEL=INFO
volumes:
- ./models:/models
- ./logs:/app/logs
depends_on:
- redis
- postgres
restart: unless-stopped
# Cache layer
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
restart: unless-stopped
# Database for predictions
postgres:
image: postgres:14-alpine
environment:
- POSTGRES_DB=ml_predictions
- POSTGRES_USER=mluser
- POSTGRES_PASSWORD=secure_password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
# Monitoring
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
restart: unless-stopped
# Visualization
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
depends_on:
- prometheus
restart: unless-stopped
volumes:
redis_data:
postgres_data:Multi-stage Build for Optimization
Example
# Multi-stage: Reduce final image size
FROM python:3.9 as builder
WORKDIR /build
COPY requirements.txt .
# Install in separate layer
RUN pip install --user --no-cache-dir -r requirements.txt
# Final stage
FROM python:3.9-slim
WORKDIR /app
# Copy only Python packages from builder
COPY --from=builder /root/.local /root/.local
# Copy application code
COPY src/ ./src/
COPY models/ ./models/
ENV PATH=/root/.local/bin:$PATH
EXPOSE 8000
CMD ["python", "src/serve.py"]
# Result: Final image ~500MB instead of ~2GB
Testing Docker Locally
Example
# Build with tag
docker build -t ml-model:latest .
# Run with logging
docker run -it ml-model:latest
# Test from another terminal
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"features": [1.0, 2.0, 3.0]}'
# Get logs
docker logs <container-id>
# Interactive debugging
docker run -it ml-model:latest /bin/bash
python -c "import model; print(model.__version__)"
Deploying to Docker Registry
bash example
# Tag for Docker Hub
docker tag ml-model:latest username/ml-model:1.0
# Push to registry
docker push username/ml-model:1.0
# Pull from registry
docker pull username/ml-model:1.0
# Run from registry
docker run -p 8000:8000 username/ml-model:1.0
# AWS ECR
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
docker tag ml-model:latest <account>.dkr.ecr.us-east-1.amazonaws.com/ml-model:latest
docker push <account>.dkr.ecr.us-east-1.amazonaws.com/ml-model:latestRunning ML Training in Docker
Example
# Dockerfile for training
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
COPY data/ ./data/
# Volume for output models
VOLUME /models
ENV PYTHONUNBUFFERED=1
# Train model, save to /models
CMD ["python", "src/train.py", "--output", "/models/model.pkl"]
bash example
# Run training
docker run \
-v $(pwd)/models:/models \
-e DATA_PATH=/app/data \
ml-train:latest
# Check output
ls -lh models/model.pklHealth Checks in Docker
Example
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
CMD ["python", "src/serve.py"]
bash example
# Docker will automatically restart unhealthy containers
docker ps # Shows (healthy) or (unhealthy) statusBest Practices
dockerfile example
# ✓ Good: Minimal base image
FROM python:3.9-slim
# ✓ Good: Specific versions
RUN pip install numpy==1.21.0
# ✗ Bad: Large base image
FROM ubuntu:20.04
# ✗ Bad: Latest versions (non-deterministic)
RUN pip install numpy
# ✓ Good: Multi-stage build
FROM python:3.9 as builder
# ... build dependencies ...
FROM python:3.9-slim
COPY --from=builder /build /app
# ✓ Good: Non-root user
RUN useradd -m mluser
USER mluser
# ✓ Good: EXPOSE port
EXPOSE 8000
# ✗ Bad: Running as root (security issue)Troubleshooting
bash example
# Check image layers
docker history ml-model:latest
# View image size
docker images | grep ml-model
# Inspect running container
docker inspect <container-id>
# Check resource usage
docker stats
# Remove dangling images
docker image prune
# Build without cache
docker build --no-cache -t ml-model:latest .
# Debug by running interactive shell
docker run -it ml-model:latest bash
python -c "import your_module; print(your_module.__file__)"Summary
Docker for ML:
- Ensures reproducibility
- Enables scalability
- Simplifies deployment
- Isolates environments
- Integrates with orchestration (Kubernetes)
Continue the lesson path
Study next in this module
01MlopsCI/CD for Machine LearningImplementing continuous integration and continuous deployment pipelines for ML models including testing, validation, and automated deployment.02MlopsFastAPI for Machine Learning APIsBuilding high-performance ML APIs with FastAPI, async support, and automatic documentation.03MlopsFlask API for Machine LearningBuilding ML model serving APIs with Flask, request handling, and deployment.04MlopsMLOps: IntroductionIntroduction to MLOps: productionizing ML systems, monitoring, deployment, and lifecycle management.