I'm trying to predict some stock data with LSTM in Pytorch.
The shape of my original dataset is (2577,6). I want to use 60 time steps to predict the next data, so I reshape my data to
(number of batches, time steps, number of features)
by doing:
look_back = 60
train_percent = 0.8
x_data = []
y_data = []
for i in range(data.shape[0] - look_back):
x_data.append(data[i : i + look_back, :])
y_data.append(data[i + look_back, 3])
where the fourth column is the response data. The shape after reshaping is (2013, 60, 6).
Here is my code
class model(nn.Module):
def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
super(model, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first = False)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
out, (hn, cn) = self.lstm(x)
out = self.fc(out[:, -1, :])
return out
lstm = model(input_dim = 6, hidden_dim = 32, output_dim = 1, num_layers = 5)
loss_fn = torch.nn.MSELoss()
optimiser = torch.optim.Adam(lstm.parameters(), lr=0.01)
num_epochs = 10000
train_loss = []
test_loss = []
for i in range(num_epochs):
lstm.train()
optimiser.zero_grad()
y_train_pred = lstm(x_train)
loss = loss_fn(y_train_pred, y_train)
loss.backward()
optimiser.step()
train_loss.append(loss.item())
with torch.no_grad():
lstm.eval()
y_test_pred = lstm(x_test)
loss = loss_fn(y_test_pred, y_test)
test_loss.append(loss.item())
if i % 100 == 0:
clear_output()
plt.plot(train_loss, label = 'training_loss')
plt.plot(test_loss, label = 'validation_loss')
plt.legend()
plt.show()
plt.plot(y_train.clone().cpu(), label = 'true value')
plt.plot(y_train_pred.clone().detach().cpu(), label = 'predicted value')
plt.legend()
plt.show()
After 400 iterations, the loss stabilizes, but when I look at the prediction on the training set, it looks a straight line at the mid of the plot. (I cannot post any image yet).