代码之家  ›  专栏  ›  技术社区  ›  asn

InvalidArgumentError:必须使用dtype float为占位符张量'placeholder'提供值

  •  0
  • asn  · 技术社区  · 8 年前

    import tensorflow as tf
    import numpy as np
    import pandas as pd
    from sklearn.datasets import load_boston
    import matplotlib.pyplot as plt
    
    boston=load_boston()
    type(boston)
    boston.feature_names
    
    bd=pd.DataFrame(data=boston.data,columns=boston.feature_names)
    
    bd['Price']=pd.DataFrame(data=boston.target)
    np.random.shuffle(bd.values)
    
    
    W0=tf.Variable(0.0000000000003)
    W1=tf.Variable(0.000000000002)
    b=tf.Variable(0.0000000000001)
        #print(bd.shape[1])
    
    tf.summary.histogram('Weights', W0)
    tf.summary.histogram('Weights', W1)
    tf.summary.histogram('Biases', b)
    
    
    
    dataset_input=bd.iloc[:, 0 : bd.shape[1]-1];
        #dataset_input.head(2)
    
    dataset_output=bd.iloc[:, bd.shape[1]-1]
    dataset_output=dataset_output.values
    dataset_output=dataset_output.reshape((bd.shape[0],1)) #converted (506,) to (506,1) because in pandas
        #the shape was not changing and it was needed later in feed_dict
    
    
    dataset_input=dataset_input.values  #only dataset_input is in DataFrame form and converting it into np.ndarray
    
        # ADDED
    dataset_input = np.array(dataset_input, dtype=np.float32)
        # ADDED
    dataset_output = np.array(dataset_output, dtype=np.float32)
    
    X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
    Y=tf.placeholder(tf.float32, shape=(None,1))
    
    Y_=W0*X*X + W1*X + b
        #Y_pred = tf.add(tf.multiply(tf.pow(X, pow_i), W), Y_pred)
    print(X.shape)
    print(Y.shape)
    
    
    loss=tf.reduce_mean(tf.square(Y_-Y))
    tf.summary.scalar('loss',loss)
    
    optimizer=tf.train.GradientDescentOptimizer(0.0000000000001)
    train=optimizer.minimize(loss)
    
    init=tf.global_variables_initializer()#tf.global_variables_initializer()#tf.initialize_all_variables()
    sess=tf.Session()
    sess.run(init)
    
    
    
    wb_=[]
    with tf.Session() as sess:
        summary_merge = tf.summary.merge_all()
    
        writer=tf.summary.FileWriter("Users/ajay/Desktop",sess.graph)
    
        epochs=6
        sess.run(init)
    
        for i in range(epochs):
            s_mer=sess.run(summary_merge,feed_dict={X: dataset_input, Y: dataset_output})  #ERROR________ERROR
            sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
    
                #CHANGED
            print("loss",sess.run(loss, feed_dict={X:dataset_input,Y:dataset_output}))
            #sess.run(loss, feed_dict={X:dataset_input,Y:dataset_output})
            writer.add_summary(s_mer,i)
    
            tf.summary.histogram(name="loss",values=loss)
            if(i%5==0):
                print(i, sess.run([W0,W1,b]))
                wb_.append(sess.run([W0,W1,b]))
    
            print(writer.get_logdir())
            print(writer.close())
    

    它给出以下错误:

    (?, 13)
    (?, 1)
    
    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
       1038     try:
    -> 1039       return fn(*args)
       1040     except errors.OpError as e:
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
       1020                                  feed_dict, fetch_list, target_list,
    -> 1021                                  status, run_metadata)
       1022 
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
         87             try:
    ---> 88                 next(self.gen)
         89             except StopIteration:
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
        465           compat.as_text(pywrap_tensorflow.TF_Message(status)),
    --> 466           pywrap_tensorflow.TF_GetCode(status))
        467   finally:
    
    InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
         [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
    
    During handling of the above exception, another exception occurred:
    
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-8-fdf4bc1529e6> in <module>()
         73 
         74     for i in range(epochs):
    ---> 75         s_mer=sess.run(summary_merge,feed_dict={X: dataset_input, Y: dataset_output})  #ERROR________ERROR
         76         sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
         77 
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
        776     try:
        777       result = self._run(None, fetches, feed_dict, options_ptr,
    --> 778                          run_metadata_ptr)
        779       if run_metadata:
        780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
        980     if final_fetches or final_targets:
        981       results = self._do_run(handle, final_targets, final_fetches,
    --> 982                              feed_dict_string, options, run_metadata)
        983     else:
        984       results = []
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
       1030     if handle is None:
       1031       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
    -> 1032                            target_list, options, run_metadata)
       1033     else:
       1034       return self._do_call(_prun_fn, self._session, handle, feed_dict,
    
    ~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
       1050         except KeyError:
       1051           pass
    -> 1052       raise type(e)(node_def, op, message)
       1053 
       1054   def _extend_graph(self):
    
    InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
         [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
    
    Caused by op 'Placeholder', defined at:
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
        app.launch_new_instance()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
        app.start()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
        self.io_loop.start()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 127, in start
        self.asyncio_loop.run_forever()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/base_events.py", line 421, in run_forever
        self._run_once()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/base_events.py", line 1431, in _run_once
        handle._run()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/events.py", line 145, in _run
        self._callback(*self._args)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 117, in _handle_events
        handler_func(fileobj, events)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
        return fn(*args, **kwargs)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
        self._handle_recv()
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
        self._run_callback(callback, msg)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
        callback(*args, **kwargs)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
        return fn(*args, **kwargs)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
        return self.dispatch_shell(stream, msg)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
        handler(stream, idents, msg)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
        user_expressions, allow_stdin)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
        res = shell.run_cell(code, store_history=store_history, silent=silent)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
        return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
        raw_cell, store_history, silent, shell_futures)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
        interactivity=interactivity, compiler=compiler, result=result)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2903, in run_ast_nodes
        if self.run_code(code, result):
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-1-67ab0a0aaf1a>", line 44, in <module>
        X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
        name=name)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
        name=name)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
        op_def=op_def)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/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' with dtype float
         [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
    

        MacBook-Air:Desktop ajay$ python3 LR.py
    /anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
      from ._conv import register_converters as _register_converters
    (?, 13)
    (?, 1)
    2018-08-16 13:40:30.387637: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    loss 592.02905
    0 [1.08720556e-07, 3.084597e-10, 4.6065617e-12]
    Users/ajay/Desktop
    None
    loss 591.9113
    Users/ajay/Desktop
    None
    loss 591.7939
    Users/ajay/Desktop
    None
    loss 591.67664
    Users/ajay/Desktop
    None
    loss 591.5597
    Users/ajay/Desktop
    None
    loss 591.44293
    5 [6.502434e-07, 1.8367864e-09, 2.7130916e-11]
    Users/ajay/Desktop
    None
    

    我的终端是否忽略了占位符或其他内容?

    有谁能给我解释一下如何识别它们,它们最有可能发生在什么时候?

    另外,这段代码中是否有与数据类型相关的内容

    /anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   Mohan Radhakrishnan    8 年前

    我想你可以加上 tf.reset_default_graph() 作为你在Jupyter笔记本上代码的第一行并检查。

    推荐文章