timefmtest.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import numpy as np
  2. from timesfm3 import TimesFM3Evaluator, ModelConfig
  3. # ============================================================
  4. # 1. 加载 TimesFM 3.0
  5. # ============================================================
  6. config = ModelConfig(
  7. checkpoint_path="google/timesfm-3.0-pytorch",
  8. device="cpu",
  9. )
  10. forecaster = TimesFM3Evaluator(config)
  11. # ============================================================
  12. # 2. 单变量预测
  13. # ============================================================
  14. print("=" * 60)
  15. print("单变量预测")
  16. print("=" * 60)
  17. context = np.sin(
  18. np.linspace(0, 20, 256)
  19. ).astype(np.float32)
  20. horizon = 24
  21. out = forecaster.predict(
  22. context,
  23. horizon=horizon,
  24. return_quantiles=True,
  25. )
  26. print("forecast shape :", out.forecast.shape)
  27. print("quantiles shape:", out.quantiles.shape)
  28. print("\n未来 24 步预测值:")
  29. print(out.forecast)
  30. # ============================================================
  31. # 3. 批量预测
  32. # ============================================================
  33. print("\n" + "=" * 60)
  34. print("批量预测")
  35. print("=" * 60)
  36. # 这里就是原来缺失的 ts1 / ts2 / ts3
  37. ts1 = np.sin(np.linspace(0, 20, 256)).astype(np.float32)
  38. ts2 = np.sin(np.linspace(2, 22, 256)).astype(np.float32)
  39. ts3 = np.sin(np.linspace(4, 24, 256)).astype(np.float32)
  40. contexts = [ts1, ts2, ts3]
  41. outs = list(
  42. forecaster.predict_batch(
  43. contexts,
  44. horizon=24,
  45. return_quantiles=True,
  46. )
  47. )
  48. for i, result in enumerate(outs, start=1):
  49. print(f"\n序列 {i}:")
  50. print("forecast shape :", result.forecast.shape)
  51. print("quantiles shape:", result.quantiles.shape)
  52. print("forecast:")
  53. print(result.forecast)
  54. # ============================================================
  55. # 4. 多变量 + Covariates
  56. # ============================================================
  57. print("\n" + "=" * 60)
  58. print("多变量 + Covariates")
  59. print("=" * 60)
  60. context_len = 128
  61. horizon = 24
  62. # ------------------------------------------------------------
  63. # 多变量目标
  64. # shape = (num_variates, context_len)
  65. # ------------------------------------------------------------
  66. target = np.stack([
  67. np.sin(np.linspace(0, 24, context_len)),
  68. np.sin(np.linspace(1, 26, context_len)),
  69. ]).astype(np.float32)
  70. # ------------------------------------------------------------
  71. # Past-only covariate
  72. #
  73. # 只有历史数据,没有未来数据
  74. #
  75. # shape = (num_covariates, context_len)
  76. # ------------------------------------------------------------
  77. past_only_cov = np.random.randn(
  78. 1,
  79. context_len
  80. ).astype(np.float32)
  81. # ------------------------------------------------------------
  82. # Past-future covariate
  83. #
  84. # 历史 + 未来都已知
  85. #
  86. # shape = (num_covariates, context_len + horizon)
  87. # ------------------------------------------------------------
  88. past_future_cov = np.sin(
  89. np.linspace(
  90. 0,
  91. 30,
  92. context_len + horizon
  93. )
  94. )[None, :].astype(np.float32)
  95. # ------------------------------------------------------------
  96. # 预测
  97. # ------------------------------------------------------------
  98. out = forecaster.predict(
  99. target,
  100. horizon=horizon,
  101. past_only_covariates=past_only_cov,
  102. past_future_covariates=past_future_cov,
  103. return_quantiles=True,
  104. )
  105. print("forecast shape :", out.forecast.shape)
  106. print("quantiles shape:", out.quantiles.shape)
  107. # ============================================================
  108. # 5. 打印多变量预测结果
  109. # ============================================================
  110. print("\n未来 24 步预测:")
  111. for variable_index in range(out.forecast.shape[0]):
  112. print(
  113. f"\n变量 {variable_index + 1}:"
  114. )
  115. print(
  116. out.forecast[variable_index]
  117. )
  118. print("\n测试完成。")