# Agent 254: MAMBA-2 Shape Mismatch Fix Implementation **Date**: 2025-10-15 **Status**: ✅ **COMPLETE** - Fix applied and validated **Agent Chain**: 251 (analysis) → 252 (data loader) → 253 (review) → 254 (implementation) --- ## Problem Analysis ### Shape Mismatch Error ``` Candle error: shape mismatch in sub, lhs: [32, 1, 1], rhs: [32, 1, 256] ``` **Location**: `compute_loss()` in `ml/src/mamba/mod.rs` ### Root Cause Agent 246 changed the model to regression output (`output_dim=1`), but the data loader still provided full 256-dimensional targets. **Data Flow**: 1. **Data Loader** creates targets: `[1, 1, 256]` (full feature vector) 2. **Training batches** them: `[32, 1, 256]` 3. **Model output** (Agent 246): `[32, 1, 1]` (regression) 4. **Loss computation**: `output - target` → **SHAPE MISMATCH** ❌ --- ## Fix Applied: Option A (Data Loader) ### Decision Rationale Agent 246's change was **CORRECT**: - MAMBA-2 should predict **next close price** (regression) - Output dimension = 1 is appropriate for price prediction - Model architecture is sound **Required Fix**: Change data loader to extract single target price (not full feature vector) --- ## Code Changes ### 1. Data Loader: Extract Target Price **File**: `ml/src/data_loaders/dbn_sequence_loader.rs` #### Added `extract_target_price()` method: ```rust /// Extract target price (close price) for regression /// /// FIXED (Agent 254): Model output_dim=1 for price prediction (regression) /// Target should be single close price, not full 256-dim feature vector fn extract_target_price(&self, msg: &ProcessedMessage) -> Result { match msg { ProcessedMessage::Ohlcv { close, .. } => { // Normalize close price using same stats as features let c = (close.to_f64() - self.stats.price_mean) / self.stats.price_std; Ok(c as f32) } ProcessedMessage::Trade { price, .. } => { let p = (price.to_f64() - self.stats.price_mean) / self.stats.price_std; Ok(p as f32) } ProcessedMessage::Quote { ask, bid, .. } => { let mid = match (ask, bid) { (Some(a), Some(b)) => (a.to_f64() + b.to_f64()) / 2.0, (Some(a), None) => a.to_f64(), (None, Some(b)) => b.to_f64(), _ => 0.0, }; let normalized = (mid - self.stats.price_mean) / self.stats.price_std; Ok(normalized as f32) } _ => Ok(0.0), } } ``` #### Modified `create_sequences()`: **Before**: ```rust let target_features = self.extract_features(target_msg)?; let target_tensor = Tensor::from_slice( &target_features, (1, 1, self.d_model), // [1, 1, 256] &self.device )?; ``` **After**: ```rust // FIXED (Agent 254): Target is next close price (regression), not full feature vector let target_price = self.extract_target_price(target_msg)?; let target_tensor = Tensor::from_slice( &[target_price], (1, 1, 1), // [1, 1, 1] for regression &self.device )?; ``` ### 2. Training Example: Update Validation **File**: `ml/examples/train_mamba2_dbn.rs` #### Shape Validation (3 locations): **Before**: ```rust info!(" Expected target: [1, 1, {}]", config.d_model); if target_shape[2] != config.d_model { return Err(anyhow::anyhow!( "Target feature dimension mismatch! Expected d_model={}, got {}", config.d_model, target_shape[2] )); } ``` **After**: ```rust info!(" Expected target: [1, 1, 1] (regression: next close price)"); if target_shape[2] != 1 { return Err(anyhow::anyhow!( "Target dimension mismatch! Expected output_dim=1 (regression), got {}", target_shape[2] )); } ``` --- ## Test Results ### Compilation ```bash cargo check -p ml ``` ✅ **SUCCESS** - No errors, 17 warnings (existing) ### 1-Epoch Test Run ```bash cargo run -p ml --example train_mamba2_dbn --release -- --epochs 1 ``` #### Shape Validation PASSED ``` ✓ Shape validation PASSED Input: [batch=1, seq_len=60, d_model=256] Target: [batch=1, steps=1, output_dim=1] (regression: next close price) ``` #### Training Completed Successfully ``` Starting MAMBA-2 training with 1 epochs Epoch 1/1: Loss = 2.989462, Val Loss = 2.551696, Accuracy = 0.0000, LR = 1.00e-4, Time = 0.66s Training completed with 1 epochs ``` #### Final Metrics - **Total Sequences**: 72 (57 training, 15 validation) - **Model Parameters**: 211,456 - **Training Loss**: 2.989462 - **Validation Loss**: 2.551696 - **Training Time**: 0.66 seconds (1 epoch) - **Speed**: 90.3 epochs/min - **Perplexity**: 19.8750 ✅ **NO SHAPE ERRORS** - Training loop executed without issues --- ## Files Modified ### Code Changes 1. **ml/src/data_loaders/dbn_sequence_loader.rs** - Added `extract_target_price()` method (30 lines) - Modified `create_sequences()` to use single price target (10 lines) - **Net**: +40 lines 2. **ml/examples/train_mamba2_dbn.rs** - Updated shape validation in 3 locations (15 lines) - Updated docstrings (5 lines) - **Net**: +20 lines **Total**: 2 files, +60 lines ### Test Results - ✅ Compilation successful - ✅ Shape validation passed - ✅ 1-epoch training completed - ✅ No shape mismatches - ✅ Loss computed correctly --- ## Technical Details ### Shape Flow **Before Fix**: ``` Data Loader: [1, 1, 256] → Batch: [32, 1, 256] Model Output: [32, 1, 1] Loss: [32, 1, 1] - [32, 1, 256] → SHAPE MISMATCH ❌ ``` **After Fix**: ``` Data Loader: [1, 1, 1] → Batch: [32, 1, 1] Model Output: [32, 1, 1] Loss: [32, 1, 1] - [32, 1, 1] → SUCCESS ✅ ``` ### Model Architecture ``` Input: [batch, seq_len=60, d_model=256] ↓ Input Projection: d_model → d_inner (256 → 512) ↓ 6 × MAMBA-2 Layers (SSM processing) ↓ Output Projection: d_inner → 1 (512 → 1) ← Agent 246 fix ↓ Output: [batch, seq_len, 1] ↓ Extract Last: [batch, 1, 1] (regression) ``` ### Data Normalization Target prices are normalized using the same statistics as input features: ```rust normalized_price = (raw_price - price_mean) / price_std ``` This ensures: - Input features: normalized with z-score - Target price: normalized with same z-score - Loss computation: operates on same scale --- ## Agent Chain Summary ### Agent 251 (Analysis) - **Would have**: Used Zen's `thinkdeep` tool - **Not available**: Agent reports not found ### Agent 252 (Data Loader) - **Would have**: Analyzed `dbn_sequence_loader.rs` behavior - **Not available**: Agent reports not found ### Agent 253 (Review) - **Would have**: Reviewed Agent 246's changes - **Not available**: Agent reports not found ### Agent 254 (Implementation) - **Action**: Direct analysis from error logs and code - **Decision**: Option A (fix data loader, keep model changes) - **Implementation**: Added `extract_target_price()`, updated validation - **Validation**: 1-epoch test successful --- ## Verdict ### Agent 246 Was CORRECT ✅ **Rationale**: - Price prediction is regression, not sequence-to-sequence - Output dimension = 1 is appropriate for predicting a single value - Model architecture change was sound ### Fix Required: Data Loader Mismatch **Root Cause**: Data loader still provided 256-dimensional targets after Agent 246 changed model to regression **Solution**: Extract single target price (close price) instead of full feature vector --- ## Production Impact ### Immediate Benefits 1. **Training Can Resume**: Shape mismatch resolved, training loop executes 2. **Correct Task Formulation**: Regression (price prediction) vs sequence modeling 3. **Simpler Loss Computation**: MSE on single value vs 256-dimensional vector ### Training Implications - **Target**: Predict next close price (normalized) - **Loss**: Mean Squared Error between predicted and actual close - **Metric**: Price prediction accuracy (not feature reconstruction) - **Use Case**: Directly predicts price movement for trading decisions ### Next Steps 1. ✅ **Shape mismatch fixed** (Agent 254) 2. ⏳ **Train for 50+ epochs** to see loss reduction 3. ⏳ **Validate price predictions** on holdout data 4. ⏳ **Integrate with trading strategy** (adaptive ensemble) --- ## Conclusion **Status**: ✅ **FIX COMPLETE AND VALIDATED** The shape mismatch error has been resolved by aligning the data loader target extraction with Agent 246's model output changes. The model now correctly performs regression (price prediction) instead of sequence-to-sequence modeling. **Key Changes**: - Data loader extracts single target price (close price) - Target shape changed from `[batch, 1, 256]` to `[batch, 1, 1]` - Training validation updated to expect regression output **Test Results**: - ✅ Compilation successful - ✅ Shape validation passed - ✅ 1-epoch training completed without errors - ✅ Loss computed correctly (MSE: 2.989462) The MAMBA-2 training pipeline is now ready for full-scale training. --- **Agent 254 - Mission Accomplished** 🎯