Linux上多次restore Tensorflow模型报错

环境:python3,tensotflow

在恢复了预先训练好的模型进行预测时,第一次是能够成功执行的,但我多次restore模型时,出现了以下问题:

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

解决方法参考https://www.jianshu.com/p/fc11f32800f9

2. NotFoundError (see above for traceback): Key Variable_3 not found in checkpoint

错误提示在checkpoint中没有找到Variable_3。使用以下语句查看checkpoint中的变量,发现的确没有Variable_3。而使用tf.global_variables()查看到新生成了包括Variable_3的新tensor。可参考这里了解具体过程。

解决办法:加入tf.reset_default_graph() 或参考这里进行多项查看。

import os#checkpoint文件的路径checkpoint_path=os.path.join(os.getcwd(),'ckpt1/ner.ckpt')reader=tf.train.NewCheckpointReader(checkpoint_path)var=reader.get_variable_to_shape_map()for key in var: print("tensor_name",key) #显示tensor的内容,若只想查看tensor的名称,可以下面这句注释掉,使结果更清晰 print(reader.get_tensor(key))

其中,我的/ckpt1下的文件如下:

3. 加入tf.reset_default_graph之后,出现以下报错:

Error: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph.

原因参考这里,就是说,不能在以下情况使用tf.reset_default_graph():

Inside a with graph.as_default(): block. 【在with graph.as_default(): 块下面】

Inside a with tf.Session(): block.【在with tf.Session():块下面】

Between creating a tf.InteractiveSession and calling sess.close() 【在使用了tf.InteractiveSession和调用sess.close()之间】

---------------------

作者:xxzhix

来源:CSDN

原文:https://blog.csdn.net/xxzhix/article/details/81983982

版权声明:本文为博主原创文章,转载请附上博文链接!