我的TensorFlow代码产生输入维数错误

时间:2018-08-13 12:14:02

标签: python tensorflow tensorboard

InvalidArgumentError                      Traceback (most recent call last)
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1321     try:
-> 1322       return fn(*args)
   1323     except errors.OpError as e:

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1306       return self._call_tf_sessionrun(
-> 1307           options, feed_dict, fetch_list, target_list, run_metadata)
   1308 

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1408           self._session, options, feed_dict, fetch_list, target_list,
-> 1409           run_metadata)
   1410     else:

InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
     [[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-8-73b3b51081a0> in <module>()
     18             print(i, 'Trainingsgenauigkeit = ', genau*100,'%', 'verlust = ', verlust)
     19 
---> 20             s = sess.run(merged, feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
     21             writer.add_summary(s, i)
     22     genauigkeitTest = sess.run(genauigkeitTraining, feed_dict={x:mnist.test.images,y:mnist.test.labels, keep_prob:1.0})

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1133     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1134       results = self._do_run(handle, final_targets, final_fetches,
-> 1135                              feed_dict_tensor, options, run_metadata)
   1136     else:
   1137       results = []

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1314     if handle is None:
   1315       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316                            run_metadata)
   1317     else:
   1318       return self._do_call(_prun_fn, handle, feeds, fetches)

C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1333         except KeyError:
   1334           pass
-> 1335       raise type(e)(node_def, op, message)
   1336 
   1337   def _extend_graph(self):

InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
     [[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]

Caused by op 'Konvolution1/Konvolution1Bild', defined at:
  File "C:\Anaconda\envs\Matura\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Anaconda\envs\Matura\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "C:\Anaconda\envs\Matura\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
    app.start()
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelapp.py", line 486, in start
    self.io_loop.start()
  File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\platform\asyncio.py", line 127, in start
    self.asyncio_loop.run_forever()
  File "C:\Anaconda\envs\Matura\lib\asyncio\base_events.py", line 421, in run_forever
    self._run_once()
  File "C:\Anaconda\envs\Matura\lib\asyncio\base_events.py", line 1425, in _run_once
    handle._run()
  File "C:\Anaconda\envs\Matura\lib\asyncio\events.py", line 127, in _run
    self._callback(*self._args)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\ioloop.py", line 759, in _run_callback
    ret = callback()
  File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 536, in <lambda>
    self.io_loop.add_callback(lambda : self._handle_events(self.socket, 0))
  File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2901, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-5-9e8915c7d2a2>", line 26, in <module>
    tf.summary.image('Konvolution1Bild', tf.unstack(konv1, axis = 3))
  File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\summary\summary.py", line 162, in image
    tag=tag, tensor=tensor, max_images=max_outputs, name=scope)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 387, in image_summary
    bad_color=bad_color, name=name)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
    op_def=op_def)
  File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\ops.py", line 1718, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
     [[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]

产生此错误的代码片段:

merged = tf.summary.merge_all()

init = tf.global_variables_initializer()

with tf.Session() as sess:

    writer= tf.summary.FileWriter( "/tmp/mnist_logs", sess.graph)

    sess.run(init)

    for i in range(anzahlIterationen):
        batch_x, batch_y = mnist.train.next_batch(batchGrösse)

        sess.run(training, feed_dict={x: batch_x, y: batch_y, dropoutPH:dropout})

        if i % (np.floor(anzahlIterationen/5)) == 0:
            verlust, genau = sess.run([kreuzentropie, genauigkeitTraining], feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
            print(i, 'Trainingsgenauigkeit = ', genau*100,'%', 'verlust = ', verlust)

            s = sess.run(merged, feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
            writer.add_summary(s, i)
    genauigkeitTest = sess.run(genauigkeitTraining, feed_dict={x:mnist.test.images,y:mnist.test.labels, keep_prob:1.0})
    print("test accuracy %g"%(testAccurac

我真的不知道此错误来自何处。我想不出一种方法来影响我输入的张量,因为我要从中获取数据。

该代码已可用于以前的版本。我想不起来我在摘要中进行了一些更改

0 个答案:

没有答案