DocumentationNeurondB Documentation

ML Engine

Overview

The ML Engine provides a comprehensive machine learning suite directly in PostgreSQL, enabling you to train, deploy, and serve ML models without data movement. Powered by ONNX Runtime, the ML Engine supports classification, regression, clustering, and AutoML capabilities with automatic GPU acceleration when available.

Key Features

  • In-Database Training: Train models directly on PostgreSQL data
  • ONNX Model Deployment: Deploy pre-trained models from PyTorch, TensorFlow, scikit-learn
  • Real-Time Inference: Sub-millisecond predictions with batch processing support
  • AutoML: Automated hyperparameter tuning and model selection
  • Model Versioning: A/B testing, rollback, and model lifecycle management
  • GPU Acceleration: Automatic GPU offload for ONNX models with CUDA/ROCm support

Supported Algorithms

CategoryAlgorithms
ClassificationRandom Forest, Gradient Boosting, SVM, Logistic Regression, Neural Networks
RegressionLinear Regression, Ridge, Lasso, Gradient Boosting, Random Forest
ClusteringK-Means, DBSCAN, Hierarchical Clustering
Time SeriesARIMA, Prophet, LSTM, GRU
Dimensionality ReductionPCA, t-SNE, UMAP
Anomaly DetectionIsolation Forest, One-Class SVM, Autoencoders

ML Capabilities

Classification

Build classification models for binary and multi-class problems. Supports various algorithms including Random Forest, Gradient Boosting, SVM, and neural networks.

Train and use classification model

-- Train a Random Forest classifier
SELECT train_random_forest_classifier(
  table_name => 'customer_data',
  features_col => 'features',
  label_col => 'churn_label',
  n_trees => 100,
  max_depth => 10
);

-- Make predictions
SELECT 
  customer_id,
  predict_classification('customer_churn_model', features) AS predicted_churn,
  predict_classification_proba('customer_churn_model', features) AS probabilities
FROM customer_data
WHERE created_at > NOW() - INTERVAL '30 days';

Regression

Predict continuous values using regression models. Ideal for forecasting, price prediction, and numerical value estimation.

Train and use regression model

-- Train a gradient boosting regressor
SELECT train_gradient_boosting_regressor(
  table_name => 'sales_data',
  features_col => 'features',
  target_col => 'revenue',
  n_estimators => 200,
  learning_rate => 0.1
);

-- Predict revenue
SELECT 
  product_id,
  predict_regression('revenue_model', features) AS predicted_revenue
FROM products
WHERE category = 'electronics';

Clustering

Group similar data points together using clustering algorithms. Useful for customer segmentation, anomaly detection, and data exploration.

K-means clustering

-- Perform K-means clustering
SELECT cluster_kmeans(
  table_name => 'customer_features',
  vector_column => 'embedding',
  k => 5,
  max_iter => 100
);

-- Assign cluster labels
SELECT 
  customer_id,
  assign_cluster('customer_segments', embedding) AS cluster_id
FROM customers;

AutoML

Automatically select the best model and hyperparameters for your dataset. Saves time and improves model performance through systematic search.

AutoML training

-- Run AutoML for classification
SELECT automl_classify(
  table_name => 'training_data',
  features_col => 'features',
  label_col => 'target',
  max_trials => 50,
  time_budget_seconds => 3600
);

-- Best model is automatically selected and deployed
SELECT predict_classification('automl_best_model', features)
FROM test_data;

Feature Engineering

Built-in feature engineering capabilities including scaling, normalization, encoding, and feature selection.

Feature engineering

-- Create feature store
CREATE TABLE feature_store AS
SELECT 
  customer_id,
  neurondb_normalize_features(
    ARRAY[
      age,
      purchase_frequency,
      avg_order_value,
      days_since_last_purchase
    ]
  ) AS features
FROM customers;

-- Use features for training
SELECT train_random_forest_classifier(
  'feature_store',
  'features',
  'churn_label'
);

ONNX Integration

Deploy models trained in PyTorch, TensorFlow, scikit-learn, or any ONNX-compatible framework directly in PostgreSQL. ONNX Runtime provides optimized inference with automatic GPU acceleration.

Deploying ONNX Models

Deploy ONNX model

-- Deploy a pre-trained ONNX model
SELECT deploy_onnx_model(
  model_name => 'sentiment_classifier',
  model_path => '/path/to/model.onnx',
  input_shape => ARRAY[1, 768],
  output_shape => ARRAY[1, 2],
  gpu_enabled => true
);

-- Use the model for inference
SELECT 
  text_id,
  predict_onnx('sentiment_classifier', embedding) AS sentiment_score
FROM text_data;

Model Conversion

Convert models from popular frameworks to ONNX format:

  • PyTorch: torch.onnx.export()
  • TensorFlow: tf2onnx.convert
  • scikit-learn: skl2onnx.convert_sklearn()
  • Keras: keras2onnx.convert_keras()

GPU Acceleration

ONNX Runtime automatically uses GPU execution providers (CUDA, ROCm, TensorRT) when available, providing 10-50x speedup for deep learning models.

GPU-accelerated inference

-- Enable GPU for ONNX models
SET neurondb.gpu_enabled = on;
SET neurondb.gpu_device = 0;

-- Deploy with GPU support
SELECT deploy_onnx_model(
  'image_classifier',
  '/path/to/resnet50.onnx',
  gpu_enabled => true,
  execution_provider => 'CUDAExecutionProvider'
);

-- Batch inference with GPU
SELECT predict_onnx_batch('image_classifier', image_embeddings)
FROM images
WHERE batch_id = 123;

Model Management

Model Versioning

Track model versions, compare performance, and rollback to previous versions when needed.

Model versioning

-- Deploy new model version
SELECT deploy_model_version(
  model_name => 'churn_predictor',
  version => 'v2.1',
  model_path => '/path/to/v2.1.onnx',
  metadata => '{"accuracy": 0.94, "f1_score": 0.91}'::jsonb
);

-- List all versions
SELECT * FROM neurondb_model_versions
WHERE model_name = 'churn_predictor'
ORDER BY created_at DESC;

-- Rollback to previous version
SELECT set_model_version('churn_predictor', 'v2.0');

A/B Testing

Test multiple model versions simultaneously and route traffic based on performance metrics.

A/B testing

-- Set up A/B test
SELECT setup_ab_test(
  model_name => 'recommendation_engine',
  variant_a => 'v1.0',
  variant_b => 'v2.0',
  traffic_split => 0.5  -- 50/50 split
);

-- Models automatically route based on configuration
SELECT predict_classification('recommendation_engine', features)
FROM user_interactions;

Model Monitoring

Monitor model performance, track predictions, and detect data drift.

Model monitoring

-- Track predictions
SELECT log_prediction(
  model_name => 'fraud_detector',
  input_features => features,
  prediction => predicted_label,
  actual_label => actual_label
);

-- Check model performance
SELECT 
  model_name,
  accuracy,
  precision,
  recall,
  f1_score,
  prediction_count
FROM neurondb_model_metrics
WHERE model_name = 'fraud_detector'
  AND date >= CURRENT_DATE - INTERVAL '7 days';

-- Detect data drift
SELECT detect_data_drift(
  model_name => 'fraud_detector',
  reference_data => 'training_data',
  current_data => 'recent_predictions'
);

Use Cases

Fraud Detection

Real-time fraud detection with classification models, analyzing transaction features in milliseconds.

Customer Churn Prediction

Predict which customers are likely to churn using behavioral features and engagement metrics.

Recommendation Systems

Build collaborative filtering and content-based recommendation systems using clustering and similarity search.

Time Series Forecasting

Predict future values for sales, demand, or metrics using time series models.

Anomaly Detection

Identify outliers and anomalies in system metrics, transactions, or user behavior.

Natural Language Processing

Deploy transformer models for sentiment analysis, text classification, and named entity recognition.

Performance

Inference Latency

  • Simple Models (RF, GB): 0.1-0.5ms per prediction
  • Neural Networks (CPU): 1-5ms per prediction
  • Neural Networks (GPU): 0.1-1ms per prediction
  • Batch Inference: 10-100x throughput improvement

Training Performance

  • Random Forest (1M rows): 30-60 seconds
  • Gradient Boosting (1M rows): 2-5 minutes
  • K-Means (1M vectors): 10-30 seconds (GPU: 3-10 seconds)
  • AutoML (50 trials): 30-60 minutes

Optimization Tips

  • Use batch inference for high-throughput scenarios
  • Enable GPU acceleration for deep learning models
  • Cache frequently used models in memory
  • Use feature stores to avoid redundant computation
  • Monitor model performance and retrain when accuracy degrades

Related Documentation