[英]Retrain model in Tensorflow
I have a simple neural-network using Tensorflow. 我有一个使用Tensorflow的简单神经网络。 Here is the Session:
这是会议:
with tensorFlow.Session() as sess:
sess.run(tensorFlow.global_variables_initializer())
for epoch in range(epochs):
i = 0
epochLoss = 0
for _ in range(int(len(data) / batchSize)):
ex, ey = nextBatch(i)
i += 1
feedDict = {x :ex, y:ey }
_, cos = sess.run([optimizer,cost], feed_dict= feedDict)
epochLoss += cos / (int(len(data)) / batchSize)
print("Epoch", epoch + 1, "completed out of", epochs, "loss:", "{:.9f}".format(epochLoss))
save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)
at the last 2 rows I saved the model and restore the graph in another class: 在最后两行中,我保存了模型并将图形还原到另一个类中:
with new_graph.as_default():
with tf.Session(graph=new_graph) as sess:
sess.run(tf.global_variables_initializer())
new_saver = tf.train.import_meta_graph('model.ckpt.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
I want to re-train the model, which means not initialize the weights, just to update them from the last point that it stopped. 我想重新训练模型,这意味着不初始化权重,只是从停止的最后一点开始更新权重。
How can I do that? 我怎样才能做到这一点?
From https://www.tensorflow.org/api_docs/python/state_ops/saving_and_restoring_variables 来自https://www.tensorflow.org/api_docs/python/state_ops/saving_and_restoring_variables
tf.train.Saver.restore(sess, save_path)
tf.train.Saver.restore(sess,save_path)
Restores previously saved variables.
恢复以前保存的变量。
This method runs the ops added by the constructor for restoring variables.
此方法运行由构造函数添加的用于还原变量的操作。 It requires a session in which the graph was launched.
它需要一个启动图形的会话。 The variables to restore do not have to have been initialized, as restoring is itself a way to initialize variables.
要还原的变量不必初始化,因为还原本身就是初始化变量的一种方式。
The following example is from https://www.tensorflow.org/how_tos/variables/ 以下示例来自https://www.tensorflow.org/how_tos/variables/
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "/tmp/model.ckpt")
print("Model restored.")
# Do some work with the model
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.