title: _SymbolicException when using intermediate model outputs in tensorflow keras
body: <p>I am using tensorflow to train a VAE on MNIST datasets. Training basic AE using similar code worked, and compiling the model was also done successfully. But when I tried to fit this model, I got an error message as followed. I guess the problem is because self.log_var is a symbolic tensor, but other examples(<a href="https://keras.io/examples/variational_autoencoder/" rel="nofollow noreferrer">https://keras.io/examples/variational_autoencoder/</a>) also implemented VAE in a similar way without a problem. </p>
  
  <p><code>_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [&lt;tf.Tensor 'dense_1/Identity:0' shape=(None, 2) dtype=float32&gt;, &lt;tf.Tensor 'dense/Identity:0' shape=(None, 2) dtype=float32&gt;]
  </code></p>
  
  <pre><code>
  class VAE():
    def __init__(self,input_dim, dec_input_dim, enc_channels, enc_kernel_size, enc_strides, dec_channels, dec_kernel_size, dec_strides, z):
      n_enc_layers=len(enc_channels)
      n_dec_layers=len(dec_channels)
  
      model_input=Input(shape=input_dim)
      e=model_input
      for x in range(n_enc_layers):
        e=Conv2D(enc_channels[x], enc_kernel_size[x], strides=enc_strides[x], padding='same')(e)
        e=BatchNormalization()(e)
        e=LeakyReLU()(e)
  
      e=Flatten()(e)
      self.mu=Dense(z)(e)
      self.log_var=Dense(z)(e)
  
      def reparameterize(args):
        mu,log_var=args
        epsilon=tf.random.normal(tf.shape(mu))
        return mu+epsilon*tf.exp(log_var/2)
  
      encoder_output=Lambda(reparameterize)([self.mu,self.log_var])
  
      self.encoder=tf.keras.models.Model(model_input,encoder_output)
  
      decoder_input=Input(shape=(z))
      d=decoder_input
      d=Dense(np.prod(dec_input_dim))(d)
      d=Reshape(dec_input_dim)(d)
      for x in range(n_dec_layers):
        d=Conv2DTranspose(dec_channels[x], dec_kernel_size[x], strides=dec_strides[x], padding='same')(d)
        if x==n_dec_layers-1:
          d=Activation(tf.nn.tanh)(d)
        else:
          d=LeakyReLU()(d)
      dec_output=d
  
      self.encoder=tf.keras.models.Model(model_input,encoder_output)
      self.decoder=tf.keras.models.Model(decoder_input, dec_output)
      self.model=tf.keras.models.Model(model_input,self.decoder(encoder_output))
  
    def compile(self,lr,r_loss_factor):
      def r_loss(y_true,y_pred):
        return tf.reduce_mean(tf.square(y_true-y_pred))
      def kl_loss(y_true,y_pred):
        return -0.5*tf.reduce_sum(1+self.log_var-self.mu**2-tf.exp(self.log_var),axis=1)
      def vae_loss(y_true,y_pred):
        return r_loss(y_true,y_pred)+kl_loss(y_true,y_pred)
  
      optimizer=tf.keras.optimizers.Adam(lr)
      self.model.compile(optimizer=optimizer,loss=vae_loss,metrics = [r_loss, kl_loss])
  </code></pre>