From e7ea17b62626c8c1eab8e93ccf49436b8503211d Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 24 Jun 2025 21:13:06 +0300 Subject: [PATCH] fixed other CNN references --- NN/models/cnn_model_pytorch.py | 4 +- NN/models/enhanced_cnn_with_orderbook.py | 11 ++++- core/enhanced_orchestrator.py | 51 +++++++++++++++++------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/NN/models/cnn_model_pytorch.py b/NN/models/cnn_model_pytorch.py index be540a0..bc1372d 100644 --- a/NN/models/cnn_model_pytorch.py +++ b/NN/models/cnn_model_pytorch.py @@ -431,11 +431,11 @@ class EnhancedCNNModel(nn.Module): return { 'action': action, 'action_name': 'BUY' if action == 0 else 'SELL', - 'confidence': float(confidence), + 'confidence': confidence, # Already converted to float above 'action_confidence': action_confidence, 'probabilities': probs.tolist(), 'regime_probabilities': regime.tolist(), - 'volatility_prediction': float(volatility), + 'volatility_prediction': volatility, # Already converted to float above 'raw_logits': outputs['logits'].cpu().numpy()[0].tolist() } diff --git a/NN/models/enhanced_cnn_with_orderbook.py b/NN/models/enhanced_cnn_with_orderbook.py index 2e0ed6b..f4e722d 100644 --- a/NN/models/enhanced_cnn_with_orderbook.py +++ b/NN/models/enhanced_cnn_with_orderbook.py @@ -501,7 +501,16 @@ class EnhancedCNNWithOrderBook(nn.Module): # Get probabilities q_values = outputs['q_values'] probs = F.softmax(q_values, dim=1) - confidence = outputs['confidence'].item() + + # Handle confidence shape properly to avoid scalar conversion errors + confidence_tensor = outputs['confidence'] + if isinstance(confidence_tensor, torch.Tensor): + if confidence_tensor.numel() == 1: + confidence = confidence_tensor.item() + else: + confidence = confidence_tensor.flatten()[0].item() + else: + confidence = float(confidence_tensor) # Action selection with confidence thresholding if confidence >= self.confidence_threshold: diff --git a/core/enhanced_orchestrator.py b/core/enhanced_orchestrator.py index cff2c78..6bb6462 100644 --- a/core/enhanced_orchestrator.py +++ b/core/enhanced_orchestrator.py @@ -1404,19 +1404,8 @@ class EnhancedTradingOrchestrator(TradingOrchestrator): predictions_array = np.array([float(predictions)], dtype=np.float32) # Create final predictions array with confidence - # Ensure confidence is a scalar value - handle all array shapes safely - if isinstance(confidence, np.ndarray): - if confidence.ndim == 0: - # 0-dimensional array (scalar) - confidence_scalar = float(confidence.item()) - elif confidence.size == 1: - # 1-element array - confidence_scalar = float(confidence.item()) - else: - # Multi-element array - take first element or mean - confidence_scalar = float(confidence.flat[0]) # Use flat[0] to safely get first element - else: - confidence_scalar = float(confidence) + # Use safe tensor conversion to avoid scalar conversion errors + confidence_scalar = self._safe_tensor_to_scalar(confidence, default_value=0.7) # Combine predictions and confidence as separate elements predictions = np.concatenate([ @@ -4736,4 +4725,38 @@ class EnhancedTradingOrchestrator(TradingOrchestrator): 'price_to_pivot_ratio': 1.0, 'volume_strength': 1.0, 'pivot_strength': 0.5 - } \ No newline at end of file + } + + # Helper function to safely extract scalar values from tensors + def _safe_tensor_to_scalar(self, tensor_value, default_value: float = 0.7) -> float: + """ + Safely convert tensor/array values to Python scalar floats + + Args: + tensor_value: Input tensor, array, or scalar value + default_value: Default value to return if conversion fails + + Returns: + Python float scalar value + """ + try: + if hasattr(tensor_value, 'item'): + # PyTorch tensor - handle different shapes + if tensor_value.numel() == 1: + return float(tensor_value.item()) + else: + return float(tensor_value.flatten()[0].item()) + elif isinstance(tensor_value, np.ndarray): + # NumPy array - handle different shapes + if tensor_value.ndim == 0: + return float(tensor_value.item()) + elif tensor_value.size == 1: + return float(tensor_value.flatten()[0]) + else: + return float(tensor_value.flat[0]) + else: + # Already a scalar value + return float(tensor_value) + except Exception as e: + logger.warning(f"Error converting tensor to scalar, using default {default_value}: {e}") + return default_value \ No newline at end of file