Gu-Youngfeng / LearningTensorflow

Some codes during the study process.
0 stars 1 forks source link

How to keep the trained model and how to read the generated files? #7

Open Gu-Youngfeng opened 5 years ago

Gu-Youngfeng commented 5 years ago

In Tensorflow, we use object tf.train.Saver() to save the trained model, the generated files include

Except for the first file(checkpoint), all the files cannot be opened by normal notepad. What do they mean and how can I use them?

Gu-Youngfeng commented 5 years ago

1. How to keep the trained model?

# variables initialization
init = tf.global_variables_initializer()
# saver
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(init)
    saver.save(sess, "models/lstm.ckpt") # save the model into lstm.ckpt

2. How to read the generated files?

saver = tf.train.import_meta_graph("models/nn.ckpt.meta")
with tf.Session() as sess:
    saver.restore(sess, "models/nn.ckpt")
    graph = tf.get_default_graph()
    ...