1
votes

GAN Discriminator

I use this code below get the disriminator of GAN neural network:

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)

Custom Keras loss function

I wan to create a function to customed the keras loss fuction:

# 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

Problem I have met

I got the problem that show me: Can not feed tesor, you have to feed it with numpy or other type of data.

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

I fond related problem in Stak:Converting Tensor to np.array using K.eval() in Keras returns InvalidArgumentError

Tensorflow: How to feed a placeholder variable with a tensor?

GAN neural network , How i get the discriminator

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()
1

1 Answers

0
votes

I was able to reproduce your error using below simple code where I am feeding a tensor to feed_dict -

Code to reproduce the error -

%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
import numpy as np

x = tf.placeholder(tf.float32)
y = x * 42

with tf.Session() as sess:
  a = tf.constant(2)
  train_accuracy = y.eval(session=sess,feed_dict={x: a})
  print(train_accuracy)

Output -

1.15.2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-44556cb4551b> in <module>()
     10 with tf.Session() as sess:
     11   a = tf.constant(2)
---> 12   train_accuracy = y.eval(session=sess,feed_dict={x: a})
     13   print(train_accuracy)

3 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1129                             'For reference, the tensor object was ' +
   1130                             str(feed_val) + ' which was passed to the '
-> 1131                             'feed with key ' + str(feed) + '.')
   1132 
   1133           subfeed_dtype = subfeed_t.dtype.as_numpy_dtype

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor("Const_6:0", shape=(), dtype=int32) which was passed to the feed with key Tensor("Placeholder_9:0", dtype=float32).

I was able to fix it when I converted the tensor to numpy type for feed_dict. So in your case, convert y_pred to numpy type.

Fixed Code -

%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
import numpy as np

x = tf.placeholder(tf.float32)
y = x * 42  

with tf.Session() as sess:
  a = tf.constant(2)
  a = np.array(a.eval())
  train_accuracy = y.eval(session=sess,feed_dict={x: b})
  print(train_accuracy)

Output -

1.15.2
84.0

Hope this answers your question. Happy Learning.