title: "Layer is not connected" issue while accessing intermediate layer from within the custom callback if model is built by sub-classing
body: <p>I've a simple model and need access of intermediate layers within a custom callback to get intermediate predictions.</p>
  <pre><code>import tensorflow as tf
  import numpy as np
  
  X = np.ones((8,16))
  y = np.sum(X, axis=1)
  
  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[1].output
          )
          print(&quot;\nLayer output: &quot;, get_output(X))
  </code></pre>
  <p>If I build the model by sub-classing like below:</p>
  <pre><code>class Model(tf.keras.Model):
      def build(self, input_shape):
          self.dense1 = tf.keras.layers.Dense(units=32)
          self.dense2 = tf.keras.layers.Dense(units=1)
          
      def call(self, input_tensor):
          x = self.dense1(input_tensor)
          x = self.dense2(x)
          return x
  
  model = Model()
  model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
  model.fit(X,y, epochs=2, callbacks=[CustomCallback()])
  </code></pre>
  <p>I get following error:</p>
  <pre><code>&lt;ipython-input-8-bab75191182e&gt; in on_epoch_end(self, epoch, logs)
        2     def on_epoch_end(self, epoch, logs=None):
        3         get_output = tf.keras.backend.function(
  ----&gt; 4             inputs = self.model.layers[0].input,
        5             outputs = self.model.layers[1].output
        6         )
  .
  .
  AttributeError: Layer dense is not connected, no input to return.
  </code></pre>
  <p>whereas if I build model using functional API like below, it works as expected.</p>
  <pre><code>initial = tf.keras.layers.Input((16,))
  x = tf.keras.layers.Dense(units=32)(initial)
  final = tf.keras.layers.Dense(units=1)(x)
  
  model = tf.keras.Model(initial, final)
  model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
  model.fit(X,y, epochs=2, callbacks=[CustomCallback()])
  </code></pre>
  <p>What's causing the error in case of model sub-classing?</p>
  <p>TF version: 2.2.0</p>