title: Custom Keras loss function with Keras by discriminator with Gan neural network
body: <h2>GAN Discriminator</h2>
  
  <p>I use this code below get the disriminator of GAN neural network:</p>
  
  <pre class="lang-py prettyprint-override"><code>import tensorflow as tf
  import numpy as np
  from IPython.display import display, Audio
  
  tf.reset_default_graph()
  saver = tf.train.import_meta_graph('./infer/infer.meta')
  graph = tf.get_default_graph()
  sess = tf.InteractiveSession()
  saver.restore(sess, tf.train.latest_checkpoint('model/'))
  
  # here is z with underline, it doesn't showing ceractly in stack.
  # I use random data to test this function.
  _z = np.random.uniform(-1., 1., size=[5, 257])
  x = graph.get_tensor_by_name('x:0')
  D_z = graph.get_tensor_by_name('D_z:0')
  D_z = sess.run(D_z, {x: _z})
  print(D_z)
  </code></pre>
  
  <h2>Custom Keras loss function</h2>
  
  <p>I wan to create a function to customed the keras loss fuction:</p>
  
  <pre class="lang-py prettyprint-override"><code># Load the graph
  tf.reset_default_graph()
  saver = tf.train.import_meta_graph('./infer/infer.meta')
  graph = tf.get_default_graph()
  sess = tf.InteractiveSession()
  saver.restore(sess, tf.train.latest_checkpoint('model/'))
  
  def gan_loss(y_true, y_pred):
      _z = y_pred
      x = graph.get_tensor_by_name('x:0')
      D_z = graph.get_tensor_by_name('D_z:0')
      D_z = sess.run(D_z, {x: _z})
  
      return D_z
  </code></pre>
  
  <h2>Problem I have met</h2>
  
  <p>I got the problem that show me: Can not feed tesor,  you have to feed it with numpy or other type of data.</p>
  
  <blockquote>
    <p>TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.</p>
  </blockquote>
  
  <p>I fond related problem in Stak:<a href="https://stackoverflow.com/questions/49192051/converting-tensor-to-np-array-using-k-eval-in-keras-returns-invalidargumenterr">Converting Tensor to np.array using K.eval() in Keras returns InvalidArgumentError</a></p>
  
  <p><a href="https://stackoverflow.com/questions/42560209/tensorflow-how-to-feed-a-placeholder-variable-with-a-tensor">Tensorflow: How to feed a placeholder variable with a tensor?</a></p>
  
  <h2>GAN neural network , How i get the discriminator</h2>
  
  <pre class="lang-py prettyprint-override"><code>X = tf.placeholder(tf.float32, [None, 257], name='x')
  D_z, h3 = discriminator(X)
  D_z = tf.identity(D_z, name='D_z')
  
  D_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='GAN/Discriminator')
  # global_step = tf.train.get_or_create_global_step()
  saver = tf.train.Saver(D_vars)
  infer_dir = './infer/'
  tf.train.write_graph(tf.get_default_graph(), infer_dir, 'infer.pbtxt')
  infer_metagraph_fp = os.path.join(infer_dir, 'infer.meta')
  tf.train.export_meta_graph(
      filename=infer_metagraph_fp,
      clear_devices=True,
      saver_def=saver.as_saver_def())
  tf.reset_default_graph()
  </code></pre>