请使用pytorch 编写LSTM时间序列预测代码包含激活函数ReLU的配置
以下是一个使用PyTorch编写LSTM时间序列预测代码的示例。其中包括一个使用ReLU激活函数的LSTM模型配置。
import torch
import torch.nn as nn
import numpy as np
# 定义LSTM模型
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, output_size, num_layers):
super(LSTM, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
self.relu = nn.ReLU()
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
out = self.relu(out)
return out
# 定义超参数
input_size = 1
hidden_size = 32
output_size = 1
num_layers = 2
learning_rate = 0.01
num_epochs = 100
# 加载数据
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=np.float32)
data = data.reshape(-1, 1)
# 将数据转换为张量
x_train = torch.tensor(data[:-1, :]).unsqueeze(0)
y_train = torch.tensor(data[1:, :]).unsqueeze(0)
# 将模型移动到GPU(如果可用)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = LSTM(input_size, hidden_size, output_size, num_layers).to(device)
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
# 前向传播
outputs = model(x_train.to(device))
loss = criterion(outputs, y_train.to(device))
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, loss.item()))
# 测试模型
with torch.no_grad():
future = 3
test_inputs = data[-1].copy()
test_inputs = torch.tensor(test_inputs).float().unsqueeze(0).unsqueeze(0).to(device)
test_outputs = []
for i in range(future):
out = model(test_inputs)
test_outputs.append(out.item())
test_inputs = torch.cat([test_inputs[:, :, 1:], out.unsqueeze(1).unsqueeze(1)], dim=2)
print('Test Outputs:', test_outputs)
在这个示例中,我们定义了一个LSTM模型,其中包括一个使用ReLU激活函数的线性层。我们使用MSE损失函数和Adam优化器来训练模型。在训练完成后,我们使用训练好的模型来预测未来的数据点
原文地址: https://www.cveoy.top/t/topic/hjQj 著作权归作者所有。请勿转载和采集!