Tensorflow Error筆記1

weixin_33785972發表於2017-07-11

願天堂沒有Tensorflow! 阿門。

ValueError: Variable conv1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

將一個模型訓練好,並且儲存了checkpoint後,我嘗試呼叫儲存的模型:

3352522-bc8c134ec4ed9584.png
**儲存的模型檔案**
 with tf.Session() as sess:                
                 print("Reading checkpoints...")
                 ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                 if ckpt and ckpt.model_checkpoint_path:
                     global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                     saver.restore(sess, ckpt.model_checkpoint_path)
                     print('Loading success, global_step is %s' % global_step)
                 else:
                     print('No checkpoint file found')

這是Tensorflow官網給出的模型呼叫方法,可是卻出現了下面的錯誤:

ValueError: Variable conv1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

原因是我們在一次測試過程中,測試了多張圖片,導致我們模型的引數需要重複使用,所以我們需要告訴TF‘允許複用引數’,所以只要在上面的程式碼加上

tf.get_variable_scope().reuse_variables()

Error就會消失。

 with tf.Session() as sess:
                 tf.get_variable_scope().reuse_variables()         
                 print("Reading checkpoints...")
                 ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                 if ckpt and ckpt.model_checkpoint_path:
                     global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                     saver.restore(sess, ckpt.model_checkpoint_path)
                     print('Loading success, global_step is %s' % global_step)
                 else:
                     print('No checkpoint file found')
3352522-d720aca39e1e667f.png
**Error消失**

相關文章