在 Tensorflow 中保存和恢復經過訓練的模型
在 Tensorflow 中訓練模型後,保存和重用它至關重要。以下是有效處理模型儲存的方法:
保存訓練好的模型(Tensorflow 0.11以上版本):
範例程式碼:
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)
還原儲存的模式型號:
範例程式碼:
# 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)
以上是如何在TensorFlow中有效保存並恢復訓練好的模型?的詳細內容。更多資訊請關注PHP中文網其他相關文章!