我是tensorflow的初学者,所以如果这是一个愚蠢的问题并且答案很明显,请原谅。
我创建了一个Tensorflow图,其中从X和y的占位符开始,我优化了一些表示我的模型的张量。图的一部分是可以计算预测向量的内容,例如对于线性回归,像
y_model = tf.add(tf.mul(X,w),d)
y_vals = sess.run(y_model,feed_dict={....})
训练完成后,我有可接受的w和d值,现在我想保存模型供以后使用。然后,在另一个python session 中,我想还原模型,以便再次运行
## Starting brand new python session
import tensorflow as tf
## somehow restor the graph and the values here: how????
## so that I can run this:
y_vals = sess.run(y_model,feed_dict={....})
对于一些不同的数据并获取y值。
我希望它以某种方式工作,其中还存储并还原了用于从占位符计算y值的图形-只要占位符得到正确的数据,这应该在没有用户(应用该占位符的人)的情况下透明地工作。模型)。
据我了解tf.train.Saver()。save(..)仅保存变量,但我也想保存图形。我认为tf.train.export_meta_graph在这里可能是相关的,但我不明白如何正确使用它,文档对我来说有点神秘,示例甚至在任何地方都没有使用export_meta_graph。
最佳答案
在docs中,尝试以下操作:
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model.
..
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in file: %s" % save_path)
您可以指定路径。
如果要还原模型,请尝试:
with tf.Session() as sess:
saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
saver.restore(sess, "/tmp/model.ckpt")
关于tensorflow - 如何保存训练有素的 tensorflow 模型以供以后使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38641887/