用chatgpt写的LSTM代码,自己做了部分修改(它的代码运行不起来),但是迭代一定次数以后,损失就不变了,这是怎么回事任务是1到63数字的时间序列预测,下面为部分输出 ![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/266687439486152.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)
import torch
from torch import nn
# 创建时间序列数据
data = torch.arange(1, 64, dtype=torch.float32)
def create_sequence(data, seq_length=5):
"""创建输入序列和输出序列"""
xs, ys = [], []
for i in range(len(data) - seq_length):
x = data[i:i + seq_length].view(-1, 1)
y = data[i + seq_length].view(-1, 1)
xs.append(x)
ys.append(y)
return torch.stack(xs), torch.stack(ys)
# 创建输入序列和输出序列
xs, ys = create_sequence(data)
# 将数据集分为训练集和测试集
train_size = int(len(xs) * 0.8)
train_xs, train_ys = xs[:train_size], ys[:train_size]
#print(train_xs, train_ys)
test_xs, test_ys = xs[train_size:], ys[train_size:]
# 将数据放到 GPU 上(如果有的话)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
train_xs, train_ys = train_xs.to(device), train_ys.to(device)
test_xs, test_ys = test_xs.to(device), test_ys.to(device)
# 定义 RNN 模型
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.lstm = nn.LSTM(input_size, hidden_size)
self.relu1 = nn.ReLU()
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, h_state = self.lstm(x)
out = out[:, -1, :].squeeze(1)
out = self.relu1(out)
out = self.fc(out)
return out
# 设置超参数
input_size = 1
hidden_size = 10
output_size = 1
learning_rate = 0.001
num_epochs = 50000
# 实例化模型和损失函数
model = RNN(input_size, hidden_size, output_size).to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 开始训练模型
best_loss = float('inf')
for epoch in range(num_epochs):
# 将数据输入到模型中进行预测
pred_train = model(train_xs)
# 计算损失函数
loss = criterion(pred_train.squeeze(), train_ys)
# 清空梯度,反向传播,更新参数
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 计算测试集上的损失函数,并更新 best_loss
with torch.no_grad():
pred_test = model(test_xs)
test_loss = criterion(pred_test.squeeze(), test_ys)
if test_loss < best_loss:
best_loss = test_loss
best_model = model
# 每隔100轮输出一次损失函数
if (epoch + 1) % 100 == 0:
print('Epoch [{}/{}], Train Loss: {:.4f}, Test Loss: {:.4f}'.format(
epoch + 1, num_epochs, loss.item(), test_loss.item()))
# Early Stopping
if epoch > 100 and test_loss > best_loss:
print('Early stopping at epoch {}'.format(epoch+1))
break
# 测试模型的效果
with torch.no_grad():
# 将数据输入到模型中进行预测
pred_test = best_model(test_xs)
# 计算测试集上的平均损失函数
test_loss = criterion(pred_test.squeeze(), test_ys)
print('Test Loss: {:.4f}'.format(test_loss.item()))
# 取出最后4个时刻的数据进行预测
last_inputs = data[-4:].view(-1, 1).to(device)
for i in range(5):
# 进行预测
pred = best_model(last_inputs.unsqueeze(0))
print("pred", pred)
# 计算预测值和真实值之间的差异
diff = pred.item() - data[-1]
# 将预测结果加入输入序列中
last_inputs = torch.cat([last_inputs[1:], pred.view(1, -1)], dim=0)
# 输出预测结果
print('Prediction {}: {:.2f} diff:({:.2f})'.format(i + 1, pred.item(), diff))
|