0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually,I want to tempt a new active function after a sigmoid function,which changes the input X when X>6 , output y=1;X<-6,y=0;otherwise,don't change X's elements , how I could do? I now the sigmoid don't effective like ReLU,I just want to try something.
(Sorry for the virgin question on stackoverflow)Actually,I want to tempt a new active function after a sigmoid function,which changes the input X when X>6 , output y=1;X<-6,y=0;otherwise,don't change X's elements , how I could do? I know the sigmoid don't effective like ReLU,I just want to try something.I'm test it on mnist dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.