在Jupyter笔记本中使用TensorFlow时,我似乎无法恢复已保存的变量.我训练一个ANN,然后我跑,saver.save(sess, "params1.ckpt")然后我再次训练它,保存新的结果,saver.save(sess, "params2.ckpt")但是当我运行saver.restore(sess, "params1.ckpt")我的模型时不加载保存的值params1.ckpt并保留它们params2.ckpt.
如果我运行模型,保存它params.ckpt,然后关闭并停止,然后尝试再次加载它,我收到以下错误:
---------------------------------------------------------------------------
StatusNotOK Traceback (most recent call last)
StatusNotOK: Not found: Tensor name "Variable/Adam" not found in checkpoint files params.ckpt
[[Node: save/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]]
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
<ipython-input-6-39ae6b7641bd> in <module>()
----> 1 saver.restore(sess, "params.ckpt")
/usr/local/lib/python3.5/site-packages/tensorflow/python/training/saver.py in restore(self, sess, save_path)
889 save_path: Path where parameters were previously saved.
890 …Run Code Online (Sandbox Code Playgroud) 我的张量流版本是0.11.我希望在训练后保存图形或保存tensorflow可以加载的其他东西.
我/使用导出和导入MetaGraph
我已经阅读过这篇文章: Tensorflow:如何保存/恢复模型?
我的Save.py文件:
X = tf.placeholder("float", [None, 28, 28, 1], name='X')
Y = tf.placeholder("float", [None, 10], name='Y')
tf.train.Saver()
with tf.Session() as sess:
...run something ...
final_tensor = tf.nn.softmax(py_x, name='final_result')
tf.add_to_collection("final_tensor", final_tensor)
predict_op = tf.argmax(py_x, 1)
tf.add_to_collection("predict_op", predict_op)
saver.save(sess, 'my_project')
Run Code Online (Sandbox Code Playgroud)
然后我运行load.py:
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('my_project.meta')
new_saver.restore(sess, 'my_project')
predict_op = tf.get_collection("predict_op")[0]
for i in range(2):
test_indices = np.arange(len(teX)) # Get A Test Batch
np.random.shuffle(test_indices)
test_indices = test_indices[0:test_size]
print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
sess.run(predict_op, feed_dict={"X:0": teX[test_indices],
"p_keep_conv:0": …Run Code Online (Sandbox Code Playgroud) python machine-learning neural-network deep-learning tensorflow
我正在使用tensorflow并且已经使用该tf.saver()方法训练了一些模型并在每个时期之后保存它们 .我能够很好地保存和加载模型,我正在以通常的方式做到这一点.
with tf.Graph().as_default(), tf.Session() as session:
initialiser = tf.random_normal_initializer(config.mean, config.std)
with tf.variable_scope("model",reuse=None, initializer=initialiser):
m = a2p(session, config, training=True)
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(model_dir)
if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path)
saver.restore(session, ckpt.model_checkpoint_path)
...
for i in range(epochs):
runepoch()
save_path = saver.save(session, '%s.ckpt'%i)
Run Code Online (Sandbox Code Playgroud)
我的代码设置为保存每个时期的模型,应该相应地标记.但是,我注意到,在十五个训练时期之后,我只有最后五个时期的检查点文件(10,11,12,13,14).文档没有说明这一点,所以我不知道为什么会发生这种情况.
保护者是否仅允许保留五个检查点或我做错了什么?
有没有办法确保保留所有检查点?
我在tensorflow中编写了一个卷积神经网络来执行mnist数据集.一切正常,但我想用tf.train.Saver()保存模型.我该怎么办?这是我的代码:
from __future__ import print_function
import tensorflow as tf
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Parameters
learning_rate = 0.001
training_iters = 200000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)
dropout = 0.75 # Dropout, probability to keep units
# tf Graph input
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes]) …Run Code Online (Sandbox Code Playgroud)