Hello, I’m implementing an LSTM to predict today’s stock price using the past 10 days’ close price.
Therefore, my input is [batch_size, sequence_len = 10, input_size = 1] since there is only one feature every day.
Then I set batch_size = 50 and implement a train_loader with TensorDataset and DataLoader.
My LSTM model is defined as
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, batch_size, output_dim, num_layers, dropout = 0.1):
super(LSTM, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.batch_size = batch_size
self.num_layers = num_layers
self.dropout = dropout
self.output_dim = output_dim
# Define the LSTM layer
self.lstm = nn.LSTM(input_size = input_dim, hidden_size = hidden_dim, num_layers = num_layers, dropout = dropout, batch_first = True)
# Define the output layer, fully connected
self.linear = nn.Linear(hidden_dim, output_dim)
def init_hidden(self):
# initialise our hidden state
return (torch.zeros(self.num_layers, self.batch_size, self.hidden_dim),
torch.zeros(self.num_layers, self.batch_size, self.hidden_dim))
def forward(self, x):
# Forward pass through LSTM layer
# shape of out: [input_size, batch_size, hidden_dim]
# shape of self.hidden: (a, b), where a and b both have shape (num_layers, batch_size, hidden_dim).
out, self.hidden = self.lstm(x, self.init_hidden)
y_pred = self.linear(out[:, -1, :])
return y_pred
And when I train the model,
INPUT_SIZE = 1 # number of features
HIDDEN_SIZE = 64
NUM_LAYERS = 3
OUTPUT_SIZE = 1
learning_rate = 0.001
num_epochs = 150
model = LSTM(INPUT_SIZE, HIDDEN_SIZE, batch_size, OUTPUT_SIZE, NUM_LAYERS)
loss_fn = torch.nn.MSELoss()
optimiser = torch.optim.Adam(model.parameters(), lr = learning_rate)
model.hidden = model.init_hidden()
model.train()
for local_batch, local_labels in train_loader:
# Transfer to GPU
local_batch = local_batch.float().to(device)
local_labels = local_labels.float().to(device)
print(local_batch.shape) # torch.Size([50, 10, 1])
print(local_labels.shape) # torch.Size([50, 1])
# Forward pass
y_pred = model(local_batch)
loss = loss_fn(y_pred, local_labels)
optimiser.zero_grad()
# Backward pass
loss.backward()
# Update parameters
optimiser.step()
I got this error in y_pred = model(local_batch)
at /usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in check_forward_args(self, input, hidden, batch_sizes)
:
-> 522 self.check_hidden_size(hidden[0], expected_hidden_size,
523 'Expected hidden[0] size {}, got {}')
TypeError: ‘method’ object is not subscriptable
I’m new to LSTM, any hints are appreciable!