Reputation: 3
I tried to explore a simple demo,but got this error. How could I modify my code?
import tensorflow as tf
sess = tf.Session()
x_ = tf.Variable([[-9,6,-2,3], [-4,3,-1,10]], dtype=tf.float32)
x = tf.placeholder(tf.float32, shape=[4,2])
y = tf.nn.relu(x)
sess.run(tf.global_variables_initializer())
print(sess.run(x_))
print(sess.run(y,feed_dict={x:x_}))
The output I get is:
[[ -9. 6. -2. 3.]
[ -4. 3. -1. 10.]]
Traceback (most recent call last):
File "C:\Users\jy\Documents\NetSarang\Xftp\Temporary\test.py", line 19, in <module>
print(sess.run(y,feed_dict={x:x_}))
File "C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 938, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
File "C:\Program Files\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
Upvotes: 0
Views: 2455
Reputation: 63978
First, the dimensionality of your x_
variable wrong: currently, it's of shape [2, 4]
, but you're attempting to use it in a slot that's expecting data of shape [4, 2]
.
Second, tf.Variable
is meant to represent literally a variable (in the mathematical sense) within your neural net model that'll be tuned as you train your model -- it's a mechanism for maintaining state.
To provide actual input to train your model, you can simply pass in a regular Python array (or numpy array) instead.
Here's a fixed version of your code that appears to do what you want:
import tensorflow as tf
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[4,2])
y = tf.nn.relu(x)
sess.run(tf.global_variables_initializer())
x_ = [[-9, -4], [6, 3], [-2, -1], [3, 10]]
print(sess.run(y, feed_dict={x:x_}))
If you really did want a node within your neural net to start off initialized with those values, I'd get rid of the placeholder and use x_
directly:
import tensorflow as tf
sess = tf.Session()
x = tf.Variable([[-9, -4], [6, 3], [-2, -1], [3, 10]], dtype=tf.float32)
y = tf.nn.relu(x)
sess.run(tf.global_variables_initializer())
print(sess.run(y))
This is probably not what you meant to do, though -- it's sort of unusual to have a model that doesn't accept any input.
Upvotes: 1