Binary classification of solar flares (FL = flare / NF = no flare) from time series of 18 SHARP magnetic parameters extracted from SDO/HMI magnetograms.
This model was developed as part of a PIBIC/CNPq Scientific Initiation project at UNICAMP (Brazil), focused on predicting solar eruptive events using deep learning on space weather data.
The architecture is a bidirectional LSTM encoder + additive attention (Bahdanau) + dense decoder, trained on the SWAN-SF dataset with strict GroupKFold cross-validation by HARPNUM to prevent data leakage between active regions.
Input (batch, T=60, 18)
→ Encoder: biLSTM (hidden=128, layers=2, bidirectional)
→ Attention: additive (Bahdanau) over all 60 time steps
→ Context vector (dim=256)
→ Decoder: Linear(256→128) → ReLU → Dropout(0.4)
→ Linear(128→64) → ReLU → Dropout(0.4)
→ Linear(64→1) → Sigmoid
Output: flare probability ∈ [0, 1]
The ensemble averages probabilities from 5 fold models and applies an optimized threshold (0.45, found by grid search over the validation set to maximize TSS).
| Choice | Rationale |
|---|---|
| GroupKFold by HARPNUM | Prevents leakage: windows from the same active region stay in the same fold |
| 15% hold-out before CV | True out-of-sample evaluation — no fold selection bias |
| Intra-epoch checkpoints (every 100 batches) | Captures TSS peaks mid-epoch in a large dataset (~316k series) |
| Model selection by val loss, not TSS@0.5 | TSS@0.5 is unstable on smaller val batches; val loss is smoother |
| Optimal threshold (grid [0.05, 0.95]) | Default 0.5 is arbitrary under class imbalance (FL ≪ NF) |
pip install torch numpy pandas
import numpy as np
from predict import SolarFlarePredictor
# series: numpy array (T, 18) with SHARP parameters, Z-score normalized
# T ≥ 60; last 60 steps are used
predictor = SolarFlarePredictor() # loads all 5 fold models from ./models/
prob, label = predictor.predict(series) # ensemble probability + binary label
print(f"Flare probability: {prob:.3f} → {'FL' if label else 'NF'}")
# With attention weights (fold 1):
prob, label, attn = predictor.predict(series, return_attention=True)
# attn: array (60,) — weight of each time step
| Property | Value |
|---|---|
| Shape | (T, 18) — T ≥ 1 (padded to 60 if shorter) |
| Normalization | Z-score (mean/std from SWAN-SF training partition) |
| Clip range | [-10, 10] applied automatically |
Feature order (must match exactly):
SHARP_FEATURES = [
"USFLUX", "MEANGAM", "MEANGBT", "MEANGBZ", "MEANGBH", "MEANJZD",
"MEANALP", "MEANJZH", "TOTUSJH", "TOTPOT", "TOTUSJZ", "ABSNJZH",
"SAVNCPP", "R_VALUE", "TOTBSQ", "TOTFZ", "MEANPOT", "MEANSHR",
]
python predict.py --input my_region.csv --show_attention
The CSV must have the 18 SHARP columns as headers and at least 60 rows of Z-score normalized values.
import torch
from model import BiLSTMAttentionSeq2Seq
model = BiLSTMAttentionSeq2Seq()
ckpt = torch.load("models/fold_1_best.pt", map_location="cpu")
model.load_state_dict(ckpt["model_state"])
model.eval()
x = torch.randn(1, 60, 18) # (batch, time, features)
prob = model(x) # tensor of shape (1,)
prob, attn = model(x, return_attention=True)
SWAN-SF — Solar Wind ANalysis – Solar Flare
Pre-processing:
scaler.pkl reused at inference| Parameter | Value |
|---|---|
HIDDEN_SIZE |
128 |
NUM_LAYERS |
2 |
DROPOUT |
0.4 |
WINDOW_SIZE |
60 (timesteps) |
N_FEATURES |
18 (SHARP) |
| Learning rate | 3e-4 (AdamW) |
| Weight decay | 1e-4 |
| Batch size | 256 |
| Positive class weight cap | 12 |
| Checkpoint patience | 8 checkpoints (intra-epoch) |
| N folds | 5 (GroupKFold by HARPNUM) |
| Hold-out fraction | 15% of HARPNUMs |
| Metric | Value |
|---|---|
| TSS (ensemble, hold-out) | ~0.78 |
| Threshold | 0.45 (optimized on val set) |
| Hold-out size | ~15% of unique HARPNUMs |
TSS = Recall − FAR (True Skill Statistic / Hanssen–Kuipers Discriminant)
TSS is immune to class imbalance and is the standard metric in operational space weather forecasting.
The trained biLSTM encoder was reused for Coronal Mass Ejection (CME) prediction using the cmesrc catalog (Hernandez et al., 2025):
Source code: github.com/gabriel-aleksandro/cme-flare-prediction
If you use this model, please cite:
@misc{depaula2025solarflare,
title = {Solar Flare Prediction with biLSTM + Bahdanau Attention (5-fold Ensemble)},
author = {de Paula, Gabriel Aleksandro},
year = {2025},
url = {https://huggingface.co/gabriel-aleksandro/solar-flare-bilstm}
}
Also cite the dataset and catalog:
@article{hernandez2025cmesrc,
title = {cmesrc: A Catalog of CME–Source Region Associations},
author = {Hernandez et al.},
journal = {ApJ},
year = {2025},
doi = {10.3847/1538-4357/ad9b27}
}
SWAN-SF dataset:
Harvard Dataverse — https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/EBCFKM
MIT
models/fold_1_best.pt 3 MBmodels/fold_2_best.pt 3 MBmodels/fold_3_best.pt 3 MBmodels/fold_4_best.pt 3 MBmodels/fold_5_best.pt 3 MB