Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -271,42 +271,40 @@ def predict_prices(data, model, tokenizer, prediction_days=30):
|
|
| 271 |
input_sequence = prices[-context_length:]
|
| 272 |
|
| 273 |
# Create prediction input
|
| 274 |
-
# CRITICAL FIX:
|
| 275 |
-
prediction_input = torch.tensor(input_sequence).unsqueeze(0).
|
| 276 |
|
| 277 |
# Generate predictions
|
| 278 |
with torch.no_grad():
|
| 279 |
-
#
|
| 280 |
-
# Model will use its default configuration for generation length.
|
| 281 |
forecast = model.generate(
|
| 282 |
prediction_input,
|
| 283 |
)
|
| 284 |
|
| 285 |
-
#
|
| 286 |
|
| 287 |
# 1. Get the actual tensor from the tuple/list if necessary
|
| 288 |
output_tensor = forecast[0] if isinstance(forecast, (tuple, list)) else forecast
|
| 289 |
|
| 290 |
-
# 2. Average across the samples (dim=1) and
|
| 291 |
-
#
|
| 292 |
-
#
|
| 293 |
-
|
| 294 |
-
predictions = output_tensor.mean(dim=1).squeeze().cpu().numpy()
|
| 295 |
|
| 296 |
# Handle case where predictions is a single scalar (convert to array for safety)
|
| 297 |
if predictions.ndim == 0:
|
| 298 |
predictions = np.array([predictions.item()])
|
| 299 |
|
| 300 |
-
#
|
|
|
|
|
|
|
|
|
|
| 301 |
last_price = prices[-1]
|
| 302 |
predicted_high = np.max(predictions)
|
| 303 |
predicted_low = np.min(predictions)
|
| 304 |
predicted_mean = np.mean(predictions)
|
| 305 |
change_pct = ((predicted_mean - last_price) / last_price) * 100
|
| 306 |
|
| 307 |
-
# Use actual prediction length from the output tensor
|
| 308 |
-
pred_len = len(predictions)
|
| 309 |
-
|
| 310 |
return {
|
| 311 |
'values': predictions,
|
| 312 |
'dates': pd.date_range(
|
|
|
|
| 271 |
input_sequence = prices[-context_length:]
|
| 272 |
|
| 273 |
# Create prediction input
|
| 274 |
+
# CRITICAL FIX: Cast input to LongTensor to satisfy model embedding layer expectation.
|
| 275 |
+
prediction_input = torch.tensor(input_sequence).unsqueeze(0).long().to(model.device)
|
| 276 |
|
| 277 |
# Generate predictions
|
| 278 |
with torch.no_grad():
|
| 279 |
+
# NOTE: Removed prediction_length from generate call as it was causing failures.
|
|
|
|
| 280 |
forecast = model.generate(
|
| 281 |
prediction_input,
|
| 282 |
)
|
| 283 |
|
| 284 |
+
# Handle complex Chronos output: [batch_size, num_samples, prediction_length]
|
| 285 |
|
| 286 |
# 1. Get the actual tensor from the tuple/list if necessary
|
| 287 |
output_tensor = forecast[0] if isinstance(forecast, (tuple, list)) else forecast
|
| 288 |
|
| 289 |
+
# 2. Average across the samples (dim=1) and convert to a simple 1D numpy array
|
| 290 |
+
# predictions = output_tensor.mean(dim=1).squeeze().cpu().numpy()
|
| 291 |
+
# To avoid unexpected dimensions, let's simplify averaging:
|
| 292 |
+
predictions = output_tensor.float().mean(dim=1).squeeze().cpu().numpy()
|
|
|
|
| 293 |
|
| 294 |
# Handle case where predictions is a single scalar (convert to array for safety)
|
| 295 |
if predictions.ndim == 0:
|
| 296 |
predictions = np.array([predictions.item()])
|
| 297 |
|
| 298 |
+
# Use actual prediction length from the output tensor
|
| 299 |
+
pred_len = len(predictions)
|
| 300 |
+
|
| 301 |
+
# Calculate prediction statistics
|
| 302 |
last_price = prices[-1]
|
| 303 |
predicted_high = np.max(predictions)
|
| 304 |
predicted_low = np.min(predictions)
|
| 305 |
predicted_mean = np.mean(predictions)
|
| 306 |
change_pct = ((predicted_mean - last_price) / last_price) * 100
|
| 307 |
|
|
|
|
|
|
|
|
|
|
| 308 |
return {
|
| 309 |
'values': predictions,
|
| 310 |
'dates': pd.date_range(
|