sess.run()的tensorflow值错误

时间:2018-01-07 14:38:44

标签: python numpy tensorflow

我尝试使用张量流,但似乎我做错了,我做的小程序:

import tensorflow as tf

x = tf.placeholder(tf.float64)
y = tf.placeholder(tf.float64)

test = {"A":tf.Variable(tf.random_normal([20, 20])),
        "B":tf.Variable(tf.random_normal([20, 20]))}

math_stuff = tf.matmul(x,y)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(math_stuff, feed_dict={x:test["A"], y:test["B"]}))

我希望看到tf.matmul(x,y)的结果与两个20x20随机数组。它引发的错误:

Traceback (most recent call last):
  File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\save\tensorflow_play.py", 
 line 15, in <module> print(sess.run(math_stuff, feed_dict={x:test["A"], y:test["B"]}))
  File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
  line 889, in run run_metadata_ptr)
  File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", 
 line 1089, in _run np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
 File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\numeric.py", 
 line 531, in asarray return array(a, dtype, copy=False, order=order)
 ValueError: setting an array element with a sequence.

1 个答案:

答案 0 :(得分:3)

feed_dict应包含数值, tf.Variable。将test的定义替换为:

test = {"A":np.random.randn(20,20),
        "B":np.random.randn(20,20)}

当然,你应该在开始时import numpy as np。然后代码按照您的意愿运行。

有关更多解释,您可以将feed_dict视为计算图的数值,而不是计算图的部分 (作为tf.Variable将是)。