title: Adding intermediate layer to the loss function in deep learning keras
body: <p>I want to use custom loss function using intermediate layers for DNN model VAE in keras. I call the model function and then add the loss to the layers.  </p>
  
  <p>Error is: </p>
  
  <pre><code>ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval
  </code></pre>
  
  <p><code>model</code> gets compiled but causes the error during training</p>
  
  <pre><code>#Below is the code which causes error
  #this returns the models (en,de, model) and layers (z_mean and #z_log_sigma)
  en,de,model,z_mean,z_log_sigma = load_model(config)
  #defining loss using intermediate layers returned
  kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - 
                        K.exp(z_log_sigma), axis=-1)
  
  model.add_loss(kl_loss)
  model.compile( optimizer=optimizer)
  #error is raised during training 
  history = model.fit_generator(
                      genfun,
                      steps_per_epoch = display_interval,
                      epochs = 1,
                      shuffle=False,
                      verbose = 1
                  ) #callbacks=[eval_map])    
  </code></pre>