Neural Network Module
The pydis_nn.neuralnetwork module provides a configurable neural network implementation using TensorFlow/Keras. TensorFlow is configured to use CPU-only mode for consistent performance across different hardware configurations.
Neural network module for 5D data interpolation.
This module provides a lightweight, configurable neural network implementation using TensorFlow/Keras for regression tasks on 5-dimensional datasets.
- class pydis_nn.neuralnetwork.NeuralNetwork(hidden_sizes=[64, 32, 16], learning_rate=0.001, max_iter=300, random_state=42, early_stopping_patience=50)[source]
Bases:
objectConfigurable neural network for 5D regression tasks.
Uses TensorFlow/Keras with a simple feedforward architecture. Designed to train quickly on CPU (<1 minute for 10K samples).
List of hidden layer sizes (default: [64, 32, 16])
- learning_rate
Learning rate for Adam optimizer (default: 0.001)
- max_iter
Maximum number of training epochs (default: 300)
- random_state
Random seed for reproducibility (optional)
- model
The compiled Keras model
- Parameters:
hidden_sizes (
List[int])learning_rate (
float)max_iter (
int)random_state (
int)early_stopping_patience (
int)
- __init__(hidden_sizes=[64, 32, 16], learning_rate=0.001, max_iter=300, random_state=42, early_stopping_patience=50)[source]
Initialize the neural network.
- Parameters:
hidden_sizes (
List[int]) – List of neuron counts for each hidden layer. Default [64, 32, 16] gives 3 hidden layers.learning_rate (
float) – Learning rate for the Adam optimizer.max_iter (
int) – Maximum number of training epochs.random_state (
int) – Random seed for reproducibility. Sets both TensorFlow and NumPy random seeds if provided.early_stopping_patience (
int) – Number of epochs with no improvement after which training will be stopped. Only used if validation data is provided. Default: 50.
- fit(X, y, X_val=None, y_val=None, return_history=False)[source]
Train the neural network.
- Parameters:
X (
ndarray) – Training features (n_samples, 5)y (
ndarray) – Training targets (n_samples,)X_val (
Optional[ndarray]) – Optional validation featuresy_val (
Optional[ndarray]) – Optional validation targetsreturn_history (
bool) – If True, return training history along with self
- Returns:
self (if return_history=False) or tuple of (self, history_dict) (if return_history=True)
- predict(X)[source]
Make predictions on new data.
- Parameters:
X (
ndarray) – Feature array (n_samples, 5)- Return type:
ndarray- Returns:
Predictions array with shape (n_samples,)
- Raises:
ValueError – If model hasn’t been trained yet
- score(X, y)[source]
Return the R² score (coefficient of determination).
Useful for evaluating model performance.
- Parameters:
X (
ndarray) – Feature array (n_samples, 5)y (
ndarray) – True target values (n_samples,)
- Return type:
float- Returns:
R² score
- evaluate_all(X_train, y_train, X_val=None, y_val=None, X_test=None, y_test=None)[source]
Evaluate model performance on train, validation, and test sets.
Computes R² scores and MSE for all provided datasets.
- Parameters:
X_train (
ndarray) – Training featuresy_train (
ndarray) – Training targetsX_val (
Optional[ndarray]) – Optional validation featuresy_val (
Optional[ndarray]) – Optional validation targetsX_test (
Optional[ndarray]) – Optional test featuresy_test (
Optional[ndarray]) – Optional test targets
- Returns:
‘train_r2’, ‘train_mse’ (always present)
’val_r2’, ‘val_mse’ (if X_val/y_val provided)
’test_r2’, ‘test_mse’ (if X_test/y_test provided)
- Return type:
Dictionary with metrics for each dataset provided
Examples
Basic usage:
from pydis_nn.neuralnetwork import NeuralNetwork
from pydis_nn.data import load_and_preprocess
# Load and preprocess data
data = load_and_preprocess('dataset.pkl', random_state=42)
# Create and train model
model = NeuralNetwork(
hidden_sizes=[64, 32, 16],
learning_rate=0.001,
max_iter=300,
random_state=42
)
model.fit(
data['X_train'],
data['y_train'],
X_val=data['X_val'],
y_val=data['y_val']
)
# Make predictions
predictions = model.predict(data['X_test'])
# Evaluate
r2_score = model.score(data['X_test'], data['y_test'])