Troubleshooting my LSTM

username: learningpytorch
cooked: <p>Please, I am trying to train a LSTM but I don’t know where it is failing. I have the error:</p>
<p>ModuleAttributeError: ‘LSTM’ object has no attribute ‘hidden_size’.</p>
<p>Full code in Colab: <a href="https://colab.research.google.com/drive/1nHODrwukJZYMMnH-fkMTLlvA9o1ihv-r?usp=sharing" rel="nofollow noopener">https://colab.research.google.com/drive/1nHODrwukJZYMMnH-fkMTLlvA9o1ihv-r?usp=sharing</a></p>
<p>``<br>
class LSTM(nn.Module):<br>
def <strong>init</strong>(self, input_size=1, hidden_size=100):<br>
super().<strong>init</strong>()<br>
self.hidden= None</p>
<pre><code>    self.lstm1 = nn.LSTM(
        input_size=input_size,
        hidden_size=hidden_size,
        num_layers=1,
        batch_first=True,
        bidirectional=False,
    )
    self.lstm2 = nn.LSTM(
        hidden_size,
        hidden_size * 2,
        num_layers=1,
        batch_first=True,
        bidirectional=False,
    )
    self.fc = nn.Linear(hidden_size * 2, 1)

def forward(self, x):
    x, _ = self.lstm1(x)
    x, _ = self.lstm2(x)

    x = self.fc(x)

    return x
</code></pre>
<p>´´</p>

username: Usama_Hasan
cooked: <p>Hy, the error is generated at this line.</p>
<pre><code class="lang-auto">model.hidden = (torch.zeros(1,1,model.hidden_size).cuda(),
                       torch.zeros(1,1,model.hidden_size).cuda())
</code></pre>
<p>Right then you should modify your LSTM class like this.</p>
<pre><code class="lang-auto">class LSTM(nn.Module):
   def init(self, input_size=1, hidden_size=100):
        super(LSTM,self).__init__()
        self.hidden_size = hidden_size
        self.hidden= None
</code></pre>
<p>Should do</p>

username: learningpytorch
cooked: <p>Thanks a million <a class="mention" href="/u/usama_hasan">@Usama_Hasan</a> for your help. After changing it, the performance is very poor as you can see in this link:<a href="https://colab.research.google.com/drive/1Q7q0dGTylMYMji1E3M104r0149xc9dbh?usp=sharing" rel="nofollow noopener">https://colab.research.google.com/drive/1Q7q0dGTylMYMji1E3M104r0149xc9dbh?usp=sharing</a></p>
<p>I don’t have clear yet the difference between hidden and hidden_size attributes of the model. I am new in this topic, sorry.</p>
<p>Could you recommend me any good tutorial available on the web for this or to improve my knowledge on LSTMs for Time-Series?</p>
<p>Thanks.</p>

username: Usama_Hasan
cooked: <p>Sure,<br>
You can check my earlier post on this.</p><aside class="quote quote-modified" data-post="2" data-topic="93779">
  <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/usama_hasan/48/29254_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/can-lstm-run-multivariate-time-series/93779/2">Can LSTM run multivariate time series?</a> <a class="badge-category__wrapper " href="/c/windows/26"><span data-category-id="26" data-drop-close="true" class="badge-category " title="This category is focused on PyTorch on Windows related issues."><span class="badge-category__name">windows</span></span></a>
  </div>
  <blockquote>
    Yes you can use LSTM for time series data prediction. You can find alot of resources for that purpose. 
You can check this github repo for research papers and link for data resources. 
<a href="https://github.com/Alro10/deep-learning-time-series" rel="noopener nofollow ugc">https://github.com/Alro10/deep-learning-time-series</a> 
If you want to checkout for implementation you can also find that in below link. 
<a href="https://github.com/zhangxu0307/time_series_forecasting_pytorch" rel="noopener nofollow ugc">https://github.com/zhangxu0307/time_series_forecasting_pytorch</a> 
<a href="https://github.com/KurochkinAlexey/ConvRNN" rel="noopener nofollow ugc">https://github.com/KurochkinAlexey/ConvRNN</a> 
<a href="https://stackoverflow.com/questions/56858924/multivariate-input-lstm-in-pytorch" rel="noopener nofollow ugc">https://stackoverflow.com/questions/56858924/multivariate-input-lstm-in…</a>
  </blockquote>
</aside>


username: Usama_Hasan
cooked: <p>First check these on PyTorch website.<br>
<a href="https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html</a></p>
<p><a href="https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html</a></p>

username: learningpytorch
cooked: <p>Thanks <a class="mention" href="/u/usama_hasan">@Usama_Hasan</a>. The multivariate approach is just what I need. Great info. I will have a look on it today and will get back to you if I have more questions. Thanks again for your support.</p>

username: learningpytorch
cooked: <p>Dear <a class="mention" href="/u/usama_hasan">@Usama_Hasan</a>, thanks for the information that you provided to me. It is great. However, I still have some questions.</p>
<p>1.- I still do not have clear the differences between hidden and hidden_size in the model.<br>
Complete code in: <a href="https://colab.research.google.com/drive/1Q7q0dGTylMYMji1E3M104r0149xc9dbh?usp=sharing" class="inline-onebox" rel="noopener nofollow ugc">Google Colab</a></p>
<pre><code class="lang-auto">class LSTM(nn.Module):
    def __init__(self, input_size=1, hidden_size=100):
        super().__init__()
        self.hidden= None
        self.hidden_size=hidden_size

        self.lstm1 = nn.LSTM(
            input_size=input_size,
            hidden_size=hidden_size,
            num_layers=1,
            batch_first=True,
            bidirectional=False,
        )
        self.lstm2 = nn.LSTM(
            hidden_size,
            hidden_size * 2,
            num_layers=1,
            batch_first=True,
            bidirectional=False,
        )
        self.fc = nn.Linear(hidden_size * 2, 1)

    def forward(self, x):
        x, _ = self.lstm1(x)
        x, _ = self.lstm2(x)

        x = self.fc(x)

        return x
</code></pre>
<p>2.- Why is the c_n part of the model not used in the second LSTM? Like here<br>
<a href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/" class="onebox" target="_blank" rel="noopener nofollow ugc">https://colah.github.io/posts/2015-08-Understanding-LSTMs/</a></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/9/1976c59fca638e2395778c3034e6f4d5207461af.png" data-download-href="https://discuss.pytorch.org/uploads/default/1976c59fca638e2395778c3034e6f4d5207461af" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/9/1976c59fca638e2395778c3034e6f4d5207461af_2_690x292.png" alt="image" data-base62-sha1="3Dgp51lL1k9Ow4uaDJILaEn9F7N" width="690" height="292" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/9/1976c59fca638e2395778c3034e6f4d5207461af_2_690x292.png, https://discuss.pytorch.org/uploads/default/original/3X/1/9/1976c59fca638e2395778c3034e6f4d5207461af.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/9/1976c59fca638e2395778c3034e6f4d5207461af.png 2x" data-dominant-color="E8F0E2"><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">image</span><span class="informations">739×313 43 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>Thanks in advance.</p>