簡體   English   中英

Tensorflow占位符中的InvalidArgumentError

[英]InvalidArgumentError In Tensorflow Placeholder

嘗試創建一個具有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})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM