zoukankan      html  css  js  c++  java
  • InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]

    在莫烦Python教程的“Dropout 解决 overfitting”一节中,出现错误如下:

    InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]

    runfile('E:/python/kerasTest/tfDropoutTest9.py', wdir='E:/python/kerasTest')
    C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagessklearncross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
      "This module will be removed in 0.20.", DeprecationWarning)
    
    runfile('E:/python/kerasTest/tfDropoutTest9.py', wdir='E:/python/kerasTest')
    Traceback (most recent call last):
    
      File "<ipython-input-2-64f3a3bcd083>", line 1, in <module>
        runfile('E:/python/kerasTest/tfDropoutTest9.py', wdir='E:/python/kerasTest')
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilssitesitecustomize.py", line 710, in runfile
        execfile(filename, namespace)
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilssitesitecustomize.py", line 101, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)
    
      File "E:/python/kerasTest/tfDropoutTest9.py", line 67, in <module>
        train_result = sess.run(merged,feed_dict={xs:X_train,ys:y_train,keep_prob:1})
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonclientsession.py", line 895, in run
        run_metadata_ptr)
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonclientsession.py", line 1128, in _run
        feed_dict_tensor, options, run_metadata)
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonclientsession.py", line 1344, in _do_run
        options, run_metadata)
    
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonclientsession.py", line 1363, in _do_call
        raise type(e)(node_def, op, message)
    
    InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
         [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
    
    Caused by op 'Placeholder_1', defined at:
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilsipythonstart_kernel.py", line 241, in <module>
        main()
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilsipythonstart_kernel.py", line 237, in main
        kernel.start()
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelkernelapp.py", line 477, in start
        ioloop.IOLoop.instance().start()
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packageszmqeventloopioloop.py", line 177, in start
        super(ZMQIOLoop, self).start()
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ornadoioloop.py", line 888, in start
        handler_func(fd_obj, events)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ornadostack_context.py", line 277, in null_wrapper
        return fn(*args, **kwargs)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packageszmqeventloopzmqstream.py", line 440, in _handle_events
        self._handle_recv()
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packageszmqeventloopzmqstream.py", line 472, in _handle_recv
        self._run_callback(callback, msg)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packageszmqeventloopzmqstream.py", line 414, in _run_callback
        callback(*args, **kwargs)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ornadostack_context.py", line 277, in null_wrapper
        return fn(*args, **kwargs)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelkernelbase.py", line 283, in dispatcher
        return self.dispatch_shell(stream, msg)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelkernelbase.py", line 235, in dispatch_shell
        handler(stream, idents, msg)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelkernelbase.py", line 399, in execute_request
        user_expressions, allow_stdin)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelipkernel.py", line 196, in do_execute
        res = shell.run_cell(code, store_history=store_history, silent=silent)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesipykernelzmqshell.py", line 533, in run_cell
        return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesIPythoncoreinteractiveshell.py", line 2698, in run_cell
        interactivity=interactivity, compiler=compiler, result=result)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesIPythoncoreinteractiveshell.py", line 2808, in run_ast_nodes
        if self.run_code(code, result):
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesIPythoncoreinteractiveshell.py", line 2862, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-1-64f3a3bcd083>", line 1, in <module>
        runfile('E:/python/kerasTest/tfDropoutTest9.py', wdir='E:/python/kerasTest')
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilssitesitecustomize.py", line 710, in runfile
        execfile(filename, namespace)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packagesspyderutilssitesitecustomize.py", line 101, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)
      File "E:/python/kerasTest/tfDropoutTest9.py", line 39, in <module>
        ys = tf.placeholder(tf.float32,[None,10])
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonopsarray_ops.py", line 1680, in placeholder
        return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonopsgen_array_ops.py", line 4105, in _placeholder
        "Placeholder", dtype=dtype, shape=shape, name=name)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonframeworkop_def_library.py", line 787, in _apply_op_helper
        op_def=op_def)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonframeworkops.py", line 3160, in create_op
        op_def=op_def)
      File "C:UsersAdminAppDataLocalcondacondaenvs	ensorflowlibsite-packages	ensorflowpythonframeworkops.py", line 1625, in __init__
        self._traceback = self._graph._extract_stack() 
    
    InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
         [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

    代码如下:

    import tensorflow as tf
    from sklearn.datasets import load_digits
    from sklearn.cross_validation import train_test_split
    from sklearn.preprocessing import LabelBinarizer
    
    #load data 
    digits = load_digits()
    X = digits.data#从0到9的图片
    y = digits.target
    y =LabelBinarizer().fit_transform(y)
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=.3)
    
    def add_layer(inputs,in_size,out_size,layer_name,activation_function=None):
       #add one more layer and return the output of this layer
       Weights = tf.Variable(tf.random_normal([in_size,out_size]))
       biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
       Wx_plus_b = tf.matmul(inputs, Weights) + biases
       Wx_plus_b = tf.nn.dropout(Wx_plus_b,keep_prob)
       if activation_function is None:
            outputs = Wx_plus_b
       else:
           outputs = activation_function(Wx_plus_b)
       tf.summary.histogram(layer_name+'/outputs',outputs)
       return outputs
    
    
    xs = tf.placeholder(tf.float32,[None,64])#8*8
    ys = tf.placeholder(tf.float32,[None,10])
    keep_prob = tf.placeholder(tf.float32)
    
    
    #add output layer
    l1 = add_layer(xs,64,50,'l1',activation_function=tf.nn.tanh)
    prediction = add_layer(l1,50,10,'l2',activation_function=tf.nn.softmax)
    
    #the loss between prediction and real data
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                                  reduction_indices=[1]))#loss
    tf.summary.scalar('loss',cross_entropy)
    train_step = tf.train.GradientDescentOptimizer(0.6).minimize(cross_entropy)
    
    sess = tf.Session()
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter("logs/train",sess.graph)
    test_writer = tf.summary.FileWriter("logs/test",sess.graph)
    
    sess.run(tf.global_variables_initializer())
    
    for i in range(500):
        sess.run(train_step,feed_dict={xs:X_train,ys:y_train,keep_prob:0.5})
        if i % 50 == 0:
            train_result = sess.run(merged,feed_dict={xs:X_train,ys:y_train,keep_prob:1})
            test_result = sess.run(merged,feed_dict={xs:X_test,ys:y_test,keep_prob:1})
            train_writer.add_summary(train_result,i)
            test_writer.add_summary(test_result,i)

    原因:

    在feed_dict中没有加入keep_prob的key和value

    sess.run(train_step,feed_dict={xs:X_train,ys:y_train,keep_prob:0.5})中,没有写入keep_prob:0.5

    造成feed_dict和placeholder的对应问题,但改正后,仍报该错误。反复检查了几遍,并没有发现问题。

    最后实在无解,关闭了Spyder和anaconda,再打开anaconda和Spyder,居然可以正常运行了。。

    但是也只是第一次可以正常运行,当删了生成的log文件,再次运行时,仍报该错误..至于为什么第二次运行就又报错仍未解决。

    虽然问题不大,但是改了feed_dict后,问题还是没能解决,被困扰了一天,因此记录一下。

  • 相关阅读:
    (Toolbar)Android中如何消除Toolbar左边的空白
    (TextView)Android中为TextView赋初始值
    (Edittext)Android中界面中有多个Edittext,如何默认让第二个获取焦点
    (警告)Android中报Custom view `&#215;&#215;&#215;` has setOnTouchListener called on it but does not override performClick警告
    (Toolbar)Android中app:showASAction的值及含义
    个人课程总结
    (list)关于list清空问题的解决
    Ubuntu hive 安装过程中遇到的一些问题
    学习进度——第十七周
    个人课程总结
  • 原文地址:https://www.cnblogs.com/zeroingToOne/p/8511462.html
Copyright © 2011-2022 走看看