我试图创建一个非常简单的感知器,包含两个隐藏层,用于学习由函数f定义的功能。我遇到的问题(除了我真的不太清楚自己在做什么之外)是,我得到了一个很长的堆栈跟踪(在底部),我认为它起源于定义y_的那一行。这个错误的最后部分是:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float
代码如下:
import tensorflow as tfimport numpy as npdef 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) + bW2 = 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 = []i = 0while i < 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])) i += 1X_test = []y_test = []i = 0while i < 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])) i += 1epochs = 1init_op = tf.global_variables_initializer()with tf.Session() as sess: # initialise the variables sess.run(init_op) for epoch in range(epochs): avg_cost = 0 batchNum = 0 batch_size = 10 total_batch = int(len(X_train) / batch_size) for i 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))
堆栈跟踪如下:
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: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]During handling of the above exception, another 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: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]Caused by 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 for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
回答:
我怀疑问题出在这行代码上,你喂入了y
而不是y_
:
_, c = sess.run([optimizer, loss], feed_dict={x:batch_x, y: batch_y})
将其改写为以下内容应该可以解决问题:
_, c = sess.run([optimizer, loss], feed_dict={x:batch_x, y_: batch_y})