简体   繁体   中英

tensorflow value error with sess.run()

I tried to play with tensorflow a bit but it seems like I am doing something wrong, the little program I made:

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

I want to see the result of tf.matmul(x,y) with the two 20x20 random array. The error that it throws at me:

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.

The feed_dict should contain numerical values, not tf.Variable . Replace your definition of test with:

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

Also you should import numpy as np at the beginning, of course. The code then behaves as you want it to.

For a bit more explanation, you can think of the feed_dict as the numerical values you give to your computational graph, not part of the computational graph (as a tf.Variable would be).

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