简体   繁体   English

恢复张量流模型

[英]Restoring the tensorflow model

I want to restore a tensorflow model after it's trained. 我想在训练后恢复张量流模型。 I know that I can use tf.train.Saver but the problem is with the restoring because I get confused with the names for get_tensor_by_name . 我知道我可以使用tf.train.Saver但问题出在恢复上,因为我对get_tensor_by_name的名称感到困惑。 Can anybody help me? 有谁能够帮助我? This is my graph: 这是我的图:

x_hat = tf.placeholder(tf.float32, shape=[None, dim_img], name='input_img')
x = tf.placeholder(tf.float32, shape=[None, dim_img], name='target_img')

# dropout
keep_prob = tf.placeholder(tf.float32, name='keep_prob')

# input for PMLR
z_in = tf.placeholder(tf.float32, shape=[None, dim_z], name='latent_variable')

# network architecture
y, z, loss, neg_marginal_likelihood, KL_divergence = vae.autoencoder(x_hat, x, dim_img, dim_z, n_hidden,
                                                                                keep_prob)

When you save a model you save 2 things: 1) the meta graph, that is a representation of the graph (all the TF symbols you've defined; and 2) the checkpoint which contains the actual Variable values (which are saved and restored by name). 保存模型时,您保存了2件事:1)元图,即图的表示形式(已定义的所有TF符号; 2)检查点,其中包含实际的Variable值(已保存和恢复)按名字)。

When you restore you can restore one or both of those components. 还原时,您可以还原其中一个或两个组件。 What you are describing is restoring both the meta graph AND the checkpoint data. 您正在描述的是同时还原元图和检查点数据。 In this case you need to look up the various operations and tensors you're interested by name, which can be confusing (especially if you didn't name your variables well, which you should always do). 在这种情况下,您需要按名称查找您感兴趣的各种运算和张量,这可能会造成混淆(特别是如果您没有很好地命名变量,则应始终这样做)。

# In this method you import the meta graph then restore
saver = tf.train.import_meta_graph('my-save-dir/my-model-10000.meta')
saver.restore(sess, 'my-save-dir/my-model-10000')

The other option to restoring (which I prefer mysefl) is to not load the meta graph at all. 恢复的另一个选项(我更喜欢mysefl)是根本不加载元图。 Instead just re-run the same code you originally used to create the graph (if you've done things well this will all be organized in one place). 而是只重新运行最初用于创建图形的相同代码(如果做得很好,所有这些都将集中在一个地方)。 Then you only restore the checkpoint. 然后,您仅还原检查点。 This approach has the benefit that you can easily keep a reference to all the operations you'll need (such as cost, train_op, placeholders, etc). 这种方法的好处是您可以轻松保留对所需所有操作的引用(例如,成本,train_op,占位符等)。

# This method only performs the restor operation 
# assuming the graph is already constructure
saver.restore(sess, 'my-save-dir/my-model-10000')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM