title: Keras LSTM from for loop, using functional API with custom number of layers
body: <p>I am trying to build a network through the keras functional API feeding two lists containing the number of units of the LSTM layers and of the FC (Dense) layers. I want to analyse 20 consecutive segments (batches) which contain fs time steps each and 2 values (2 features per time step). This is my code:</p>
  
  <pre><code>Rec = [4,4,4]  
  FC = [8,4,2,1]    
  def keras_LSTM(Rec,FC,fs, n_witness, lr=0.04, optimizer='Adam'):
      model_LSTM = Input(batch_shape=(20,fs,n_witness))
      return_state_bool=True
      for i in range(shape(Rec)[0]):
          nRec = Rec[i]
          if i == shape(Rec)[0]-1:
              return_state_bool=False
          model_LSTM = LSTM(nRec, return_sequences=True,return_state=return_state_bool,
                       stateful=True, input_shape=(None,n_witness),            
                       name='LSTM'+str(i))(model_LSTM)
      for j in range(shape(FC)[0]):
          nFC = FC[j]
          model_LSTM = Dense(nFC)(model_LSTM)
          model_LSTM = LeakyReLU(alpha=0.01)(model_LSTM)
      nFC_final = 1
      model_LSTM = Dense(nFC_final)(model_LSTM)
      predictions = LeakyReLU(alpha=0.01)(model_LSTM)
  
      full_model_LSTM = Model(inputs=model_LSTM, outputs=predictions)
      model_LSTM.compile(optimizer=keras.optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999,
                      epsilon=1e-8, decay=0.066667, amsgrad=False), loss='mean_squared_error')
      return full_model_LSTM
  
  model_new = keras_LSTM(Rec, FC, fs=fs, n_witness=n_wit)
  model_new.summary()
  </code></pre>
  
  <p>When compiling I get the following error:</p>
  
  <p><strong>ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(20, 2048, 2), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []</strong></p>
  
  <p>Which I actually don't quite understand, but suspect it may have something to do with inputs?</p>