Tonic commited on
Commit
fab022d
·
1 Parent(s): 9268db8

try to implement chronos and improve error messages

Browse files
Files changed (1) hide show
  1. app.py +69 -59
app.py CHANGED
@@ -346,84 +346,94 @@ 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
- # First try with predict_quantiles
 
 
 
 
 
 
 
350
  try:
351
- print("Trying predict_quantiles...")
352
- # Convert context to the correct format
353
- context_tensor = context.to(device=pipe.model.device, dtype=torch.float16)
354
-
355
- # Ensure context is properly formatted
356
- if len(context_tensor.shape) != 3:
357
- raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
358
-
359
- # Try with default configuration first
360
- try:
361
- quantiles, mean = pipe.predict_quantiles(
362
- context=context_tensor,
363
- prediction_length=actual_prediction_length
364
- )
365
- except Exception as e:
366
- print(f"Default predict_quantiles failed: {str(e)}")
367
- # Try with explicit quantile levels
368
- quantiles, mean = pipe.predict_quantiles(
369
- context=context_tensor,
370
- prediction_length=actual_prediction_length,
371
- quantile_levels=[0.1, 0.5, 0.9] # 10th, 50th, and 90th percentiles
372
- )
373
-
374
- if quantiles is None or mean is None:
375
- raise ValueError("Chronos returned empty prediction")
376
 
377
- print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
 
378
 
379
- # Convert to numpy arrays
380
- quantiles = quantiles.detach().cpu().numpy()
381
- mean = mean.detach().cpu().numpy()
382
 
383
- # Denormalize predictions
384
- mean_pred = scaler.inverse_transform(mean.reshape(-1, 1)).flatten()
385
- lower_bound = scaler.inverse_transform(quantiles[0, :, 0].reshape(-1, 1)).flatten()
386
- upper_bound = scaler.inverse_transform(quantiles[0, :, 2].reshape(-1, 1)).flatten()
387
 
388
- # Calculate standard deviation from quantiles
389
- std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
 
390
 
391
  except Exception as e:
392
- print(f"predict_quantiles failed with error: {str(e)}")
393
- print("Falling back to predict...")
394
-
395
- # Fallback to predict if predict_quantiles fails
396
- # Convert context to the correct format
397
- context_tensor = context.to(device=pipe.model.device, dtype=torch.float16)
398
 
399
- # Try with default configuration first
400
  try:
401
- prediction = pipe.predict(
402
  context=context_tensor,
403
- prediction_length=actual_prediction_length
 
404
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  except Exception as e:
406
- print(f"Default predict failed: {str(e)}")
407
- # Try with explicit num_samples
 
 
408
  prediction = pipe.predict(
409
  context=context_tensor,
410
  prediction_length=actual_prediction_length,
411
  num_samples=100
412
  )
413
-
414
- if prediction is None:
415
- raise ValueError("Chronos predict returned None")
416
 
417
- prediction = prediction.detach().cpu().numpy()
418
-
419
- if prediction.size == 0:
420
- raise ValueError("Chronos predict returned empty array")
421
 
422
- print(f"Prediction shape: {prediction.shape}")
423
-
424
- # Calculate mean and std from samples
425
- mean_pred = scaler.inverse_transform(prediction.mean(axis=0).reshape(-1, 1)).flatten()
426
- std_pred = prediction.std(axis=0) * (scaler.data_max_ - scaler.data_min_)
 
 
 
427
 
428
  # If we had to limit the prediction length, extend the prediction
429
  if actual_prediction_length < prediction_days:
 
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
+ # Try with base configuration
357
  try:
358
+ print("Trying base configuration...")
359
+ # Use the base configuration from the model
360
+ prediction = pipe(
361
+ context_tensor,
362
+ max_length=actual_prediction_length,
363
+ num_return_sequences=1,
364
+ do_sample=True,
365
+ temperature=1.0,
366
+ top_p=0.9,
367
+ return_dict_in_generate=True,
368
+ output_scores=True
369
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
+ if prediction is None:
372
+ raise ValueError("Model returned None")
373
 
374
+ # Extract the generated sequence
375
+ generated_sequence = prediction.sequences[0]
 
376
 
377
+ # Convert to numpy and reshape
378
+ prediction_np = generated_sequence.detach().cpu().numpy()
 
 
379
 
380
+ # Calculate mean and std
381
+ mean_pred = scaler.inverse_transform(prediction_np.reshape(-1, 1)).flatten()
382
+ std_pred = np.std(prediction_np) * (scaler.data_max_ - scaler.data_min_)
383
 
384
  except Exception as e:
385
+ print(f"Base configuration failed: {str(e)}")
386
+ print("Falling back to predict_quantiles...")
 
 
 
 
387
 
388
+ # Try with predict_quantiles
389
  try:
390
+ quantiles, mean = pipe.predict_quantiles(
391
  context=context_tensor,
392
+ prediction_length=actual_prediction_length,
393
+ quantile_levels=[0.1, 0.5, 0.9]
394
  )
395
+
396
+ if quantiles is None or mean is None:
397
+ raise ValueError("Chronos returned empty prediction")
398
+
399
+ print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
400
+
401
+ # Convert to numpy arrays
402
+ quantiles = quantiles.detach().cpu().numpy()
403
+ mean = mean.detach().cpu().numpy()
404
+
405
+ # Denormalize predictions
406
+ mean_pred = scaler.inverse_transform(mean.reshape(-1, 1)).flatten()
407
+ lower_bound = scaler.inverse_transform(quantiles[0, :, 0].reshape(-1, 1)).flatten()
408
+ upper_bound = scaler.inverse_transform(quantiles[0, :, 2].reshape(-1, 1)).flatten()
409
+
410
+ # Calculate standard deviation from quantiles
411
+ std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
412
+
413
  except Exception as e:
414
+ print(f"predict_quantiles failed: {str(e)}")
415
+ print("Falling back to predict...")
416
+
417
+ # Final fallback to predict
418
  prediction = pipe.predict(
419
  context=context_tensor,
420
  prediction_length=actual_prediction_length,
421
  num_samples=100
422
  )
 
 
 
423
 
424
+ if prediction is None:
425
+ raise ValueError("Chronos predict returned None")
426
+
427
+ prediction = prediction.detach().cpu().numpy()
428
 
429
+ if prediction.size == 0:
430
+ raise ValueError("Chronos predict returned empty array")
431
+
432
+ print(f"Prediction shape: {prediction.shape}")
433
+
434
+ # Calculate mean and std from samples
435
+ mean_pred = scaler.inverse_transform(prediction.mean(axis=0).reshape(-1, 1)).flatten()
436
+ std_pred = prediction.std(axis=0) * (scaler.data_max_ - scaler.data_min_)
437
 
438
  # If we had to limit the prediction length, extend the prediction
439
  if actual_prediction_length < prediction_days: