【发布时间】:2020-08-09 15:25:18
【问题描述】:
我想确保我理解 LSTM,所以我使用 Pytorch 框架实现了一个虚拟示例。
作为输入,我使用长度为 10 的连续数字序列,并且要预测的值始终是序列的最后一个数字 + 1。例如:
x = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
y = 16
由于这是一项非常简单的预测任务,我预计该模型运行良好,但我观察到性能非常差。模型按批次预测一个常数值,在训练过程中不断增加。
我想知道我错过了什么。以下是我制作的代码 - 任何帮助将不胜感激。
from torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler, SequentialSampler
import torch.nn as nn
import torch
class MyDataset(Dataset):
def __init__(self):
pass
def __getitem__(self, index):
x = torch.tensor([index-9,index-8,index-7,index-6,index-5,index-4,index-3,index-2,index-1,index])
y = torch.tensor(index + 1)
return x,y
def __len__(self):
return 1000
class LSTM(nn.Module):
def __init__(self, hidden_layer_size=1, batch_size = 1):
super().__init__()
self.hidden_layer_size = hidden_layer_size
self.batch_size = batch_size
self.lstm = nn.LSTM(1, hidden_layer_size)
self.linear = nn.Linear(10, 1)
self.hidden_cell = (torch.zeros(1,self.batch_size,self.hidden_layer_size),
torch.zeros(1,self.batch_size,self.hidden_layer_size))
def forward(self, input_seq):
lstm_out, self.hidden_cell = self.lstm(input_seq.view(10 ,self.batch_size, -1), self.hidden_cell)
predictions = self.linear(lstm_out.squeeze().T)
return predictions
batch_size = 32
epochs = 1000
train = MyDataset()
sampler = RandomSampler(train)
train_dataloader = DataLoader(train, sampler=sampler, batch_size= batch_size , drop_last = True)
model = LSTM(batch_size = batch_size)
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for e in range(epochs):
for step, batch in enumerate(train_dataloader) :
seq, labels = batch
optimizer.zero_grad()
model.hidden_cell = (torch.zeros(1, batch_size, model.hidden_layer_size),
torch.zeros(1, batch_size, model.hidden_layer_size))
y_pred = model(seq.float())
print(y_pred)
single_loss = loss_function(y_pred, labels.float())
single_loss.backward()
optimizer.step()
【问题讨论】:
标签: python pytorch lstm recurrent-neural-network