Neural Network Factory API

The Neural Network Factory provides a comprehensive framework for creating and managing various neural network architectures for Hurst parameter estimation. This module implements train-once, apply-many workflows with proper GPU memory management and model persistence.

Neural Network Architectures

The factory supports 8 different neural network architectures:

Feedforward Network

Convolutional Network

LSTM Network

Bidirectional LSTM Network

GRU Network

Transformer Network

ResNet Network

Hybrid CNN-LSTM Network

Factory Class

Configuration

Architecture Enumeration

Convenience Functions

Usage Examples

Creating a Single Network

from lrdbenchmark.analysis.machine_learning.neural_network_factory import (
    NeuralNetworkFactory, NNArchitecture, NNConfig
)

# Create configuration
config = NNConfig(
    architecture=NNArchitecture.TRANSFORMER,
    input_length=500,
    hidden_dims=[64, 32],
    learning_rate=0.001,
    epochs=50
)

# Create network
factory = NeuralNetworkFactory()
network = factory.create_network(config)

Creating All Benchmark Networks

from lrdbenchmark.analysis.machine_learning.neural_network_factory import create_all_benchmark_networks

# Create all available networks
networks = create_all_benchmark_networks(input_length=500)

for name, network in networks.items():
    print(f"Created {name} network")

Training and Prediction

import numpy as np

# Generate training data
X_train = np.random.randn(100, 500)
y_train = np.random.uniform(0.2, 0.8, 100)

# Train the network
history = network.train_model(X_train, y_train)

# Make predictions
new_data = np.random.randn(1, 500)
prediction = network.predict(new_data)

print(f"Prediction: {prediction[0]:.3f}")

Model Persistence

# Models are automatically saved after training
# To load a saved model:
network.load_model()  # Loads from default path

# Or specify a custom path:
network.load_model("path/to/model.pth")

Performance Characteristics

Based on comprehensive benchmarking, the neural network architectures show the following performance:

Architecture

MAE

Execution Time

Success Rate

Transformer

0.1802

0.7ms

100%

LSTM

0.1833

0.3ms

100%

Bidirectional LSTM

0.1834

0.3ms

100%

Convolutional

0.1844

0.0ms

100%

GRU

0.1849

0.2ms

100%

ResNet

0.1859

0.1ms

100%

Feedforward

0.1946

0.0ms

100%

Key Features

  • Train-once, Apply-many: Models are trained once and can be used for multiple predictions

  • GPU Memory Management: Batch processing prevents CUDA out-of-memory issues

  • Model Persistence: Automatic saving and loading of trained models

  • Device Management: Automatic GPU/CPU selection and device placement

  • Consistent Performance: All architectures achieve similar accuracy levels

  • Fast Inference: Ultra-fast prediction times (0.0-0.7ms per sample)

  • Production Ready: Robust error handling and memory management