2017-04-10 63 views
0

说我有一个tensorflow形状:Feed_dict和numpy的重塑

y_ = tf.placeholder(tf.float32,[None,19],name='Labels') 

这里我的想法是,每次19中得到19个元素的矢量,并将其添加(pluging)到Y_

和列表inputlabel57为长:

我想这个名单喂线进入y_

sess.run(train_step,feed_dict={x:xdata,y_:np.reshape(inputlabel,(3,19))}) 

这种喂食不起作用,我真的不知道我该如何解决它。这里要说的是我得到的错误信息:

--------------------------------------------------------------------------- 
InvalidArgumentError      Traceback (most recent call last) 
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 
    1021  try: 
-> 1022  return fn(*args) 
    1023  except errors.OpError as e: 

c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 
    1003         feed_dict, fetch_list, target_list, 
-> 1004         status, run_metadata) 
    1005 

c:\users\engine\appdata\local\programs\python\python35\lib\contextlib.py in __exit__(self, type, value, traceback) 
    65    try: 
---> 66     next(self.gen) 
    67    except StopIteration: 

c:\users\engine\appdata\local\programs\python\python35\lib\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 'Labels' with dtype float 
    [[Node: Labels = 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-7-1da2dc43ca63> in <module>() 
     1 for j in range(len(batch_xs)-1): 
----> 2  print(sess.run(train_step,feed_dict={x:batch_xs[j],y_:np.reshape(batch_ys[j],(3,numberOFClasses))})) 

c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata) 
    765  try: 
    766  result = self._run(None, fetches, feed_dict, options_ptr, 
--> 767       run_metadata_ptr) 
    768  if run_metadata: 
    769   proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) 

c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 
    963  if final_fetches or final_targets: 
    964  results = self._do_run(handle, final_targets, final_fetches, 
--> 965        feed_dict_string, options, run_metadata) 
    966  else: 
    967  results = [] 

c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 
    1013  if handle is None: 
    1014  return self._do_call(_run_fn, self._session, feed_dict, fetch_list, 
-> 1015       target_list, options, run_metadata) 
    1016  else: 
    1017  return self._do_call(_prun_fn, self._session, handle, feed_dict, 

c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 
    1033   except KeyError: 
    1034   pass 
-> 1035  raise type(e)(node_def, op, message) 
    1036 
    1037 def _extend_graph(self): 

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

Caused by op 'Labels', defined at: 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\runpy.py", line 184, in _run_module_as_main 
    "__main__", mod_spec) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\runpy.py", line 85, in _run_code 
    exec(code, run_globals) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\__main__.py", line 3, in <module> 
    app.launch_new_instance() 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance 
    app.start() 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelapp.py", line 474, in start 
    ioloop.IOLoop.instance().start() 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start 
    super(ZMQIOLoop, self).start() 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\ioloop.py", line 887, in start 
    handler_func(fd_obj, events) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper 
    return fn(*args, **kwargs) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events 
    self._handle_recv() 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv 
    self._run_callback(callback, msg) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback 
    callback(*args, **kwargs) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper 
    return fn(*args, **kwargs) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher 
    return self.dispatch_shell(stream, msg) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell 
    handler(stream, idents, msg) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request 
    user_expressions, allow_stdin) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute 
    res = shell.run_cell(code, store_history=store_history, silent=silent) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell 
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell 
    interactivity=interactivity, compiler=compiler, result=result) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes 
    if self.run_code(code, result): 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code 
    exec(code_obj, self.user_global_ns, self.user_ns) 
    File "<ipython-input-3-49d5bdb3e7ad>", line 6, in <module> 
    y_ = tf.placeholder(tf.float32,[None],name='Labels') 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1502, in placeholder 
    name=name) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2149, in _placeholder 
    name=name) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op 
    op_def=op_def) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2327, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1226, in __init__ 
    self._traceback = _extract_stack() 

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

** **更新

为随后Inputlabel声明:

.............. 
inputlabel =[] 
.................. 
for i in batch(Training_Data,batchSize): 
    inputlabel.append(i) 

def batch(iterable, n=1): 
    l = len(iterable) 
    for ndx in range(0, l, n): 
     yield iterable[ndx:min(ndx + n, l)] 

没有该类型的问题是解决了,我得到了一些神秘的东东:

InvalidArgumentError (see above for traceback): Incompatible shapes: [3,19] vs. [57,19] 
    [[Node: gradients/mul_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](gradients/mul_grad/Shape, gradients/mul_grad/Shape_1)]] 
+1

show inputLabel。这对我有用。 – Himaprasoon

+1

'print(np.reshape(inputlabel,[3,19])。shape)'。我认为你的输入标签是不同的形状。输出应该是'(3,19)' – Himaprasoon

回答

1

这工作:

import numpy as np 
import tensorflow as tf 
y_ = tf.placeholder(tf.float32,[None,19],name='Labels') 
sess = tf.InteractiveSession() 
labels = np.zeros(57, dtype=np.float32) 
sess.run(y_, feed_dict = {y_: np.reshape(labels, (3,19))}) 

难道你的inputlabel是错误的类型?

0

你只需要转换的(NSAMPLES)整数编码1热标签形状的阵列(NSAMPLES,19)只包含0和1,当您导入数据列表,

(1, 8, 2) -> [[1, 0, 0, ...], [0, 0, 1, 0, 0, ...], [0, 1, 0, 0, ...]]

你可以那样做:

label_1_hot_coding = row[-1] 
array_of_bits = np.zeros(numberOFClasses) 
n = 1 
for i,_in enumerate(array_of_bits): 
    if n== label_1_hot_coding: 
     array_of_bits[i] = 1 
Training_Labels.append(array_of_bits) 

您的标签,现在自然形状(nSamples, numberOfClasses),和形状(batchSize, numberOfClasses)的批次,这是你的程序的其他部分想。