title: "Layer is not connected, no input to return" error while trying to get intermediate layer prediction using tensorflow custom callback
body: <p>I'm trying to access predictions of intermediate layers of a model during training using custom callback. Following stripped down version of the actual code demonstrates the issue.</p>
  <pre><code>import tensorflow as tf
  import numpy as np
  
  class Model(tf.keras.Model):
      def __init__(self, input_shape=None, name=&quot;cus_model&quot;, **kwargs):
          super(Model, self).__init__(name=name, **kwargs)
          
      def build(self, input_shape):
          self.dense1 = tf.keras.layers.Dense(input_shape=input_shape, units=32)
          
      def call(self, input_tensor):
          return self.dense1(input_tensor)
  
  class CustomCallback(tf.keras.callbacks.Callback):
      def on_epoch_end(self, epoch, logs=None):
          get_output = tf.keras.backend.function(
              inputs = self.model.layers[0].input,
              outputs = self.model.layers[0].output
          )
          print(&quot;Layer output: &quot;,get_output.outputs)
  
  X = np.ones((8,16))
  y = np.sum(X, axis=1)
  
  model = Model()
  model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
  model.fit(X,y, epochs=8, callbacks=[CustomCallback()])
  </code></pre>
  <p>The callback is written as suggested in this <a href="https://stackoverflow.com/a/58848732/2679778">answer</a>. Getting following error:</p>
  <pre><code>&lt;ipython-input-3-635fd53dbffc&gt; in on_epoch_end(self, epoch, logs)
       12     def on_epoch_end(self, epoch, logs=None):
       13         get_output = tf.keras.backend.function(
  ---&gt; 14             inputs = self.model.layers[0].input,
       15             outputs = self.model.layers[0].output
       16         )
  .
  .
  AttributeError: Layer dense is not connected, no input to return.
  </code></pre>
  <p>What's causing this? How to resolve it?</p>