Problem multi layers of LSTM output error

chan_man: <p>I am a beginner of the PyTorch, now I am writing a code for the time series forecasting by LSTM. The LSTM includes two layers and stacks together. The first layer of LSTM includes 10 LSTM units and the hidden units will pass to another layer of LSTM which includes a single LSTM unit. The following is the architecture diagram of the neural network.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6bbbeb43b658cb5b21cd956f2e1c11a416c98b5.png" alt="f1" data-base62-sha1="uDCq3P8ojyqdA5gMQ9bToj54mRT" width="433" height="294"></p>
<p>following is my code,</p>
<pre><code>def __init__(self, nb_features=1, hidden_size_1=100, hidden_size_2=100, nb_layers_1 =5, nb_layers_2 = 1, dropout=0.4): #(self, nb_features=1, hidden_size=100, nb_layers=10, dropout=0.5):      ####### nb_layers=5
    super(Sequence, self).__init__()
    self.nb_features = nb_features
    self.hidden_size_1 = hidden_size_1
    self.hidden_size_2 = hidden_size_2
    self.nb_layers_1 =nb_layers_1
    self.nb_layers_2 = nb_layers_2
    self.lstm_1 = nn.LSTM(self.nb_features, self.hidden_size_1, self.nb_layers_1, dropout=dropout)  #, dropout=dropout
    self.lstm_2 = nn.LSTM(self.hidden_size_1, self.hidden_size_2, self.nb_layers_2, dropout=dropout)
    self.lin = nn.Linear(self.hidden_size_2, 1)

def forward(self, input):
    h0 = Variable(torch.zeros(self.nb_layers_1, input.size()[1], self.hidden_size_1))
    h1 = Variable(torch.zeros(self.nb_layers_2, input.size()[1], self.hidden_size_2))
    #print(type(h0))
    c0 = Variable(torch.zeros(self.nb_layers_1, input.size()[1], self.hidden_size_1))
    c1 = Variable(torch.zeros(self.nb_layers_2, input.size()[1], self.hidden_size_2))
    #print(type(c0))

    output_0, hn_0 = self.lstm_1(input, (h0, c0))
    output, hn = self.lstm_2(output_0, (h1, c1))
    out = torch.tanh(self.lin(output[-1])) ##########out = self.lin(output[-1])
    #out = self.lin(output_2[-1])
    return out
</code></pre>
<p>The code can be run, however, the output is a straight line even tuning the hyperparameter (learning rate, dropout, activation method) and increases epoch (i.e. 3000 epochs), the output result was shown in the following.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/0/703df11bc6bd47598f4b9a3e8392a9e8bcc04620.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/703df11bc6bd47598f4b9a3e8392a9e8bcc04620" title="f2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/0/703df11bc6bd47598f4b9a3e8392a9e8bcc04620_2_690x498.jpeg" alt="f2" data-base62-sha1="g0WbyI3Y3uWHiYLhZu4iA0t6a1q" width="690" height="498" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/0/703df11bc6bd47598f4b9a3e8392a9e8bcc04620_2_690x498.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/7/0/703df11bc6bd47598f4b9a3e8392a9e8bcc04620_2_1035x747.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/0/703df11bc6bd47598f4b9a3e8392a9e8bcc04620.jpeg 2x" data-dominant-color="F9E7D6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">f2</span><span class="informations">1072×774 130 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Could you please give me some suggestions to solve this problem. many thanks</p>

Fajri_Koto: <p>Hi,</p>
<p>I think the problem is because the forward function keeps calling the initialization of h0, h1, c0, and c1. So, in this case, you always use vector zeros as the hidden state. Try to remove it from the forward function.</p>
<p>Also, I don’t think that initialization is necessary. As mentioned in the documentation:</p>
<blockquote>
<p>If (h_0, c_0) is not provided, both  <strong>h_0</strong>  and  <strong>c_0</strong>  default to zero.</p>
</blockquote>

chan_man: <p>Hi,</p>
<p>Many thanks for your reply.</p>
<p>I have tried your suggestion that changed the code from:<br>
h0 = Variable(torch.zeros(self.nb_layers_1, input.size()[1], self.hidden_size_1))</p>
<p>to</p>
<p>h0 = torch.ones(self.nb_layers_1, input.size()[1], self.hidden_size_1)</p>
<p>for both h0, h1, c0 and c1, however, the result was also shown a straight line.</p>
<p>Additional information, if I just use one layer of LSTM with 5 LSTM units, using the same code(by mean that initialization both the hidden state and cell state as zeros), it can be successfully predicted, but add one more single unit LSTM layer it doesn’t.</p>

Fajri_Koto: <p>May be for the second LSTM, you need to forward the hidden state of your first LSTM, instead of initializing a new hidden state.</p>

hpf: <p>Hi,I have a problem similar to yours. Have you found a way to solve the problem</p>