Main class for building and training multi-layer perceptrons.
__init__(gpu)
Initialize a SequentialModel.
gpu(bool): Enable GPU acceleration (requires CuPy) - If GPU is not available, SaANN automatically switches to CPU
SequentialModel expects the input features (X) to be of shape (batch, features) and input targets (y) to be of shape (batch, 1) if regression, or a one-hot encoded array of shape (batch, classes) if classification.
construct(layers_info, learning_rate, batch_norm, dropout)
Builds the network architecture.
-
layers_info(list): List of tuples(input_size, neurons, activation, initialization) -
learning_rate(float): Learning rate for optimization -
batch_norm(bool): Enables batch normalization -
dropout(bool): Enables dropout -
Note:
- if you are training a regression model, it is recommended to have as the last MLP layer:
(previous_layer_neurons, 1, "linear", "he") - if you are training a classification model, it is recommended to have as the last MLP layer:
(previous_layer_neurons, num_classes, "softmax", "he")- this will override the selected loss function tocross-entropy
- if you are training a regression model, it is recommended to have as the last MLP layer:
fit(X, y, epochs, batch_size, wd, loss_function, graphical, real_time, log_plot)
Trains the model on data.
X(array): Training featuresy(array): Training labelsepochs(int): Number of training epochsbatch_size(int): Batch size for trainingwd(float): Weight decay factor for regularizationloss_function(str): 'MSE', 'MAE', 'Cross-entropy' or 'Huber' (or 'Huber:delta' for custom delta)graphical(bool): Display training plotreal_time(bool): Update training plot in real-timelog_plot(bool): Display plot in semilogy scale- Returns: Final prediction of training (array)
predict(X)
Makes predictions on new data.
X(array): Input features- Returns: Predictions (array)
automatic(X, y, layers_info, learning_rate, epochs, batch_size, wd, split_test_percentage, scaling, graphical, real_time, log_plot, test_loss, scatter_comparison)
Runs the complete pipeline: data splitting, scaling, training, and evaluation.
X(array): Full dataset featuresy(array): Full dataset labelslayers_info(list): List of tuples(input_size, neurons, activation, initialization)learning_rate(float): Learning rate for optimizationepochs(int): Number of training epochsbatch_size(int): Batch size for trainingwd(float): Weight decay factor for regularizationloss_function(str): 'MSE', 'MAE', 'Cross-entropy' or 'Huber' (or 'Huber:delta' for custom delta)split_test_percentage(float): Test/Train split ratio (0.0-1.0)scaling(str): Scaling method ('zscore', 'minmax', 'log', 'mean', or None)graphical(bool): Display training plotreal_time(bool): Update training plot in real-timelog_plot(bool): Display plot in semilogy scaletest_loss(bool): Print loss metricsscatter_comparison(bool): Display scatter plot of test vs. predicted- Returns: Tuple of (predictions, train_predictions, X_train, X_test, y_train, y_test)
save_model(path)
Save the model's architecture and weights in a pickle file
path(str): File path for output
Experimental but fully functional convolutional neural network for image classification.
__init__(filter_size, num_filters, padding, stride, num_channels, activation_function, init_function, gpu)
Initialize a CNN.
filter_size(int): Size of convolution filters - Default = 3num_filters(int): Number of filters in the first conv layer - Default = 4padding(int): Padding for convolutions - Default = 1stride(int): Stride for convolutions - Default = 1num_channels(int): Number of channels of the input images - Default = 3activation_function(str): Activation function of ConvBlock - Default = "relu"init_function(str): Initialization of the ConvBlock weights - Default = "he"gpu(bool): Enable GPU acceleration (requires CuPy) - If GPU is not available, SaANN automatically switches to CPU
SaANN's CNN expects images in NHWC format (batch, height, width, channels).
The shapes of the input arrays need to follow:
X.shape == (num_samples, height, width, num_channels)
y.shape == (num_samples, num_classes)Important: y needs to be one-hot encoded - you can use either the ImageProcessing class or the one_hot_vector function in saann.processing.
construct(layers_info, learning_rate, batch_norm, dropout)
Builds the network architecture.
layers_info(list): List of tuples(input_size, neurons, activation, initialization)learning_rate(float): Learning rate for optimizationbatch_norm(bool): Enables batch normalizationdropout(bool): Enables dropout
The last layer should follow (previous_layer_neurons, num_classes, "softmax", "he")
Build the network architecture (MLP head after convolutions).
fit(X, y, epochs, batch_size, wd, graphical, report)
Train the CNN on data.
X(array): Training images (batch_size, height, width, channels)y(array): Training labelsepochs(int): Number of training epochsbatch_size(int): Batch size for trainingwd(float): Weight decay factorgraphical(bool): Display training plotreport(bool): Print metrics report after training- Returns: Final prediction of training (array)
predict(X)
Make predictions on new images.
X(array): Input images- Returns: Predictions (array)
save_model(path)
Save the model's architecture and weights in a pickle file
path(str): File path for output
Main class for building and training a Recurrent model.
__init__(rnn_type, gpu)
Initialize a RecurrentModel.
rnn_type(str): Changes architecture of the RNN layer: 'lstm', 'gru' or None (for vanilla)gpu(bool): Enable GPU acceleration (requires CuPy) - If GPU is not available, SaANN automatically switches to CPU
RecurrentModel expects the input features (X) to be of shape (batch, seq_len, features) and input targets (y) to be of shape (batch, 1) if regression, or a one-hot encoded array of shape (batch, classes) if classification.
construct(input_dim, hidden_dim, output_dim, activation_function, init_function, learning_rate, random_scale, act_function_rnn, init_function_rnn, random_scale_rnn, many_to_one, dropout)
Construct the RNN and the Dense layers of the model
input_dim(int): Input dimensionshidden_dim(int): Hidden dimensionsoutput_dim(int): Output dimensionsactivation_function(str): Activation function for the Dense layerinit_function(str): Initiation function for the Dense layerlearning_rate(float): Hyperparameter - Learning rate for optimizationrandom_scale(float): Scale for the 'random' initiation method (dense layer)act_function_rnn(str): Activation function for the RNN layerinit_function_rnn(str): Initiation function for the RNN layerrandom_scale_rnn(float): Scale for the 'random' initiation method (RNN layer)many_to_one(bool): Enables the many-to-one or the many-to-many if Falsedropout(bool): Enables dropout
fit(X, y, epochs, batch_size, wd, loss_function, graphical, real_time, log_plot)
Trains the model on data.
X(array): Training featuresy(array): Training labelsepochs(int): Number of training epochsbatch_size(int): Batch size for trainingwd(float): Weight decay factor for regularizationloss_function(str): 'MSE', 'MAE', 'Cross-entropy' or 'Huber' (or 'Huber:delta' for custom delta)graphical(bool): Display training plotreal_time(bool): Update training plot in real-timelog_plot(bool): Display plot in semilogy scale- Returns: Final prediction of training (array)
predict(X)
Makes predictions on new data.
X(array): Input features- Returns: Predictions (array)
save_model(path)
Save the model's architecture and weights in a pickle file
path(str): File path for output
Experimental class implementing dual‑network training with weight‑swapping regularization.
__init__(gpu)
Initializes the Cross‑Training model.
gpu(bool): Enable GPU acceleration (CuPy). Falls back to CPU automatically.
construct(layers_info, learning_rate, batch_norm, dropout)
Builds two identical MLPs.
layers_info(list): Same format asSequentialModellearning_rate(float): SGD learning ratebatch_norm(bool): Enable batch normalization. Default isFalsedropout(bool): Enable dropout. Default isFalse
fit(X, y, epochs, batch_size, wd, loss_function, fine_tuning_ratio, graphical, real_time, log_plot, parallelize)
Trains the two MLPs using the Cross‑Training strategy.
X(array): Training featuresy(array): Training labelsepochs(int): Total number of epochsbatch_size(int): Batch sizewd(float): Weight decay- If
wd> 0, weight decay is applied only during fine‑tuning. Cross‑training phase always useswd= 0.
- If
loss_function(str): 'mse', 'mae', 'huber', 'cross-entropy'fine_tuning_ratio(float): Fraction of epochs dedicated to fine‑tuninggraphical(bool): Plot training loss. Default isFalsereal_time(bool): Live plot updates. Default isFalselog_plot(bool): Semilogy scale. Default isFalseparallelize(bool): Parallel forward/backward passes (CPU only). Default isFalse- Returns: Final predictions on training data (array)
predict(X)
Returns predictions using the best-performing model.
X(array): Input features- Returns: Prediction array
Additional regards:
- If model1 has lower training loss → use model1
- Otherwise → use model2
- If unclear → average predictions
save_model(path)
Save the model's architecture and weights in a pickle file
path(str): File path for output
load_model(path)
Load the model saved using the model.save_model(path) method. It automatically detects the type of model saved.
path(str): File path of model's pickle- Returns: Model (class)
The training of the TransformerModel operates on multiple layers of the architecture. For that reason, it was preferred to provide the API needed for training as in the example found in Quick Start.
For the tokenizer, one can use two methods.
From saann.tokenizer:
ByteTokenizer()
- 256‑token vocabulary
- Returns: tokenizer
CharTokenizer(text)
text(array): Input (text) array- ASCII/UTF‑8 character vocabulary
- Returns: tokenizer
Both provide:
encode(text)- returns the list of token IDs
decode(tokens)- returns the string after reconstruction
To initialize the model, from saann.transformer.transformer_model:
Class for the transformer model
__init__(self, vocab_size, embed_dim, num_heads, ff_hidden_dim, num_layers, max_seq_len, learned_positional)
Initialize the model
vocab_size(int): Size of the tokenizer vocabolaryembed_dim(int): Dimensions of the embeddingsnum_heads(int): Number of headsff_hidden_dim(int): Number of hidden dimension of the Feed-Forwardnum_layers(int): Number of layersmax_seq_len(int): Maximum value for the sequence lengthlearned_positional(bool): tag tokens with unique positions in a sequence
To train the model, from saann.training:
create_optimizer(model, learning_rate, wd)
Creates the optimizer for the training
model(class): Model classlearning_rate(float): Maximum learning rate for the scheduler. Default is1e-4wd(float): Weight decay factor. Default is0.1- Returns: optimizer class
train_transformer(model, optimizer, data, batch_size, seq_len, epochs, checkpoint_every, checkpoint_dir, tokenizer)
Operates the pipeline for training the TransformerModel
model(class): Model classoptimizer(class):data(array): Encoded input arraybatch_size(int): Size of batch for trainingseq_len(int): Sequence lengthepochs(int): Number of epochscheckpoint_every(int): Number of epochs needed to save each checkpointcheckpoint_dir(str): File path for the checkpoint directory. Default is "checkpoints". Saves fully trained model as "checkpoint_final.npz"tokenizer(class): Tokenizer class
load_model(path)
Load the model saved during the training.
path(str): File path of the model- Returns: Model (class)
To generate the output, from saann.generate:
generate_top_p(model, start_tokens, max_new_tokens, p, temperature, rep_penalty)
Generate tokens using the trained model
model(class): Trained model classstart_tokens(array): Array of encoded token to initialize the generationmax_new_tokens(int): Number of tokens to generatep(float): Nucleus sampling thresholdtemperature(float): Temperature parameterrep_penalty(float): Repetition penalty- Returns: encoded tokens
Comprehensive metrics suite for model evaluation.
Calculates and returns the correlation matrix of the features array
X(array): Features arraylabels(list): List of features' labels - Default is Nonegraphical(bool): Plots the correlation matrix as a heatmap - Default is True- Returns: Correlation matrix (array)
__init__(y_pred, y_test)
Initialize metrics calculator.
y_pred(array): Predicted labels (probabilities, not logits)y_test(array): True labels (one-hot encoded - usesaans.processing.one_hot_vector()if needed)
report(graphical, threshold_step)
Print a full metrics summary including:
-
Precision, Recall, F1 (Macro/Micro/Weighted)
-
Balanced Accuracy, Specificity, MCC
-
Confusion Matrix
-
ROC curves (One-vs-Rest for multi-class)
-
graphical(bool): Plots ROC curves and heatmap of the confusion matrix -
threshold_step(float): Step used to increment from 0 to 1 for ROC calculation
confusion_matrix(graphical)
Calculate the confusion matrix
graphical(bool): Plots the heatmap of the confusion matrix- Returns: Confusion matrix (array)
precision()
Calculate precision for each class
- Returns: Precisions (array)
recall()
Calculate sensitivity for each class
- Returns: Recalls (array)
F1score()
Calculate the F1-score for each class
- Returns: F1-scores (array)
macro_f1()
Calculate the Macro F1-score for each class
- Returns: Macro F1-scores (float)
micro_f1()
Calculate the Micro F1-score for each class
- Returns: Micro F1-scores (float)
weighted_f1()
Calculate the Weighted F1-score for each class
- Returns: Weighted F1-scores (float)
AUC(graphical, threshold_step)
Calculate the macro-averaged AUC
graphical(bool): Plots ROC curvesthreshold_step(float): Step used to increment from 0 to 1 for ROC calculation- Returns: Macro-averaged AUC (float)
balanced_accuracy()
Calculate balanced accuracy for each class
- Returns: Balanced accuracy (array)
specificity()
Calculate specificity for each class
- Returns: Specificity (array)
cohen_kappa()
Calculate Cohen's kappa for each class
- Returns: Cohen's kappa (float)
MCC()
Calculate Matthews correlation coefficient for each class
- Returns: Matthews correlation coefficient (float)
train_test_split(X, y, split_test_percentage)
Splits the dataset provided into Train and Test arrays
X(array): Featuresy(array): Targetssplit_test_percentage(float): Test/Train split ratio- Returns: Tuple of (X_train, X_test, y_train, y_test)
one_hot_vector(y)
Format the y array for classification
y(array): Targets- Returns: Formatted targets (array)
The ImageProcessing class prepares image datasets for use with SaANN's CNN and MLP models.
It handles:
- directory scanning
- resizing
- cleaning (RGBA → RGB)
- normalization
- one‑hot encoding
- shuffling
- train/test splitting
All images are returned in NHWC format:
(batch, height, width, channels)prepare_images(size, amount)
Resizes all images in the dataset.
-
size(int): output resolution (size × size) -
amount(int): optional limit of images per class (can beNone) -
Creates a temporary folder:
resized/ -
Saves all resized images as JPEG (
.jpg) -
Preserves class names using the folder structure
Folder structure required:
dataset/
class1/
class2/
class3/
class4/
...image_upload(rel_path)
Loads all resized images into memory.
- Reads
.jpgfiles from theresized/directory - Extracts class labels from filename prefixes (
class_index.jpg) - Returns raw pixel arrays before cleaning
clean_dataset(X, y)
Ensures all images have valid channels.
- Drops grayscale images (channels < 3)
- Converts RGBA → RGB by removing the alpha channel
- Keeps only 3‑channel images
- Prints whether cleaning was required
prepare_features(X=None)
Normalizes image pixel values.
- Converts to
float32 - Normalizes to the range [-0.5, 0.5]
- Converts list of arrays into a single NumPy/CuPy array
Important: Do not normalize manually — SaANN does it automatically.
prepare_targets(y=None, list_classes=None)
Converts class labels into one‑hot encoded vectors.
- Output shape:
(num_samples, num_classes) - Required for all classification tasks
- Class order is alphabetical unless overridden
Example:
['daisy', 'rose', 'tulip'] → [1,0,0], [0,1,0], [0,0,1]shuffle_dataset(X=None, y=None)
Randomly shuffles features and labels together.
- Ensures X and y stay aligned
- Uses NumPy/CuPy permutation
ready_dataset(size, amount, shuffle, remove_resized, split_test_percentage)
Load and prepare an image dataset. It is the full default image pre-processing pipeline
size(int): Resize images to size×sizeamount(int): Maximum images per class. Can be None.shuffle(bool): Shuffle the datasetremove_resized(bool): Remove resized files after processingsplit_test_percentage(float): Test/Train split ratio- Returns: Tuple of
(X_train, X_test, y_train, y_test, class_names)
Where:
Xhas shape(batch, height, width, channels)yis one‑hot encodedclass_namesis a list of class labels
Pipeline steps:
- Resize images
- Load resized images
- Clean channels
- Normalize features
- One‑hot encode labels
- Shuffle (optional)
- Train/test split (optional)
- Delete resized folder (optional)
Important:
- SaANN's CNN expects images in NHWC format
(batch, height, width, channels) - The CNN model has a parameter
num_channelthat allows to change the number of channels the input images have - Its default value is 3 but can be changed by the user
- Be aware that if the data was processed using
ready_datasetthennum_channelMUST remain 3 - If you change
num_channel, please provide your own pre-processed data
Data scaling utilities.
zScore(x), MinMax(x), LogNorm(x), MeanNorm(x)
Apply respective scaling transformations to features.
x(array): Features to scale- Returns: Scaled features (array)
MSE(y_true, y_pred)
Mean Squared Error loss.
y_true(array): Targets used for testingy_pred(array): Predicted targets
MAE(y_true, y_pred)
Mean Absolute Error loss.
y_true(array): Targets used for testingy_pred(array): Predicted targets
cross_entropy(y_true, y_pred)
Cross-Entropy Error loss.
y_true(array): Targets used for testingy_pred(array): Predicted targets
Huber(y_true, y_pred, delta)
Huber loss with configurable delta parameter.
y_true(array): Targets used for testingy_pred(array): Predicted targets
R2_score(y_true, y_pred)
Coefficient of determination (R²).
y_true(array): Targets used for testingy_pred(array): Predicted targets
QLIKE(y_true, y_pred, eps)
Calculates the Quasi-Likelihood (QLIKE) Loss.
y_true(array): Targets used for testingy_pred(array): Predicted targetseps(float): Epsilon clipping threshold for numerical stability
SaANN includes an experimental finance module designed for volatility forecasting and financial time-series modelling.
The finance module provides volatility estimation, HAR feature engineering, sequence generation for recurrent networks, financial forecasting metrics, and an automated dataset preparation pipeline.
Complete preprocessing workflow for volatility forecasting.
__init__(estimator, feature_builder, target_builder, sequence_builder, test_size)
Initialize a volatility forecasting dataset.
estimator(class): Volatility estimator classfeature_builder(class): Feature generation classtarget_builder(class): Forward target generator classsequence_builder(class): Sequence generation classtest_size(float): Fraction of samples to reserve for testing
Default components:
- ParkinsonVolatility
- HARFeatures
- ForwardVolatilityTarget
- SequenceBuilder
prepare(df)
Prepare OHLCV market data for forecasting.
df(DataFrame): Pandas DataFrame containing financial data
Required columns:
- High
- Low
Optional columns:
- Open
- Close
- Volume
Returns:
(
X_train,
X_test,
y_train,
y_test
)Pipeline:
OHLCV DataFrame
↓
Volatility Estimation
↓
HAR Feature Generation
↓
Forward Target Generation
↓
Sequence Generation
↓
Train/Test Split
↓
Feature Scaling
Parkinson high-low volatility estimator.
transform(df)
Compute volatility from OHLC market data.
Parameters:
df(DataFrame)
Returns:
volatilityOutput shape:
(len(df),)The Parkinson estimator uses daily high-low ranges to estimate realized volatility.
Generate Heterogeneous Auto-Regressive (HAR) volatility features.
__init__(daily_window, weekly_window, monthly_window)
Initialize the HAR feature generator.
Parameters:
daily_window(int): Daily volatility window. Default = 1weekly_window(int): Weekly volatility window. Default = 5monthly_window(int): Monthly volatility window. Default = 22
transform(volatility)
Generate HAR volatility features.
Parameters:
volatility(array)
Returns:
XOutput shape:
(samples, 3)Generated features:
RV_daily
RV_weekly
RV_monthly
Create forward realized volatility forecasting targets.
__init__(horizon, aggregation)
Initialize the target generator.
Parameters:
horizon(int): Forecast horizon. Default = 5aggregation(str): Aggregation method
Supported aggregations:
- "mean"
- "max"
- "median"
transform(volatility)
Generate forecasting targets.
Parameters:
volatility(array)
Returns:
yOutput shape:
(samples,)Targets are constructed strictly from future observations.
Forecast alignment:
Input:
[t-lookback+1 ... t]
Target:
[t+1 ... t+horizon]
This prevents look-ahead bias and leakage.
Convert financial features into recurrent model sequences.
__init__(lookback)
Initialize the sequence generator.
Parameters:
lookback(int): Number of timesteps used as model input
build(features, target)
Generate sequence tensors.
Parameters:
features(array)target(array)
Returns:
(
X,
y
)Output shapes:
X.shape == (
samples,
lookback,
features
)
y.shape == (
samples,
1
)The resulting tensors are directly compatible with:
- RecurrentModel(rnn_type=None)
- RecurrentModel(rnn_type="gru")
- RecurrentModel(rnn_type="lstm")
The finance module extends SaANN's existing loss functions with forecasting-specific evaluation metrics.
The following standard losses remain available through:
from saann.losses import (
MSE,
MAE,
R2_score,
QLIKE
)Additional finance-specific metrics are provided through:
from saann.finance import metricsrmspe(y_true, y_pred)
Root Mean Squared Percentage Error.
Parameters:
- y_true (array)
- y_pred (array)
Returns:
floatOutput is expressed as a percentage (%).
mape(y_true, y_pred)
Mean Absolute Percentage Error.
Parameters:
y_true(array)y_pred(array)
Returns:
floatOutput is expressed as a percentage (%).
mase(y_true, y_pred)
Mean Absolute Scaled Error.
Parameters:
y_true(array)y_pred(array)
Returns:
floatInterpretation:
MASE < 1 → Better than naive forecast
MASE > 1 → Worse than naive forecast
directional_accuracy(y_true, y_pred, baseline=None)
Measure the directional forecasting performance of the model.
Parameters:
y_true(array)y_pred(array)baseline(array): Optional comparison series
Returns:
floatOutput is expressed as a percentage (%).
horizon_directional_accuracy(y_true, y_pred, step=5)
Measure directional accuracy using non-overlapping horizons.
Parameters:
y_true(array)y_pred(array)step(int)
Returns:
floatOutput is expressed as a percentage (%).
volatility_report(y_true, y_pred, baseline=None, verbose=False, graphical=False)
Generate a volatility forecasting report.
Parameters:
y_true(array)y_pred(array)baseline(array): Optional benchmark seriesverbose(bool): Print report to consolegraphical(bool): Generate visual diagnostics
Returns:
dict- relu: Rectified Linear Unit
- sigmoid: Sigmoid function
- tanh: Hyperbolic tangent
- linear: Linear activation (identity)
- softmax: Softmax activation
- softplus: Softplus activation
- he: He initialization (recommended for ReLU)
- xavier: Xavier/Glorot initialization
- random: Random normal initialization
⬅ Previous: Quick Start · Back to README · Next: Architecture