张量流中的feed_dict中的错误异常处理

时间:2017-04-25 16:03:26

标签: python numpy tensorflow

我创建了张量流图,如下所示:

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

然后按以下方式运行:

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]})

有些原因,我得到以下例外:

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

此错误发生在

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

但“W”是numpy.ndarray的类型。问题出在哪儿?我该如何解决?

1 个答案:

答案 0 :(得分:1)

TF抱怨因为您的变量stf.Tensor(它的'W'变量没有问题)。

它不会是一个张量,代码sess.run(s)的这部分会抱怨这样的事情:Fetch argument XX has invalid type <type 'YY'>, must be a string or Tensor. (Can not convert a YY into a Tensor or Operation.)