python - InvalidArgumentError In Tensorflow Placeholder -
trying create simple perceptron 2 hidden layers learns function defined f. problem i'm having (besides not having clue i'm doing) i'm getting long stack trace (at bottom) think originating @ line y_ defined. final part of error is:
invalidargumenterror (see above traceback): must feed value placeholder tensor 'placeholder_1' dtype float
code:
import tensorflow tf import numpy np def f(x): y1 = 2.41*x[0] + 0.09 + np.random.normal() y2 = 3.84*x[1] + 5.3 + np.random.normal() y3 = 0.79*x[2] + 13.0 + np.random.normal() return [y1, y2, y3] x = tf.placeholder(tf.float32, shape = ([none, 3])) y_ = tf.placeholder(tf.float32, shape = ([none, 3])) w1 = tf.variable(tf.zeros([3, 10])) b = tf.variable(tf.zeros([10])) x_med = tf.matmul(x, w1) + b w2 = tf.variable(tf.zeros([10, 3])) y = tf.matmul(x_med, w2) dif = tf.subtract(y_, y) sqrd = tf.reduce_mean(tf.multiply(dif, dif)) / 3 #loss = tf.reduce_sum( tf.matmul(np.array(y_ - y), np.array(y_ - y))) loss = tf.reduce_sum(sqrd) optimizer = tf.train.gradientdescentoptimizer(learning_rate = 0.03).minimize(loss) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) x_train = [] y_train = [] = 0 while < 100000: x1 = np.random.uniform(0, 100) x2 = np.random.uniform(0, 100) x3 = np.random.uniform(0, 100) x_train.append([x1, x2, x3]) y_train.append(f([x1, x2, x3])) += 1 x_test = [] y_test = [] = 0 while < 250: x1 = np.random.uniform(0, 100) x2 = np.random.uniform(0, 100) x3 = np.random.uniform(0, 100) x_test.append([x1, x2, x3]) y_test.append(f([x1, x2, x3])) += 1 epochs = 1 init_op = tf.global_variables_initializer() tf.session() sess: # initialise variables sess.run(init_op) epoch in range(epochs): avg_cost = 0 batchnum = 0 batch_size = 10 total_batch = int(len(x_train) / batch_size) in range(total_batch): batch_x = x_train[batchnum: batchnum + batch_size] batch_y = y_train[batchnum: batchnum + batch_size] batchnum += batch_size _, c = sess.run([optimizer, loss], feed_dict={x:batch_x, y: batch_y}) print(c) avg_cost += c / total_batch print("epoch: " + str(epoch) + " avg cost: " + str(avg_cost))
stack trace:
traceback (most recent call last): file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1039, in _do_call return fn(*args) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1021, in _run_fn status, run_metadata) file "c:\users\allbee\anaconda3\lib\contextlib.py", line 89, in __exit__ next(self.gen) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.tf_getcode(status)) tensorflow.python.framework.errors_impl.invalidargumenterror: must feed value placeholder tensor 'placeholder_1' dtype float [[node: placeholder_1 = placeholder[dtype=dt_float, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] during handling of above exception, exception occurred: traceback (most recent call last): file "anntest.py", line 74, in <module> feed_dict={x:batch_x, y: batch_y}) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 778, in run run_metadata_ptr) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 982, in _run feed_dict_string, options, run_metadata) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1032, in _do_run target_list, options, run_metadata) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1052, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.invalidargumenterror: must feed value placeholder tensor 'placeholder_1' dtype float [[node: placeholder_1 = placeholder[dtype=dt_float, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] caused op 'placeholder_1', defined at: file "anntest.py", line 12, in <module> y_ = tf.placeholder(tf.float32, shape = ([none, 3])) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1507, in placeholder name=name) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1997, in _placeholder name=name) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op op_def=op_def) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op original_op=self._default_original_op, op_def=op_def) file "c:\users\allbee\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__ self._traceback = _extract_stack() invalidargumenterror (see above traceback): must feed value placeholder tensor 'placeholder_1' dtype float [[node: placeholder_1 = placeholder[dtype=dt_float, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
i suspect problem stems line, feed y
instead of y_
:
_, c = sess.run([optimizer, loss], feed_dict={x:batch_x, y: batch_y})
rewriting following should work:
_, c = sess.run([optimizer, loss], feed_dict={x:batch_x, y_: batch_y})
Comments
Post a Comment