How to get all the memory cell vertor of LSTM

qwe: <p>out,(h_n,c_n)=LSTM(input,(h_0,c_0))</p>
<p>c_n is a tensor containing the hidden state for last time step n. I would like getting the memory cell c_n tensor of all time steps .what should I do?  LSTMcell?</p>

qwe: <p>can a nn.LSTM() be consisted of nn.LSTMcell()?</p>

vdw: <p>No, not directly. <code>LSTM</code> wraps a lot of functionality around <code>LSTMcell</code>, such as multiple layers and bidirectionality. <code>LSTM</code> also allows you to give it a while sequence.</p>
<p>What you could do is to get all memory cell <code>c_i</code> for each time step given a sequence <code>seq</code> is to use <code>LSTM</code> (or <code>LSTMcell</code>, I suppose) and process your sequence in a loop step by step, e.g.:</p>
<pre><code>c_cells = []
for i in range(len(seq)):
    input = seq[i]
    out, hidden = lstm(input, hidden)
    c_cells.append(hidden[1])
</code></pre>
<p>Please not that this is just crude pseudo code.</p>

qwe: <p>thanks for your reply . I tried your practice. Certainly, I can get all the memory cell vector by using a loop, but the results are different between using nn.LSTM directly and using a loop of LSTM . I am confused.</p>

qwe: <p>the results of hidden states h_n are different  between using nn.LSTM directly and using a loop of LSTM</p>

vdw: <p>I assume that you wanted to write that the results are “different” <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Looping over an RNN is quite common, e.g., in sequence-to-sequence models (see, e.g., the <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html" rel="nofollow noopener">Seq2Seq PyTorch tutorial</a>). Check the code of the decoder(s). Not that the <code>forward</code> method of the decoder does not contain a loop but only takes one word (i.e., one time step) at a time. The loop is outside in the <code>train</code> method.</p>
<p>Both approaches are fine, you only need to take care that you accumulate the loss properly; best check the tutorial link above. I also think you cannot use <code>bidirectional=True</code> when creating your <code>nn.LSTM</code>. Other than that, there shouldn’t really be a (major) difference to process a sequence “manually” using a loop or let <code>nn.LSTM</code> do everything under the hood.</p>

qwe: <p>I will show my code :<br>
####################<span class="hashtag">#------------1------------</span>#####################<br>
init():<br>
self.LSTM_audio = nn.LSTM(Audio_dim, Audio_dim, 1, batch_first=True, bidirectional=False)<br>
h0c0_audio = (Variable(torch.zeros(1, self.batch_size, self.Audio_dim).cuda()),<br>
Variable(torch.zeros(1, self.batch_size, self.Audio_dim).cuda()))</p>
<p>forward():<br>
out_audio, (hn_a,cn_a) = self.LSTM_audio( audio, h0c0_audio )</p>
<p>####################<span class="hashtag">#------------2------------</span>#####################<br>
init():<br>
LSTM = nn.LSTM( Mat_size, hidden_size, 1, batch_first=True, bidirectional=False )<br>
states = (Variable(torch.zeros(1, self.batch_size, self.hidden_size).cuda()),<br>
Variable(torch.zeros(1, self.batch_size, self.hidden_size).cuda()))<br>
hiddens = Variable(torch.zeros(self.batch_size, self.seq_len, self.hidden_size).cuda())</p>
<p>forward():<br>
for time_step in range( self.seq_len ):<br>
data_seq = x_t[ :, time_step, : ]<br>
data_seq = data_seq.unsqueeze( 1 )</p>
<p>out, self.states = self.LSTM( data_seq, self.states )<br>
hiddens[ :, time_step, : ] = out</p>
<p>Question: I find the value of  "out_audio in 1 " and "hiddens of 2 " are different .I am very confused</p>