以下是一个基于Transformer模型进行时间序列预测的代码实例,使用PyTorch实现:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
class TimeSeriesDataset(Dataset):
def __init__(self, data, seq_len):
self.data = data
self.seq_len = seq_len
def __len__(self):
return len(self.data) - self.seq_len
def __getitem__(self, index):
return self.data[index:index+self.seq_len], self.data[index+self.seq_len]
class TransformerModel(nn.Module):
def __init__(self, input_dim, output_dim, d_model, nhead, num_layers):
super().__init__()
self.encoder = nn.TransformerEncoder(
nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead), num_layers=num_layers)
self.decoder = nn.Linear(d_model, output_dim)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x[:, -1, :])
return x
def train(model, train_loader, epochs, lr):
optimizer = optim.Adam(model.parameters(), lr=lr)
criterion = nn.MSELoss()
for epoch in range(epochs):
running_loss = 0.0
for data, target in train_loader:
optimizer.zero_grad()
output = model(data.unsqueeze(0))
loss = criterion(output, target.unsqueeze(0))
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch+1} loss: {running_loss/len(train_loader)}")
def predict(model, data, seq_len):
model.eval()
with torch.no_grad():
predictions = []
for i in range(len(data)-seq_len):
input_seq = data[i:i+seq_len].unsqueeze(0)
output_seq = model(input_seq)
predictions.append(output_seq.item())
return predictions
if __name__ == '__main__':
# Generate sample data
data = torch.sin(torch.arange(0, 10, 0.1))
train_data = TimeSeriesDataset(data[:80], seq_len=10)
train_loader = DataLoader(train_data, batch_size=1, shuffle=True)
# Define model hyperparameters
input_dim = 1
output_dim = 1
d_model = 64
nhead = 4
num_layers = 2
# Create and train model
model = TransformerModel(input_dim, output_dim, d_model, nhead, num_layers)
train(model, train_loader, epochs=50, lr=0.001)
# Make predictions
predictions = predict(model, data, seq_len=10)
```
这个代码实例演示了如何使用Transformer模型进行时间序列预测。在这个例子中,我们生成了一个正弦波形的时间序列数据,并将其分为10个数据点一组的样本。我们使用PyTorch的DataLoader和Dataset类来创建数据集和数据加载器,然后定义了一个Transformer模型,并使用均方误差损失函数和Adam优化器进行训练。最后,我们使用训练好的模型进行预测,并返回一个包含所有预测值的列表。