title: Keras custom loss function with samples from complete input dataset
body: <p>I am trying to devise a custom loss function for Variational auto-encoder in Keras with two parts: <code>reconstruction loss</code> and <code>divergence loss</code>. However, instead of using the gaussian distribution for divergence loss, I want to sample randomly from the input and then perform the divergence loss based on the sampled inputs. However, I do not know how to sample inputs which are from the complete datastet and then perform a loss with respect to it. The encoder model is:</p>
  
  <pre><code>x_input = Input((input_size,))
  enc1 = Dense(encoder_size[0], activation='relu')(x_input)
  drop = Dropout(keep_prob)(enc1)
  enc2 = Dense(encoder_size[1], activation='relu')(drop)
  drop = Dropout(keep_prob)(enc2)
  mu = Dense(latent_dim, activation='linear', name='encoder_mean')(drop)
  encoder = Model(x_input,mu)
  </code></pre>
  
  <p>The structure of loss should be:</p>
  
  <pre><code># the input is the placeholder for the complete input
  def loss(x, y, input):
      reconstruction_loss = mean_squared_error(x, y)
      sample_num = 100
      sample_input = sample_from_input(input, sample_num)
      sample_encoded = encoder.predict(sample_input) &lt;-- this would not work with placeholder
      sample_prior = gaussian(mean=0, std=1)
      # perform KL divergence between sample_encoded and sample_prior
  </code></pre>
  
  <p>I have not found anything similar given. It would be great if somebody can point me in the right direction. </p>