gpt4 book ai didi

python - Tensorflow 占位符中的 InvalidArgumentError

转载 作者:行者123 更新时间:2023-11-30 09:50:34 26 4
gpt4 key购买 nike

尝试创建一个非常简单的具有 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"]()]]

最佳答案

我怀疑问题源于这一行,您在其中输入 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})

关于python - Tensorflow 占位符中的 InvalidArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45747890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com