当前位置:   article > 正文

Transformers 实现时间序列预测

Transformers 实现时间序列预测

使用Transformers实现时间序列预测通常涉及使用预训练的Transformer模型(如BERT、GPT等)来处理时间序列数据。下面是一个简单的示例,演示如何使用Transformers库中的模型来进行时间序列预测。

  1. import torch
  2. import torch.nn as nn
  3. from transformers import BertModel, BertConfig
  4. import numpy as np
  5. import pandas as pd
  6. from sklearn.preprocessing import StandardScaler
  7. from sklearn.model_selection import train_test_split
  8. # 创建一个简单的时间序列数据集
  9. # 这里假设时间序列是一个简单的sin函数
  10. np.random.seed(42)
  11. n_points = 1000
  12. X = np.linspace(0, 100, n_points)
  13. y = np.sin(X) + np.random.normal(0, 0.1, n_points)
  14. # 划分训练集和测试集
  15. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  16. # 数据标准化
  17. scaler = StandardScaler()
  18. X_train_scaled = scaler.fit_transform(X_train.reshape(-1, 1))
  19. X_test_scaled = scaler.transform(X_test.reshape(-1, 1))
  20. # 转换为PyTorch张量
  21. X_train_tensor = torch.tensor(X_train_scaled, dtype=torch.float32)
  22. X_test_tensor = torch.tensor(X_test_scaled, dtype=torch.float32)
  23. y_train_tensor = torch.tensor(y_train, dtype=torch.float32).unsqueeze(1) # 添加一个维度以适应模型输入
  24. y_test_tensor = torch.tensor(y_test, dtype=torch.float32).unsqueeze(1)
  25. # 定义一个简单的Transformer模型作为时间序列预测器
  26. class TransformerTimeSeriesPredictor(nn.Module):
  27. def __init__(self, input_dim, output_dim, num_layers=6, hidden_dim=64, n_heads=8):
  28. super(TransformerTimeSeriesPredictor, self).__init__()
  29. config = BertConfig(
  30. hidden_size=hidden_dim,
  31. num_hidden_layers=num_layers,
  32. num_attention_heads=n_heads,
  33. intermediate_size=hidden_dim * 4,
  34. hidden_dropout_prob=0.1,
  35. attention_probs_dropout_prob=0.1
  36. )
  37. self.encoder = BertModel(config)
  38. self.fc = nn.Linear(hidden_dim, output_dim)
  39. def forward(self, x):
  40. _, pooled_output = self.encoder(x)
  41. output = self.fc(pooled_output)
  42. return output
  43. # 初始化模型并定义优化器和损失函数
  44. model = TransformerTimeSeriesPredictor(input_dim=1, output_dim=1)
  45. optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
  46. criterion = nn.MSELoss()
  47. # 训练模型
  48. num_epochs = 100
  49. for epoch in range(num_epochs):
  50. model.train()
  51. optimizer.zero_grad()
  52. outputs = model(X_train_tensor)
  53. loss = criterion(outputs, y_train_tensor)
  54. loss.backward()
  55. optimizer.step()
  56. print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item()}')
  57. # 在测试集上进行预测
  58. model.eval()
  59. with torch.no_grad():
  60. predicted = model(X_test_tensor)
  61. test_loss = criterion(predicted, y_test_tensor)
  62. print(f'Test Loss: {test_loss.item()}')
  63. # 可视化结果
  64. import matplotlib.pyplot as plt
  65. plt.figure(figsize=(10, 5))
  66. plt.plot(X_test, y_test, label='True')
  67. plt.plot(X_test, predicted.numpy(), label='Predicted')
  68. plt.legend()
  69. plt.show()

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号