title: Obtaining output of an Intermediate layer in TensorFlow/Keras
body: <p>I'm trying to obtain output of an intermediate layer in Keras, Following is my code:</p>
  
  <pre><code>XX = model.input # Keras Sequential() model object
  YY = model.layers[0].output
  F = K.function([XX], [YY]) # K refers to keras.backend
  
  
  Xaug = X_train[:9]
  Xresult = F([Xaug.astype('float32')])
  </code></pre>
  
  <p>Running this, I got an Error : </p>
  
  <pre><code>InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dropout_1/keras_learning_phase' with dtype bool
  </code></pre>
  
  <p>i came to know that because I'm using dropout layer in my model, I have to specify a <code>learning_phase()</code> flag to my function as per keras <a href="https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer" rel="noreferrer">documentation</a>.
  I changed my code to the following:</p>
  
  <pre><code>XX = model.input
  YY = model.layers[0].output
  F = K.function([XX, K.learning_phase()], [YY])
  
  
  Xaug = X_train[:9]
  Xresult = F([Xaug.astype('float32'), 0])
  </code></pre>
  
  <p>Now I'm getting a new Error that I'm unable to figure out:</p>
  
  <pre><code>TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.
  </code></pre>
  
  <p>Any help would be appreciated.<br>
  PS : I'm new to TensorFlow and Keras.</p>
  
  <p><strong>Edit 1 :</strong> 
  Following is the complete code that I'm using. I'm using Spatial Transformer Network as discussed in this NIPS <a href="https://papers.nips.cc/paper/5854-spatial-transformer-networks.pdf" rel="noreferrer">paper</a> and it's Kera's implementation <a href="https://github.com/oarriaga/spatial_transformer_networks/tree/master/src" rel="noreferrer">here</a></p>
  
  <pre><code>input_shape =  X_train.shape[1:]
  
  # initial weights
  b = np.zeros((2, 3), dtype='float32')
  b[0, 0] = 1
  b[1, 1] = 1
  W = np.zeros((100, 6), dtype='float32')
  weights = [W, b.flatten()]
  
  locnet = Sequential()
  locnet.add(Convolution2D(64, (3, 3), input_shape=input_shape, padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(Convolution2D(64, (3, 3), padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(MaxPooling2D(pool_size=(2, 2)))
  locnet.add(Convolution2D(128, (3, 3), padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(Convolution2D(128, (3, 3), padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(MaxPooling2D(pool_size=(2, 2)))
  locnet.add(Convolution2D(256, (3, 3), padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(Convolution2D(256, (3, 3), padding='same'))
  locnet.add(Activation('relu'))
  locnet.add(MaxPooling2D(pool_size=(2, 2)))
  locnet.add(Dropout(0.5))
  locnet.add(Flatten())
  locnet.add(Dense(100))
  locnet.add(Activation('relu'))
  locnet.add(Dense(6, weights=weights))
  
  
  model = Sequential()
  
  model.add(SpatialTransformer(localization_net=locnet,
                               output_size=(128, 128), input_shape=input_shape))
  
  model.add(Convolution2D(64, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Convolution2D(64, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Convolution2D(128, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Convolution2D(128, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Convolution2D(256, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Convolution2D(256, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Convolution2D(256, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Convolution2D(256, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.5))
  model.add(Flatten())
  model.add(Dense(256))
  model.add(Activation('relu'))
  
  model.add(Dense(num_classes))
  model.add(Activation('softmax'))
  
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  
  #==============================================================================
  # Start Training
  #==============================================================================
  #define training results logger callback
  csv_logger = keras.callbacks.CSVLogger(training_logs_path+'.csv')
  model.fit(X_train, y_train,
            batch_size=batch_size,
            epochs=20,
            validation_data=(X_valid, y_valid),
            shuffle=True,
            callbacks=[SaveModelCallback(), csv_logger])
  
  
  
  
  #==============================================================================
  # Visualize what Transformer layer has learned
  #==============================================================================
  
  XX = model.input
  YY = model.layers[0].output
  F = K.function([XX, K.learning_phase()], [YY])
  
  
  Xaug = X_train[:9]
  Xresult = F([Xaug.astype('float32'), 0])
  
  # input
  for i in range(9):
      plt.subplot(3, 3, i+1)
      plt.imshow(np.squeeze(Xaug[i]))
      plt.axis('off')
  
  for i in range(9):
      plt.subplot(3, 3, i + 1)
      plt.imshow(np.squeeze(Xresult[0][i]))
      plt.axis('off')
  </code></pre>