Tonic commited on
Commit
da0507e
·
1 Parent(s): 027a49e

tensor formatting solution , shape handling, device management, restore prediction extension functionality

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -316,8 +316,8 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
316
  # Take the most recent data points
317
  normalized_prices = normalized_prices[-min_data_points:]
318
 
319
- # Reshape for Chronos (batch_size=1, sequence_length, features=1)
320
- context = torch.tensor(normalized_prices.reshape(1, -1, 1), dtype=torch.float32)
321
 
322
  # Make prediction with GPU acceleration
323
  pipe = load_pipeline()
@@ -346,16 +346,16 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
346
  print(f"Attempting prediction with context shape: {context.shape}")
347
  print(f"Prediction length: {actual_prediction_length}")
348
 
349
- # Convert context to the correct format
350
- context_tensor = context.to(device=pipe.model.device, dtype=torch.float16)
351
 
352
- # Ensure context is properly formatted
353
- if len(context_tensor.shape) != 3:
354
- raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
355
 
356
- # Use predict_quantiles directly
357
  quantiles, mean = pipe.predict_quantiles(
358
- context=context_tensor,
359
  prediction_length=actual_prediction_length,
360
  quantile_levels=[0.1, 0.5, 0.9]
361
  )
@@ -377,6 +377,14 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
377
  # Calculate standard deviation from quantiles
378
  std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
379
 
 
 
 
 
 
 
 
 
380
  except Exception as e:
381
  print(f"Chronos prediction error: {str(e)}")
382
  print(f"Error type: {type(e)}")
 
316
  # Take the most recent data points
317
  normalized_prices = normalized_prices[-min_data_points:]
318
 
319
+ # Convert to tensor and ensure proper shape
320
+ context = torch.tensor(normalized_prices, dtype=torch.float32)
321
 
322
  # Make prediction with GPU acceleration
323
  pipe = load_pipeline()
 
346
  print(f"Attempting prediction with context shape: {context.shape}")
347
  print(f"Prediction length: {actual_prediction_length}")
348
 
349
+ # Move context to the same device as the model
350
+ context = context.to(device=pipe.model.device)
351
 
352
+ # Ensure context is properly formatted for Chronos
353
+ if len(context.shape) == 1:
354
+ context = context.unsqueeze(0) # Add batch dimension
355
 
356
+ # Use predict_quantiles with proper formatting
357
  quantiles, mean = pipe.predict_quantiles(
358
+ context=context,
359
  prediction_length=actual_prediction_length,
360
  quantile_levels=[0.1, 0.5, 0.9]
361
  )
 
377
  # Calculate standard deviation from quantiles
378
  std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
379
 
380
+ # If we had to limit the prediction length, extend the prediction
381
+ if actual_prediction_length < prediction_days:
382
+ last_pred = mean_pred[-1]
383
+ last_std = std_pred[-1]
384
+ extension = np.array([last_pred * (1 + np.random.normal(0, last_std, prediction_days - actual_prediction_length))])
385
+ mean_pred = np.concatenate([mean_pred, extension])
386
+ std_pred = np.concatenate([std_pred, np.full(prediction_days - actual_prediction_length, last_std)])
387
+
388
  except Exception as e:
389
  print(f"Chronos prediction error: {str(e)}")
390
  print(f"Error type: {type(e)}")