InvalidArgumentError:您必须为占位符张量提供值

时间:2017-10-16 16:13:19

标签: python tensorflow tensorboard

我是python和tensorflow的新手,所以我做了一些测试和测试,但是关于张量板的例子,我遇到了以下问题:

runfile('D:/Sintítulo 8.py', wdir='D:')
Traceback (most recent call last):

  File "<ipython-input-129-8074dce3c7b8>", line 1, in <module>
    runfile('D:/Sintítulo 8.py', wdir='D:')

  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Sintítulo 8.py", line 47, in <module>
    _, losses, summary=sess.run([train, loss, Merged], feed_dict={X: x_data, Y: y_data})

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1321, in _do_run
    options, run_metadata)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)

InvalidArgumentError: You must feed a value for placeholder tensor 'labels' with dtype float and shape [?,10]
     [[Node: labels = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'labels', defined at:
  File "D:\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 241, in <module>
    main()
  File "D:\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 237, in main
    kernel.start()
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "D:\Anaconda3\lib\site-packages\tornado\ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "D:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "D:\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "D:\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2808, in run_ast_nodes
    if self.run_code(code, result):
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-28-1563b8f59996>", line 1, in <module>
    runfile('D:/Tensorflow/mnist_examp.py', wdir='D:/Tensorflow')
  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)
  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "D:/Tensorflow/mnist_examp.py", line 148, in <module>
    main()
  File "D:/Tensorflow/mnist_examp.py", line 144, in main
    mnist_model(learning_rate, use_two_fc, use_two_conv, hparam)
  File "D:/Tensorflow/mnist_examp.py", line 61, in mnist_model
    y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1548, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2094, in _placeholder
    name=name)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'labels' with dtype float and shape [?,10]
     [[Node: labels = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

正如我所说,我刚刚开始学习,所以代码可能不是最清晰的。我的代码如下:

import tensorflow as tf
import matplotlib.pyplot as plt

x_data = [1, 2, 3, 4]#np.linspace(0,10,10)
y_data = [0, -1, -2, -3]

#weight
W = tf.Variable(tf.random_normal([1]), name="weight")
#bias
b = tf.Variable(tf.random_normal([1]), name="bias")

#plaaceholders
with tf.name_scope ("Input"):
    X=tf.placeholder(tf.float32, name="Input_X")
    Y=tf.placeholder(tf.float32, name="Input_Y")

#Model-linear regression
with tf.name_scope ("Modelo"):
    y_pred = tf.add(tf.multiply(X,W),b)

#cost
with tf.name_scope ("Error"):
    loss = tf.reduce_mean(tf.square(y_pred - y_data))
    tf.summary.scalar("loss",loss)
    tf.summary.histogram("optimizer",loss)

#training algorithm
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

#initializing the variables
init = tf.global_variables_initializer()

#starting the session session 
sess = tf.Session()

# training the line
sess.run(init)
pasos=1000

Merged= tf.summary.merge_all()
writer= tf.summary.FileWriter(r'D:\Tensorflow\tmp\example1', graph=tf.get_default_graph())

for step in range(pasos):
        _, losses, summary=sess.run([train, loss, Merged], feed_dict={X: x_data, Y: y_data})
        writer.add_summary(summary, step)

我在Windows 10 64bit中运行anaconda,我已经尝试过全部搜索,但似乎我看起来的任何地方都是在tensorflow网络中的MNIST示例(带有红色指甲开发视频的那个)而且那个时候太密集了。

0 个答案:

没有答案