我在tensorflow中创建并保存了简单的nn:
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, [1, 1],name='input_placeholder')
y = tf.placeholder(tf.float32, [1, 1],name='input_placeholder')
W = tf.get_variable('W', [1, 1])
layer = tf.matmul(x, W, name='layer')
loss = tf.subtract(y,layer)
train_step = tf.train.AdagradOptimizer(0.1).minimize(loss, name='train_step')
all_saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
x_test = np.zeros((1, 1))
y_test = np.zeros((1, 1))
some_output = sess.run([train_step],feed_dict = {x:x_test,y:y_test})
save_path = r'C:\Temp\tf_exp\save_folder\test'
all_saver.save(sess,save_path)
然后我将C:\Temp\tf_exp\save_folder\
中的所有文件都移到了C:\Temp\tf_exp\restore_folder
并将它们移动到了checkpoint
test.data-00000-of-00001
test.index
test.meta
。我移动的文件是:
meta_path = r'C:\Temp\tf_exp\restore_folder\test.meta'
checkpoint_path = r'C:\Temp\tf_exp\restore_folder\\'
print(checkpoint_path)
new_all_saver = tf.train.import_meta_graph(meta_path)
sess=tf.Session()
new_all_saver.restore(sess, tf.train.latest_checkpoint(checkpoint_path))
graph = tf.get_default_graph()
layer= graph.get_tensor_by_name('layer:0')
x=graph.get_tensor_by_name('input_placeholder:0')
然后我尝试从新位置恢复nn:
C:\Temp\tf_exp\restore_folder\\
ERROR:tensorflow:Couldn't match files for checkpoint C:\Temp\tf_exp\save_folder\test
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-9af4e683fc4b> in <module>()
5 new_all_saver = tf.train.import_meta_graph(meta_path)
6 sess=tf.Session()
----> 7 new_all_saver.restore(sess, tf.train.latest_checkpoint(checkpoint_path))
8 graph = tf.get_default_graph()
9 layer= graph.get_tensor_by_name('layer:0')
~\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\training\saver.py in restore(self, sess, save_path)
1555 return
1556 if save_path is None:
-> 1557 raise ValueError("Can't load save_path when it is None.")
1558 logging.info("Restoring parameters from %s", save_path)
1559 sess.run(self.saver_def.restore_op_name,
ValueError: Can't load save_path when it is None.
以下是恢复代码生成的错误:
var test = document.getElementsByName("parameterstest")[0].tagName;
test.style.display='none';
我该如何避免它?移动文件的正确方法是什么?
更新
当我在寻找答案时,看起来使用相对路径是可行的方法。但我不知道如何使用相对路径。我应该将Python的当前工作目录更改为保存模型数据的位置吗?
答案 0 :(得分:4)
创建save_relative_paths=True
时只需添加tf.train.Saver()
:
# original code: all_saver = tf.train.Saver()
all_saver = tf.train.Saver(save_relative_paths=True)
有关详细信息,请参阅official doc。
答案 1 :(得分:3)
您可以尝试通过以下方式恢复:
with tf.Session() as sess:
saver = tf.train.import_meta_graph(/path/to/test.meta)
saver.restore(sess, "path/to/checkpoints/test")
在这种情况下,因为您将检查点的名称设为“test”,所以您有3个文件:
test.data-00000-of-00001
test.index
test.meta
因此,在恢复时,需要将路径放到检查点文件夹+“ / test ”。系统会自动加载相应的数据和索引文件。
答案 2 :(得分:2)
您可以尝试在记事本上打开检查点文件并进行编辑:
model_checkpoint_path: "Name-of-saver"
all_model_checkpoint_paths: "Name-of-saver"