title: LSTM Autoencoder
body: <p>I'm trying to build a LSTM autoencoder with the goal of getting a fixed sized vector from a sequence, which represents the sequence as good as possible. This autoencoder consists of two parts:</p>
  
  <ul>
  <li><code>LSTM</code> Encoder: Takes a sequence and returns an output vector (<code>return_sequences = False</code>)</li>
  <li><code>LSTM</code> Decoder: Takes an output vector and returns a sequence (<code>return_sequences = True</code>)</li>
  </ul>
  
  <p>So, in the end, the encoder is a <strong>many to one</strong> LSTM and the decoder is a <strong>one to many</strong> LSTM.</p>
  
  <p><a href="https://i.stack.imgur.com/kwhAP.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/kwhAP.jpg" alt="enter image description here"></a>
  Image source: <a href="http://karpathy.github.io/2015/05/21/rnn-effectiveness/" rel="noreferrer">Andrej Karpathy</a></p>
  
  <p>On a high level the coding looks like this (similar as described <a href="https://github.com/fchollet/keras/issues/5138" rel="noreferrer">here</a>):</p>
  
  <pre><code>encoder = Model(...)
  decoder = Model(...)
  
  autoencoder = Model(encoder.inputs, decoder(encoder(encoder.inputs)))
  
  autoencoder.compile(loss='binary_crossentropy',
                optimizer='adam',
                metrics=['accuracy'])
  
  autoencoder.fit(data, data,
            batch_size=100,
            epochs=1500)
  </code></pre>
  
  <p>The shape (number of training examples, sequence length, input dimension) of the <code>data</code> array is <code>(1200, 10, 5)</code> and looks like this:</p>
  
  <pre><code>array([[[1, 0, 0, 0, 0],
          [0, 1, 0, 0, 0],
          [0, 0, 1, 0, 0],
          ..., 
          [0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0]],
          ... ]
  </code></pre>
  
  <p><strong>Problem:</strong> I am not sure how to proceed, especially how to integrate <code>LSTM</code> to <code>Model</code> and how to get the decoder to generate a sequence from a vector.</p>
  
  <p>I am using <code>keras</code> with <code>tensorflow</code> backend.</p>
  
  <p><strong>EDIT:</strong> If someone wants to try out, here is my procedure to generate random sequences with moving ones (including padding):</p>
  
  <pre><code>import random
  import math
  
  def getNotSoRandomList(x):
      rlen = 8
      rlist = [0 for x in range(rlen)]
      if x &lt;= 7:
          rlist[x] = 1
      return rlist
  
  
  sequence = [[getNotSoRandomList(x) for x in range(round(random.uniform(0, 10)))] for y in range(5000)]
  
  ### Padding afterwards
  
  from keras.preprocessing import sequence as seq
  
  data = seq.pad_sequences(
      sequences = sequence,
      padding='post',
      maxlen=None,
      truncating='post',
      value=0.
  )
  </code></pre>