2017-06-16 83 views
2

我正試圖在Tensorflow中恢復我的模型。這是我如何保存的模型:
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文檔,您不必在恢復後初始化變量,所以我想它不會去那裏錯了。有誰知道如何解決這一問題?我有我做一些非常愚蠢的一種感覺......

回答

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}) 
+0

要清楚,我必須使用'保護= tf.train.import_meta_graph(「model.ckpt。 meta「)'來創建保護程序?因爲這不起作用。 – Kes

+0

我的不好,看到上面的修改後的版本。 – MZHm

+0

誰能告訴我爲什麼這個工程? – ycyoon