简体   繁体   中英

wrong exception handling in feed_dict in tensorflow

I create my tensorflow graph as following:

s = tf.zeros([T+1, self.hidden_dim])
o = tf.zeros([T, self.word_dim])
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
c = tf.placeholder(tf.float32)
d = tf.placeholder(tf.float32)
dot_product = tf.reduce_sum(tf.multiply(a, b))
s_t = tf.nn.tanh(c + d)
o_t = dot_product

Then afterwards run it as following:

with tf.Session() as sess:
     sess.run(s)
     sess.run(o)
     print type(self.W)
     # For each time step...
     for t in range(T):
          product = sess.run(dot_product, feed_dict={a: self.W, b: s[t-1]})
          s[t] = sess.run(s_t, feed_dict={c: self.U[:, x[t]], d: product}) 
          o[t] = sess.run(o_t, feed_dict={a: self.V, b: s[t]})

Fome reason, I get the following exception:

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

This eror occures on

product = sess.run(dot_product, feed_dict={a: self.W, b: s[t-1]})

But "W" is type of numpy.ndarray. Where is the problem? How can I fix it?

TF complains because your variable s is a tf.Tensor (it has no problems with your 'W' variable).

It it would not be a tensor, this part of the code sess.run(s) would complain with something like this: Fetch argument XX has invalid type <type 'YY'>, must be a string or Tensor. (Can not convert a YY into a Tensor or Operation.) Fetch argument XX has invalid type <type 'YY'>, must be a string or Tensor. (Can not convert a YY into a Tensor or Operation.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM