尝试创建一个非常简单的感知器,其中包含2个隐藏层,用于学习f定义的函数。我遇到的问题(除了我真的不知道我在做什么)是我得到一个很长的堆栈跟踪(在底部) 我认为是源于定义y_的行。这个错误的最后一部分是:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float
代码:
import tensorflow as tf
import numpy as 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 = []
i = 0
while 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 += 1
X_test = []
y_test = []
i = 0
while 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 += 1
epochs = 1
init_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"]()]]
答案 0 :(得分:1)
我怀疑问题来自这一行,你在那里提供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})