tensorflow報錯:InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [1...

tensorflow報錯:InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]

我是在執(zhí)行tensorflow/examples/speech_commands/freeze.py時報的錯,具體錯誤如下:

(tensorflow) ubuntu@VM-0-13-ubuntu:~/tf3$ python tensorflow/examples/speech_commands/freeze.py \
> --start_checkpoint=/tmp/speech_commands_train/conv.ckpt-18000 \
> --output_file=/tmp/my_frozen_graph.pb
2018-09-13 14:34:38.589857: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-09-13 14:34:38.699275: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:897] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-09-13 14:34:38.699780: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties: 
name: Tesla P40 major: 6 minor: 1 memoryClockRate(GHz): 1.531
pciBusID: 0000:00:06.0
totalMemory: 22.38GiB freeMemory: 22.21GiB
2018-09-13 14:34:38.699807: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0
2018-09-13 14:34:39.003121: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-09-13 14:34:39.003185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971]      0 
2018-09-13 14:34:39.003194: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0:   N 
2018-09-13 14:34:39.003625: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 21544 MB memory) -> physical GPU (device: 0, name: Tesla P40, pci bus id: 0000:00:06.0, compute capability: 6.1)
Traceback (most recent call last):
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1278, in _do_call
    return fn(*args)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1263, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1350, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]
         [[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@final_fc_bias"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](final_fc_bias, save/RestoreV2/_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1725, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 877, in run
    run_metadata_ptr)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1272, in _do_run
    run_metadata)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1291, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]
         [[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@final_fc_bias"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](final_fc_bias, save/RestoreV2/_1)]]

Caused by op 'save/Assign', defined at:
  File "tensorflow/examples/speech_commands/freeze.py", line 208, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "tensorflow/examples/speech_commands/freeze.py", line 134, in main
    models.load_variables_from_checkpoint(sess, FLAGS.start_checkpoint)
  File "/home/ubuntu/tf3/tensorflow/examples/speech_commands/models.py", line 154, in load_variables_from_checkpoint
    saver = tf.train.Saver(tf.global_variables())
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1281, in __init__
    self.build()
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1293, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1330, in _build
    build_save=build_save, build_restore=build_restore)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 778, in _build_internal
    restore_sequentially, reshape)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 419, in _AddRestoreOps
    assign_ops.append(saveable.restore(saveable_tensors, shapes))
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 112, in restore
    self.op.get_shape().is_fully_defined())
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/state_ops.py", line 216, in assign
    validate_shape=validate_shape)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_state_ops.py", line 60, in assign
    use_locking=use_locking, name=name)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]
         [[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@final_fc_bias"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](final_fc_bias, save/RestoreV2/_1)]]


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tensorflow/examples/speech_commands/freeze.py", line 208, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "tensorflow/examples/speech_commands/freeze.py", line 134, in main
    models.load_variables_from_checkpoint(sess, FLAGS.start_checkpoint)
  File "/home/ubuntu/tf3/tensorflow/examples/speech_commands/models.py", line 155, in load_variables_from_checkpoint
    saver.restore(sess, start_checkpoint)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1759, in restore
    err, "a mismatch between the current graph and the graph")
tensorflow.python.framework.errors_impl.InvalidArgumentError: Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]
         [[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@final_fc_bias"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](final_fc_bias, save/RestoreV2/_1)]]

Caused by op 'save/Assign', defined at:
  File "tensorflow/examples/speech_commands/freeze.py", line 208, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "tensorflow/examples/speech_commands/freeze.py", line 134, in main
    models.load_variables_from_checkpoint(sess, FLAGS.start_checkpoint)
  File "/home/ubuntu/tf3/tensorflow/examples/speech_commands/models.py", line 154, in load_variables_from_checkpoint
    saver = tf.train.Saver(tf.global_variables())
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1281, in __init__
    self.build()
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1293, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1330, in _build
    build_save=build_save, build_restore=build_restore)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 778, in _build_internal
    restore_sequentially, reshape)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 419, in _AddRestoreOps
    assign_ops.append(saveable.restore(saveable_tensors, shapes))
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 112, in restore
    self.op.get_shape().is_fully_defined())
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/state_ops.py", line 216, in assign
    validate_shape=validate_shape)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_state_ops.py", line 60, in assign
    use_locking=use_locking, name=name)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/ubuntu/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [12] rhs shape= [6]
         [[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@final_fc_bias"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](final_fc_bias, save/RestoreV2/_1)]]

(tensorflow) ubuntu@VM-0-13-ubuntu:~/tf3$ 

具體描述一下我遇到此錯誤的情形,我是在traning自己的數(shù)據(jù)時,traning時執(zhí)行的這個命令:

python tensorflow/examples/speech_commands/train.py \
--data_url= \
--data_dir=../traning \
--wanted_words=jia,yu,wo,yi

導(dǎo)出時執(zhí)行的這個命令:

python tensorflow/examples/speech_commands/freeze.py \
--start_checkpoint=/tmp/speech_commands_train/conv.ckpt-18000 \
--output_file=/tmp/my_frozen_graph.pb

然后就報錯了,網(wǎng)上搜索了好久,有說需要把之前traning之后的數(shù)據(jù)刪除,重新traning,我照做后再次traning(對于Tesla P40 GPU來講,每次traning要花費100分鐘左右)還是這個錯誤。最后認真看了一下,似乎是數(shù)量對應(yīng)不上而已。我改了一下導(dǎo)出命令,就成功了。

python tensorflow/examples/speech_commands/freeze.py \
--start_checkpoint=/tmp/speech_commands_train/conv.ckpt-18000 \
--output_file=/tmp/my_frozen_graph.pb \
--wanted_words=jia,yu,wo,yi
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容