Repository navigation
feat(ml): PatchTST & iTransformer training scripts (Stage 2) - #710
Merged
Merged
Conversation
…age 2) Two SOTA transformer architectures for financial time-series forecasting: - PatchTST (ICLR 2023): patching + channel independence, 635K params default - iTransformer (ICLR 2024): inverted attention over variables, 613K params Both include anti-pattern safeguards: train-only normalization, majority-class baseline, thermal watchdog (80C), AMP support, walk-forward validation. 21 CPU-only smoke tests covering forward shapes, gradient flow, state dict roundtrip, sequence building, normalization, and majority baseline computation. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
jsboige
commented
May 4, 2026
jsboige
left a comment
Owner
Author
There was a problem hiding this comment.
COMMENT review (self-authored — cannot APPROVE)
PatchTST & iTransformer training scripts (Stage 2)
5 files, +1476: 2 training scripts + 1 doc + 2 test files.
Scripts reviewed
-
train_patchtst.py (633 lines): PatchTST implementation (Nie et al., ICLR 2023)
- Patching + channel independence as described in the paper
- Variable positional encoding (learnable)
- Proper train-only normalization (
normalize_sequencescomputes stats on train set) - Majority-class baseline computed and compared (anti-pattern #703 safeguard)
- AMP support, thermal watchdog, gradient clipping (max_norm=1.0)
- CosineAnnealingWarmRestarts scheduler
- Best-state checkpointing (saves best val_loss model)
-
train_itransformer.py (541 lines): iTransformer implementation (Li et al., ICLR 2024)
- Correct inverted architecture: [B,T,N] → [B,N,T] → Linear(T,d_model) → Transformer over N variable tokens
- Mean aggregation across variables for final prediction
- Same safeguards as PatchTST (normalization, baseline, AMP, thermal)
-
STAGE2_TRANSFORMERS_TS.md (102 lines): Architecture comparison table, quick start, anti-pattern safeguards documented
Tests
- test_patchtst.py (11 tests): forward pass shapes, parameter count, state dict roundtrip, gradient flow, normalization
- test_itransformer.py (10 tests): similar coverage for iTransformer
Anti-pattern compliance
--test-ratio HONORED: actual ratio reported alongside requested- Train-only Z-normalization (anti-leakage)
- Majority-class baseline always reported with edge calculation
batch_thermal_check()every 5 batches, MAX_TEMP=80°C- Walk-forward validation supported via Stage 0 WalkForwardSplitter
Minor notes
sys.path.append(str(Path(__file__).resolve().parent.parent.parent / "shared"))— fragile but consistent- Both scripts share duplicated code (normalize_sequences, compute_majority_class_baseline, save_checkpoint) — could be extracted to shared module, but acceptable for now
data_hashincluded in metadata for reproducibility
No bugs found. Ready for external APPROVE.
14 tasks
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
…paths) (#7598) MGS-10-CenterBias.ipynb hardcoded `c:/dev/MetaGeneticSharp/...` absolute paths in cell 1 (5 #r directives + 1 prerequisite comment). Same anti-pattern as PR #7583 (MGS-7b N-D landscape) and PR #7595 (MGS-7-TSP). Transform c:/dev/MetaGeneticSharp/ -> ..\MetaGeneticSharp\ (relative to notebook location at Search/Part4-Metaheuristics/, submodule at Search/MetaGeneticSharp/). Build config kept Debug (matches canonical, minimum surface change). Verifications: - nbclient .net-csharp: 8/8 cells, 0 errors, all execution_count - promote ec+outputs: 8/8 cells promoted from .executed.ipynb - strip probeAddresses banner: 9 banner lines stripped from canonical (NOT from .executed.ipynb - C708-L5 NEW lesson applied) - H.3 validate_pr_notebooks.py origin/main: PASS 1/1 (8 cells [.net-csharp]) - dotnet build MetaGeneticSharp.Extensions Debug: 0 errors, 21 warnings (CA1416 platform + CS8618 nullable, same as c.709) Submodule pointer NOT bumped (no submodule content change, only notebook paths). MetaGeneticSharp stays at submodule-main SHA 6a501d967331205dea193c7a415ec1c852766f7c. c.71x sweep continues with MGS-11+ if window permits (13 MGS notebooks total remaining after this fix, ordered low-path-count first). Co-authored-by: Claude-Code <noreply@anthropic.com>
This was referenced Jul 20, 2026
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
…elative paths) (#7607) Apply the #710 portability pattern (cf #7595 MGS-7, #7598 MGS-10, #7603 MGS-19, #7605 MGS-18) to MGS-16-AlgorithmSelection.ipynb: 6 hardcoded '#r' references c:/dev/MetaGeneticSharp/... -> ../MetaGeneticSharp/... (forward-slash, cross-platform portable, matching MGS-3 canonical form on main). Re-executed end-to-end via .net-csharp kernel (MetaGeneticSharp.sln Debug built, 0 errors): 14/14 code cells, exec_count != null, 0 errors, wiring cell emits 'DLLs chargees. CenterBiasBenchmark' proving the relative paths load. Outputs regenerated (C.2). Build config = Debug maintained. Submodule pointer NOT bumped. See #710 Co-authored-by: Claude <noreply@anthropic.com>
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
… (relative paths) (#7603) * fix(search,#710): make MGS-19-MetropolisReinsertion notebook portable (relative paths) Apply the #710 portability pattern (cf #7595 MGS-7, #7598 MGS-10) to MGS-19-MetropolisReinsertion.ipynb: 5 hardcoded '#r' references c:/dev/MetaGeneticSharp/... -> ../MetaGeneticSharp/... (forward-slash, cross-platform portable, matching MGS-3 canonical form on main). Re-executed end-to-end via .net-csharp kernel (MetaGeneticSharp.sln Debug built first, 0 errors): 9/9 code cells, exec_count != null, 0 errors, wiring cell emits 'DLLs chargees. Operateurs de reinsertion du fork :' proving the relative paths load. Outputs regenerated (C.2). Build config = Debug maintained. Submodule pointer NOT bumped. See #710 Co-Authored-By: Claude <noreply@anthropic.com> * fix(search): strip probeAddresses banner from MGS-19-MetropolisReinsertion output (banner-guard) .NET Interactive bootstrap re-injects the probeAddresses banner on every re-exec; sanctioned strip via scripts/notebook_tools/strip_probe_banner.py --apply (secrets-hygiene regle 6, not hand-scrubbing). Content unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
…hs) (#7605) * fix(search,#710): make MGS-18-CecBanc notebook portable (relative paths) Apply the #710 portability pattern (cf #7595 MGS-7, #7598 MGS-10, #7603 MGS-19) to MGS-18-CecBanc.ipynb: 5 hardcoded '#r' references c:/dev/MetaGeneticSharp/... -> ../MetaGeneticSharp/... (forward-slash, cross-platform portable, matching MGS-3 canonical form on main). Re-executed end-to-end via .net-csharp kernel (MetaGeneticSharp.sln Debug built first, 0 errors): 11/11 code cells, exec_count != null, 0 errors, wiring cell emits 'DLLs chargees. Moteur de-biais CEC :' proving the relative paths load. Outputs regenerated (C.2). Build config = Debug maintained. Submodule pointer NOT bumped. See #710 Co-Authored-By: Claude <noreply@anthropic.com> * fix(search): strip probeAddresses banner from MGS-18-CecBanc output (banner-guard) .NET Interactive bootstrap re-injects the probeAddresses banner on every re-exec; sanctioned strip via scripts/notebook_tools/strip_probe_banner.py --apply (secrets-hygiene regle 6, not hand-scrubbing). Content unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
…lative paths) (#7608) * fix(search,#710): make MGS-15-LandscapeAnalysis notebook portable (relative paths) Apply the #710 portability pattern (cf #7595 MGS-7, #7598 MGS-10, #7603 MGS-19, #7605 MGS-18, #7607 MGS-16) to MGS-15-LandscapeAnalysis.ipynb: 8 hardcoded '#r' references c:/dev/MetaGeneticSharp/... -> ../MetaGeneticSharp/... (forward-slash, cross-platform portable, matching MGS-3 canonical form on main). Re-executed end-to-end via .net-csharp kernel (MetaGeneticSharp.sln Debug built, 0 errors): 10/10 code cells, exec_count != null, 0 errors, wiring cell emits 'Benchmarks dispos : SphereFitness, RastriginFitness, AckleyFitness' proving the relative paths load. Landscape analysis figures regenerated (C.2). Build config = Debug maintained. Submodule pointer NOT bumped. See #710 Co-Authored-By: Claude <noreply@anthropic.com> * fix(search): strip probeAddresses banner from MGS-15-LandscapeAnalysis output (banner-guard) .NET Interactive bootstrap re-injects the probeAddresses banner on every re-exec; sanctioned strip via scripts/notebook_tools/strip_probe_banner.py --apply (secrets-hygiene regle 6, not hand-scrubbing). Content unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
jsboige
added a commit
that referenced
this pull request
Jul 20, 2026
…paths) (#7612) ### Summary Replaces hardcoded `c:/dev/MetaGeneticSharp/...` paths with portable `..\MetaGeneticSharp\...` (relative to the notebook directory) in `MGS-8-LandscapeExplorer.ipynb`. The pattern follows the c.706 (MGS-7 TSP) and c.710 (MGS-10 CenterBias) precedent: a relative path that resolves correctly when the .NET Interactive kernel cwd is set to the notebook's parent directory. This unblocks running MGS-8 on any machine that has the `MetaGeneticSharp` submodule checked out alongside the notebook — no symlink to `c:/dev/MetaGeneticSharp` required. ### Changes | File | Change | |------|--------| | `MyIA.AI.Notebooks/Search/Part4-Metaheuristics/MGS-8-LandscapeExplorer.ipynb` | 1 markdown line (build prerequisite) + 7 `#r` paths + 1 `NativeLibrary.Load` path: `c:/dev/MetaGeneticSharp/...` → `..\MetaGeneticSharp\...` | Source-only diff (cell sources): **9 line changes** in the `#r` and `NativeLibrary.Load` block + 1 markdown prerequisite line. Outputs regenerated by re-execution (cell sources preserved verbatim otherwise). ### Verification - `dotnet build src/MetaGeneticSharp.Extensions/MetaGeneticSharp.Extensions.csproj -c Debug` → 0 errors (21 pre-existing CA1416/CS8618 warnings, unrelated) - `scripts/execute_dotnet_notebook.py MyIA.AI.Notebooks/Search/Part4-Metaheuristics/MGS-8-LandscapeExplorer.ipynb 600` → **16/16 cells SUCCESS, 0 errors** (kernel cwd set to `Part4-Metaheuristics/` via `System.IO.Directory.SetCurrentDirectory`) - `scripts/notebook_tools/validate_pr_notebooks.py origin/main <nb>` → **PASS** (16 cells [.net-csharp]) - `scripts/notebook_tools/strip_probe_banner.py --apply <canonical>` → 0 banners (clean) - Submodule SHA: `MetaGeneticSharp @ 6a501d96` (unchanged from main) ### Context - Rollout MGS portability c.706 → c.710 → **c.711** (this PR). 1 notebook per cycle; 13 MGS notebooks remaining (`MGS-3, MGS-4, MGS-6, MGS-9, MGS-11..19`). - Closes (sub-task of #710 — partial, see #710 for full scope): fixes MGS-8 hardcoded path. Other MGS notebooks to follow in subsequent cycles. - Sibling PRs: #7598 (c.710 MGS-10, MERGED eb020ca → 4a5419c), #7583 + submodule #41 (c.706 MGS-7 N-D landscape). ### Lessons captured - `scripts/execute_dotnet_notebook.py` (canonical .NET kernel executor) handles cwd setup via `System.IO.Directory.SetCurrentDirectory(working_dir)` BEFORE executing cells. This is the canonical pattern for re-executing .NET Interactive notebooks with relative paths. - NotebookEdit tool wipes `execution_count` + `outputs` AND converts `source` list→str — use Python with `nbformat.write` for proper round-trip preservation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Files
scripts/train_patchtst.pyscripts/train_itransformer.pytests/test_patchtst.pytests/test_itransformer.pydocs/STAGE2_TRANSFORMERS_TS.mdTest plan
python -m pytest tests/ -v— 21/21 passed (2.83s)python scripts/train_patchtst.py --dry-run— 635K params, pipeline validatedpython scripts/train_itransformer.py --dry-run— 613K params, pipeline validatedCo-Authored-By: Claude Opus 4.7 noreply@anthropic.com
🤖 Generated with Claude Code