赞
踩
使用Transformers实现时间序列预测通常涉及使用预训练的Transformer模型(如BERT、GPT等)来处理时间序列数据。下面是一个简单的示例,演示如何使用Transformers库中的模型来进行时间序列预测。
- import torch
- import torch.nn as nn
- from transformers import BertModel, BertConfig
- import numpy as np
- import pandas as pd
- from sklearn.preprocessing import StandardScaler
- from sklearn.model_selection import train_test_split
-
- # 创建一个简单的时间序列数据集
- # 这里假设时间序列是一个简单的sin函数
- np.random.seed(42)
- n_points = 1000
- X = np.linspace(0, 100, n_points)
- y = np.sin(X) + np.random.normal(0, 0.1, n_points)
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- # 数据标准化
- scaler = StandardScaler()
- X_train_scaled = scaler.fit_transform(X_train.reshape(-1, 1))
- X_test_scaled = scaler.transform(X_test.reshape(-1, 1))
-
- # 转换为PyTorch张量
- X_train_tensor = torch.tensor(X_train_scaled, dtype=torch.float32)
- X_test_tensor = torch.tensor(X_test_scaled, dtype=torch.float32)
- y_train_tensor = torch.tensor(y_train, dtype=torch.float32).unsqueeze(1) # 添加一个维度以适应模型输入
- y_test_tensor = torch.tensor(y_test, dtype=torch.float32).unsqueeze(1)
-
- # 定义一个简单的Transformer模型作为时间序列预测器
- class TransformerTimeSeriesPredictor(nn.Module):
- def __init__(self, input_dim, output_dim, num_layers=6, hidden_dim=64, n_heads=8):
- super(TransformerTimeSeriesPredictor, self).__init__()
- config = BertConfig(
- hidden_size=hidden_dim,
- num_hidden_layers=num_layers,
- num_attention_heads=n_heads,
- intermediate_size=hidden_dim * 4,
- hidden_dropout_prob=0.1,
- attention_probs_dropout_prob=0.1
- )
- self.encoder = BertModel(config)
- self.fc = nn.Linear(hidden_dim, output_dim)
-
- def forward(self, x):
- _, pooled_output = self.encoder(x)
- output = self.fc(pooled_output)
- return output
-
- # 初始化模型并定义优化器和损失函数
- model = TransformerTimeSeriesPredictor(input_dim=1, output_dim=1)
- optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
- criterion = nn.MSELoss()
-
- # 训练模型
- num_epochs = 100
- for epoch in range(num_epochs):
- model.train()
- optimizer.zero_grad()
- outputs = model(X_train_tensor)
- loss = criterion(outputs, y_train_tensor)
- loss.backward()
- optimizer.step()
- print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item()}')
-
- # 在测试集上进行预测
- model.eval()
- with torch.no_grad():
- predicted = model(X_test_tensor)
- test_loss = criterion(predicted, y_test_tensor)
- print(f'Test Loss: {test_loss.item()}')
-
- # 可视化结果
- import matplotlib.pyplot as plt
-
- plt.figure(figsize=(10, 5))
- plt.plot(X_test, y_test, label='True')
- plt.plot(X_test, predicted.numpy(), label='Predicted')
- plt.legend()
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。