This commit is contained in:
Dobromir Popov
2025-06-24 21:25:20 +03:00
parent e7ea17b626
commit f47cf52ae1

View File

@ -1395,13 +1395,14 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
predictions = prediction_result.get('probabilities', [0.33, 0.33, 0.34]) predictions = prediction_result.get('probabilities', [0.33, 0.33, 0.34])
confidence = prediction_result.get('confidence', 0.7) confidence = prediction_result.get('confidence', 0.7)
# Convert predictions to numpy array first # Convert predictions to numpy array first using safe conversion
if isinstance(predictions, np.ndarray): if isinstance(predictions, np.ndarray):
predictions_array = predictions.flatten() predictions_array = predictions.flatten()
elif isinstance(predictions, (list, tuple)): elif isinstance(predictions, (list, tuple)):
predictions_array = np.array(predictions, dtype=np.float32).flatten() predictions_array = np.array(predictions, dtype=np.float32).flatten()
else: else:
predictions_array = np.array([float(predictions)], dtype=np.float32) # Use safe tensor conversion for single values
predictions_array = np.array([self._safe_tensor_to_scalar(predictions, 0.5)], dtype=np.float32)
# Create final predictions array with confidence # Create final predictions array with confidence
# Use safe tensor conversion to avoid scalar conversion errors # Use safe tensor conversion to avoid scalar conversion errors
@ -1416,7 +1417,7 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
# Handle (pred_class, pred_proba) tuple from CNN models # Handle (pred_class, pred_proba) tuple from CNN models
pred_class, pred_proba = prediction_result pred_class, pred_proba = prediction_result
# Flatten and process the probability array # Flatten and process the probability array using safe conversion
if isinstance(pred_proba, np.ndarray): if isinstance(pred_proba, np.ndarray):
if pred_proba.ndim > 1: if pred_proba.ndim > 1:
# Handle 2D arrays like [[0.1, 0.2, 0.7]] # Handle 2D arrays like [[0.1, 0.2, 0.7]]
@ -1428,16 +1429,17 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
# Use the probability values as the predictions array # Use the probability values as the predictions array
predictions = pred_proba_flat.astype(np.float32) predictions = pred_proba_flat.astype(np.float32)
else: else:
# Fallback: use class prediction only # Fallback: use class prediction with safe conversion
predictions = np.array([float(pred_class)], dtype=np.float32) predictions = np.array([self._safe_tensor_to_scalar(pred_class, 0.5)], dtype=np.float32)
else: else:
# Handle direct prediction result # Handle direct prediction result using safe conversion
if isinstance(prediction_result, np.ndarray): if isinstance(prediction_result, np.ndarray):
predictions = prediction_result.flatten() predictions = prediction_result.flatten()
elif isinstance(prediction_result, (list, tuple)): elif isinstance(prediction_result, (list, tuple)):
predictions = np.array(prediction_result, dtype=np.float32).flatten() predictions = np.array(prediction_result, dtype=np.float32).flatten()
else: else:
predictions = np.array([float(prediction_result)], dtype=np.float32) # Use safe tensor conversion for single tensor/scalar values
predictions = np.array([self._safe_tensor_to_scalar(prediction_result, 0.5)], dtype=np.float32)
# Extract hidden features if model supports it # Extract hidden features if model supports it
hidden_features = None hidden_features = None
@ -4740,7 +4742,8 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
Python float scalar value Python float scalar value
""" """
try: try:
if hasattr(tensor_value, 'item'): # Handle PyTorch tensors first
if hasattr(tensor_value, 'numel') and hasattr(tensor_value, 'item'):
# PyTorch tensor - handle different shapes # PyTorch tensor - handle different shapes
if tensor_value.numel() == 1: if tensor_value.numel() == 1:
return float(tensor_value.item()) return float(tensor_value.item())
@ -4754,9 +4757,12 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
return float(tensor_value.flatten()[0]) return float(tensor_value.flatten()[0])
else: else:
return float(tensor_value.flat[0]) return float(tensor_value.flat[0])
elif hasattr(tensor_value, 'item') and not isinstance(tensor_value, np.ndarray):
# Other tensor types that have .item() method
return float(tensor_value.item())
else: else:
# Already a scalar value # Already a scalar value
return float(tensor_value) return float(tensor_value)
except Exception as e: except Exception as e:
logger.warning(f"Error converting tensor to scalar, using default {default_value}: {e}") logger.debug(f"Error converting tensor to scalar, using default {default_value}: {e}")
return default_value return default_value