title: Keras - Pop and re-add layers, but layer does not disconnect
body: <p>Using Keras (1.2.2), I am loading a sequential model whose last layers are:</p>
  
  <pre><code>model.add(Dense(512))
  model.add(Activation('relu'))
  model.add(Dense(nb_classes))
  model.add(Activation('softmax'))
  </code></pre>
  
  <p>Then, I want to pop the last layer, add another fully connected layer, and re-add the classification layer.</p>
  
  <pre><code>model = load_model('model1.h5')                                                                         
  layer1 = model.layers.pop() # Copy activation_6 layer                                      
  layer2 = model.layers.pop() # Copy classification layer (dense_2)                          
  
  model.add(Dense(512, name='dense_3'))
  model.add(Activation('softmax', name='activation_7'))
  
  model.add(layer2)
  model.add(layer1)
  
  print(model.summary())
  </code></pre>
  
  <p>As you can see my dense_3 and activation_7 did not connect to the network (Empty value in summary() with "Connected to"). I cannot find anything in the documentation that explains how to solve this problem. Any ideas?</p>
  
  <pre><code>dense_1 (Dense)                  (None, 512)           131584      flatten_1[0][0]                  
  ____________________________________________________________________________________________________
  activation_5 (Activation)        (None, 512)           0           dense_1[0][0]                    
  ____________________________________________________________________________________________________
  dense_3 (Dense)                  (None, 512)           5632                                         
  ____________________________________________________________________________________________________
  activation_7 (Activation)        (None, 512)           0                                            
  ____________________________________________________________________________________________________
  dense_2 (Dense)                  (None, 10)            5130        activation_5[0][0]               
  ____________________________________________________________________________________________________
  activation_6 (Activation)        (None, 10)            0           dense_2[0][0]                    
  ====================================================================================================
  </code></pre>
  
  <p>Following the answer below, I compiled the model before printing out <code>model.summary()</code>, but for some reasons, the layers are not being popped correctly, as the summary shows: The last layer's connections are wrong:</p>
  
  <pre><code>dense_1 (Dense)                  (None, 512)           131584      flatten_1[0][0]                  
  ____________________________________________________________________________________________________
  activation_5 (Activation)        (None, 512)           0           dense_1[0][0]                    
  ____________________________________________________________________________________________________
  dense_3 (Dense)                  (None, 512)           5632        activation_6[0][0]               
  ____________________________________________________________________________________________________
  activation_7 (Activation)        (None, 512)           0           dense_3[0][0]                    
  ____________________________________________________________________________________________________
  dense_2 (Dense)                  (None, 10)            5130        activation_5[0][0]               
                                                                     activation_7[0][0]               
  ____________________________________________________________________________________________________
  activation_6 (Activation)        (None, 10)            0           dense_2[0][0]                    
                                                                     dense_2[1][0]                    
  ====================================================================================================
  </code></pre>
  
  <p>But it should be</p>
  
  <pre><code>dense_1 (Dense)                  (None, 512)           131584      flatten_1[0][0]                  
  ____________________________________________________________________________________________________
  activation_5 (Activation)        (None, 512)           0           dense_1[0][0]                    
  ____________________________________________________________________________________________________
  dense_3 (Dense)                  (None, 512)           5632        activation_5[0][0]               
  ____________________________________________________________________________________________________
  activation_7 (Activation)        (None, 512)           0           dense_3[0][0]                    
  ____________________________________________________________________________________________________
  dense_2 (Dense)                  (None, 10)            5130                       
                                                                     activation_7[0][0]               
  ____________________________________________________________________________________________________
  activation_6 (Activation)        (None, 10)            0           dense_2[0][0]                    
  
  ====================================================================================================
  </code></pre>