LSTM, 'method' object is not subscriptable

Kieran: <p>Hello, I’m implementing an LSTM to predict today’s stock price using the past 10 days’ close price.<br>
Therefore, my input is [batch_size, sequence_len = 10, input_size = 1] since there is only one feature every day.<br>
Then I set batch_size = 50 and implement a train_loader with TensorDataset and DataLoader.<br>
My LSTM model is defined as</p>
<pre><code class="lang-auto">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
</code></pre>
<p>And when I train the model,</p>
<pre><code class="lang-auto">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()

</code></pre>
<p>I got this error <code>in y_pred = model(local_batch)</code>  at /usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in <code>check_forward_args(self, input, hidden, batch_sizes)</code>:</p>
<pre><code class="lang-auto">-&gt; 522         self.check_hidden_size(hidden[0], expected_hidden_size,
    523                                'Expected hidden[0] size {}, got {}')
</code></pre>
<p>TypeError: ‘method’ object is not subscriptable</p>
<p>I’m new to LSTM, any hints are appreciable!</p>

tom: <aside class="quote no-group" data-username="Kieran" data-post="1" data-topic="88433">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kieran/48/22786_2.png" class="avatar"> Kieran:</div>
<blockquote>
<p><code>out, self.hidden = self.lstm(x, self.init_hidden)</code></p>
</blockquote>
</aside>
<p>this passes the <code>init_hidden</code> method, you likely want <code>init_hidden()</code> instead. (Why you would need to do this when 0 initial hidden it is implicit in PyTorch, I don’t know.)</p>
<p>Best regards</p>
<p>Thomas</p>