DHDRL commited on
Commit
307bdde
Β·
verified Β·
1 Parent(s): b8f29b6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +79 -49
README.md CHANGED
@@ -1,71 +1,101 @@
1
  ---
2
  license: agpl-3.0
 
3
  tags:
4
- - reinforcement-learning
5
- - stable-baselines3
6
- - deep-reinforcement-learning
7
- - agricultural-ai
8
- - weather-modelling
9
- - curriculum-learning
10
- - edge-ai
11
  ---
12
 
13
- # Agricultural Weather-Risk RL β€” Permutation-Invariant GRU Policy with Curriculum Weight Transfer
14
 
15
- Core RL components and training infrastructure for a budget-constrained, multi-zone weather-risk inspection task. The system includes a Gymnasium environment, a **permutation-invariant GRU features extractor** that enables weight transfer across curriculum phases with varying numbers of zones, full multi-phase curriculum training, and an export path to quantized MNN for edge deployment.
 
 
 
 
 
 
 
 
16
 
17
  ## What is included
18
 
19
- - `weather_forecast_env.py` β€” Gymnasium environment with per-zone belief tracking, action masking, soft reset, and NaN-safe wrapper.
20
- - `gru_weather_policy.py` β€” Permutation-invariant GRU features extractor (shared per-zone processing + attention-weighted aggregation + max pooling). Makes the majority of policy parameters independent of `n_zones`.
21
- - `train_curriculum.py` β€” 5-phase curriculum trainer (`normal` β†’ `monsoon` β†’ `drought` β†’ `heatwave` β†’ `humidity`) with automatic weight transfer between phases.
22
- - `crop_risk_scorer.py` β€” Deterministic, economics-calibrated risk scoring used for both training rewards and evaluation.
23
- - `climatology.py` + `indonesia_zones.py` β€” Real per-zone climatology and 14 grounded Indonesian agricultural zones with rice crop calendars.
24
- - `backtest_indonesia.py` β€” Historical replay harness with precision/recall and lead-time metrics.
25
- - `mnn_export.py` + `edge_wrapper.cpp` β€” ONNX export and C++/Vulkan edge runtime with external GRU hidden-state management.
26
- - `era5_data_pipeline.py` β€” Multi-source data pipeline (ERA5 / Open-Meteo / synthetic) with graceful degradation.
27
 
28
- ## Key Technical Achievement
29
 
30
- The central engineering result is a **permutation-invariant, size-independent policy architecture** that solves weight transfer across curriculum phases with changing numbers of zones.
 
 
 
31
 
32
- Previous designs tied the GRU input dimension (and all downstream weights) to a fixed `n_zones`, causing observation space mismatches at phase transitions. The new extractor uses:
33
- - Shared per-zone GRU processing
34
- - Learned attention + max pooling across the variable zone axis
35
- - Separate basin-scale context (ENSO, IOD, etc.)
36
 
37
- This design transfers **61 of 63 parameter tensors** cleanly when `n_zones` changes (only the final action head is reinitialized). Full 5-phase curriculum runs confirm that phases sharing the same `n_zones` inherit competence immediately, while phases after an entity-count change only need to relearn the action head.
38
 
39
- ## Validation Results
40
 
41
- Full 5-phase curriculum (690k steps) completed successfully on GPU:
 
 
 
 
 
 
 
 
42
 
43
- | Transition | n_zones change | Tensors transferred | Behavior |
44
- |-------------------------|----------------|---------------------|------------------------------|
45
- | normal β†’ monsoon | 2 β†’ 3 | 61/63 | Action head reinitialized |
46
- | monsoon β†’ drought | 3 β†’ 3 | 63/63 | Full transfer |
47
- | drought β†’ heatwave | 3 β†’ 4 | 61/63 | Action head reinitialized |
48
- | heatwave β†’ humidity | 4 β†’ 4 | 63/63 | Full transfer |
49
 
50
- All phases converged to their structural performance ceiling (`n_zones + 1`). Full-transfer phases started at ceiling performance immediately. Explained variance stayed in the healthy 0.6–0.8 range. The entire curriculum finished in ~50 minutes on one T4.
 
51
 
52
- ## How to Use
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- ### Training
55
- ```bash
56
- python train_curriculum.py --phase all --output-dir ./runs --device cuda --seed 42
57
- Inference (after training)
58
- Pythonfrom sb3_contrib import MaskablePPO
59
- model = MaskablePPO.load("runs/humidity/models/final_humidity.zip")
60
- Edge Export
61
- Bashpython mnn_export.py --checkpoint runs/humidity/models/final_humidity.zip \
62
- --output weather_rl_model.mnn --quantize int8 --n-zones 4
63
  Dependencies
 
 
 
 
 
64
 
65
- torch >= 2.0
66
- stable-baselines3 >= 2.0
67
- sb3-contrib >= 2.0
68
- gymnasium >= 0.29
69
- numpy
70
 
71
- GPU recommended for training. The edge runtime (edge_wrapper.cpp) targets Vulkan with CPU fallback.
 
 
1
  ---
2
  license: agpl-3.0
3
+ library_name: stable-baselines3
4
  tags:
5
+ - deep-reinforcement-learning
6
+ - agricultural-ai
7
+ - weather-modelling
8
+ - curriculum-learning
9
+ - edge-ai
10
+ pipeline_tag: reinforcement-learning
 
11
  ---
12
 
13
+ # Agricultural Weather-Risk RL β€” MaskablePPO + Permutation-Invariant GRU
14
 
15
+ Budget-constrained multi-zone weather-risk inspection for Indonesian rice
16
+ zones. A Gymnasium environment tracks per-zone beliefs under a scarce
17
+ inspection budget; a **permutation-invariant GRU** feature extractor supports
18
+ curriculum phases with changing `n_zones`; deterministic crop-risk scoring
19
+ grounds rewards and product alerts. Intended path includes ONNX β†’ MNN export
20
+ for edge inference.
21
+
22
+ This card describes the training stack and architecture. It is a research
23
+ codebase, not a production alerting service.
24
 
25
  ## What is included
26
 
27
+ - `weather_forecast_env.py` β€” Gymnasium env (belief map, action masks, triage/scarce/full budget modes, per-episode zone-order shuffle, real `EpisodeContext` injection)
28
+ - `gru_weather_policy.py` β€” Permutation-invariant GRU extractor (shared per-zone processing + aggregation; most weights independent of `n_zones`)
29
+ - `train_curriculum.py` / `train_kaggle.py` β€” Curriculum and standalone training (`--budget-mode triage|scarce|full`)
30
+ - `crop_risk_scorer.py` β€” Deterministic, economics-calibrated risk scoring
31
+ - `climatology.py` + `indonesia_zones.py` β€” Per-zone climatology and 14 grounded Indonesian agricultural zones
32
+ - `evaluate_checkpoint_real.py` β€” Real-trajectory eval (L1 labels, belief Ξ”, zero_inspect control)
33
+ - `mnn_export.py` + `edge_wrapper.cpp` β€” ONNX/MNN export and C++ edge runtime with external GRU hidden state
 
34
 
35
+ ## Key technical note
36
 
37
+ The GRU extractor is designed so that changing `n_zones` across curriculum
38
+ phases only requires reinitializing the action head; the bulk of the policy
39
+ parameters transfer. That enables multi-phase runs without full restarts when
40
+ entity count changes.
41
 
42
+ **Full-tour episode length (`n_zones + 1`) is not evidence of zone-selection
43
+ skill.** Under triage (`max_steps = max(1, n_zones - 1)`), the agent must leave
44
+ at least one zone unvisited. Allocation claims require a triage-trained
45
+ checkpoint evaluated under shuffle, not full-tour belief or length metrics.
46
 
47
+ ## How to use
48
 
49
+ ### Train (triage default)
50
 
51
+ ```bash
52
+ python train_kaggle.py \
53
+ --out ./run_nz2_triage \
54
+ --n-zones 2 \
55
+ --budget-mode triage \
56
+ --steps 100000 \
57
+ --clean-episode-ratio 0.90 \
58
+ --event-spatial-correlation 0.85 \
59
+ --device auto
60
 
 
 
 
 
 
 
61
 
62
+ Load and act
63
+ Pythonfrom sb3_contrib import MaskablePPO
64
 
65
+ model = MaskablePPO.load("run_nz2_triage/final_model.zip")
66
+ # obs from WeatherForecastEnv; pass action_masks= for MaskablePPO
67
+ action, _ = model.predict(obs, action_masks=masks, deterministic=True)
68
+
69
+
70
+ Edge export (optional)
71
+ Bashpython mnn_export.py \
72
+ --checkpoint run_nz2_triage/final_model.zip \
73
+ --output weather_rl_model.mnn \
74
+ --quantize int8 \
75
+ --n-zones 2
76
+ Validation (what is and is not shown)
77
+
78
+ ClaimStatusWeight transfer across n_zones changes (most tensors)Supported by curriculum designFull-tour training converges on length / belief movementObserved on earlier runs; not allocation skillTriage + shuffle avoids fixed physical zone lock-inObserved (slot preference can remain)Risk-conditioned zone choice under triageNot yet demonstratedProduct skill vs curated L1 impact labelsScorer/product path exists; quote only with L1 density caveats
79
+ Synthetic triage EV under current defaults (cleanβ‰ˆ0.90, max_steps=1) shows
80
+ only a weak dirty-day advantage for selecting the correct zone; overall return
81
+ still favours always-inspect. Longer training under the same reward does not
82
+ substitute for a stronger selection signal or calibrated clean ratio.
83
+ Limitations
84
+
85
+ Research stack, not SLA. No on-call, feed uptime, or regulatory certification.
86
+ Synthetic-heavy training. Real claims need held-out product metrics and L1 coverage honesty (unlabeled days excluded from P/R/F1).
87
+ Full-tour β‰  triage. Do not cite full-tour belief_R or ep_len β†’ n_zones+1 as zone-selection skill.
88
+ Triage allocation open. A triage-trained policy under shuffle has not been shown to beat zero_inspect on L1 belief movement and choose zones by contemporaneous risk rather than a fixed action slot.
89
+ Edge path is export-oriented. MNN/C++ wrapper is provided; not validated here as a production edge deployment.
90
 
 
 
 
 
 
 
 
 
 
91
  Dependencies
92
+ texttorch>=2.0
93
+ gymnasium>=0.29
94
+ stable-baselines3>=2.0
95
+ sb3-contrib>=2.0
96
+ numpy>=1.24
97
 
98
+ GPU recommended for training; CPU is viable for short runs and inference.
 
 
 
 
99
 
100
+ License
101
+ AGPL-3.0. See repository for full terms.