我试着玩了一下 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"]}))
我想用两个 20x20 随机数组查看 tf.matmul(x,y) 的结果。它向我抛出的错误:
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.
请您参考如下方法:
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 那样)。
