Saving and Restoring Trained Models in Tensorflow
After training a model in Tensorflow, preserving and reusing it is crucial. Here's how to effectively handle model storage:
Saving the Trained Model (Tensorflow version 0.11 and above):
Example Code:
import tensorflow as tf # Prepare input placeholders w1 = tf.placeholder("float", name="w1") w2 = tf.placeholder("float", name="w2") # Define test operation w3 = tf.add(w1, w2) w4 = tf.multiply(w3, tf.Variable(2.0, name="bias"), name="op_to_restore") # Initialize variables and run session sess = tf.Session() sess.run(tf.global_variables_initializer()) # Create saver object saver = tf.train.Saver() # Save the model saver.save(sess, 'my_test_model', global_step=1000)
Restoring the Saved Model:
Example Code:
# Restore model saver = tf.train.import_meta_graph('my_test_model-1000.meta') saver.restore(sess, tf.train.latest_checkpoint('./')) # Get placeholders and feed data w1 = sess.graph.get_tensor_by_name("w1:0") w2 = sess.graph.get_tensor_by_name("w2:0") feed_dict = {w1: 13.0, w2: 17.0} # Run saved operation op_to_restore = sess.graph.get_tensor_by_name("op_to_restore:0") result = sess.run(op_to_restore, feed_dict)
The above is the detailed content of How to Effectively Save and Restore Trained Models in TensorFlow?. For more information, please follow other related articles on the PHP Chinese website!