我正在尝试在Tensorflow中恢复我的模型。这就是我保存模型的方式:
ae = autoencoder(input_shape=[None, height, width, depth], conv_strides=[[1, stride1, stride1, 1], [1, stride2, stride2, 1]], n_filters=[1, num_filters, num_filters], filter_sizes=[size_filter, size_filter, size_filter], corruption=False, poolsize=2)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
# create a session to use the graph
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
# Network is trained here
...
saver.save(sess, "model.ckpt")
然后我尝试使用此代码恢复它(在另一个文件中,在训练模型后,在单独的会话中):
with tf.Session() as sess:
saver = tf.train.import_meta_graph("model.ckpt.meta")
saver.restore(sess, "model.ckpt")
print("Model restored")
ae = autoencoder(input_shape=[None, height, width, depth], conv_strides=[[1, stride1, stride1, 1], [1, stride2, stride2, 1]], n_filters=[1, num_filters, num_filters], filter_sizes=[size_filter, size_filter, size_filter], corruption=False, poolsize=2)
# create stuff here to reconstruct images using the autoencoder
...
recon = sess.run(ae['y'], feed_dict={ae['x']: batch})
打印出模型已恢复,但出现错误:
FailedPreconditionError:尝试使用未初始化的值
根据Tensorflow文档,您不必在恢复后初始化变量,所以我想它不会出错。有谁知道如何解决这一问题?我有一种感觉,我正在做一些非常愚蠢的事......
答案 0 :(得分:2)
试试这个:
ae = autoencoder(input_shape=[None, height, width, depth], conv_strides=
[[1, stride1, stride1, 1], [1, stride2, stride2, 1]], n_filters=[1, num_filters, num_filters], filter_sizes=[size_filter, size_filter, size_filter], corruption=False, poolsize=2)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "model.ckpt")
print("Model restored")
# create stuff here to reconstruct images using the autoencoder
...
recon = sess.run(ae['y'], feed_dict={ae['x']: batch})