Stratified Fold by Sequence Length
Overview
In image-to-sequence tasks, output lengths vary widely (e.g., simple vs complex molecules). Random splits can create folds where one fold gets mostly short sequences and another mostly long ones, causing misleading validation scores. Stratify by binned sequence length so each fold has a representative length distribution.
Quick Start
from sklearn.model_selection import StratifiedKFold
import pandas as pd
# Compute target sequence length
train["seq_length"] = train["target_text"].str.len()
# Bin into discrete categories for stratification
train["length_bin"] = pd.qcut(train["seq_length"], q=10, labels=False, duplicates="drop")
# Create stratified folds
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
train["fold"] = -1
for fold, (_, val_idx) in enumerate(skf.split(train, train["length_bin"])):
train.loc[val_idx, "fold"] = fold
Workflow
- Compute output sequence length for each training sample
- Bin lengths into quantile-based categories (10 bins is typical)
- Use
StratifiedKFoldwith the length bins as the stratification target - Assign fold numbers; each fold has similar length distribution
- Train and validate per fold — validation metrics are now length-balanced
Key Decisions
- Number of bins: 10 quantile bins is standard; more bins = stricter balance
- Direct length vs bins:
StratifiedKFoldneeds discrete labels, so bin continuous lengths - Multi-target stratification: If you have both length and class labels, use
iterative-stratification - Alternatives: Group by length ranges and sample proportionally