在深度學(xué)習(xí)中,,不管使用那種學(xué)習(xí)框架,,我們會(huì)遇到一個(gè)很重要的問(wèn)題,,那就是在訓(xùn)練完之后,,如何存儲(chǔ)學(xué)習(xí)到的深度網(wǎng)絡(luò)的參數(shù),?在測(cè)試時(shí),,如何調(diào)用這些網(wǎng)絡(luò)參數(shù),?針對(duì)這兩個(gè)問(wèn)題,本篇博文主要探索TensorFlow如何解決他們,?本篇博文分為三個(gè)部分,,第一是講解tensorflow相關(guān)的函數(shù),,第二是代碼例程,第三是運(yùn)行結(jié)果,。
一 tensorflow相關(guān)的函數(shù)
我們說(shuō)的這兩個(gè)功能主要由一個(gè)類(lèi)來(lái)完成,,class tf.train.Saver
- saver = tf.train.Saver()
- save_path = saver.save(sess, model_path)
- load_path = saver.restore(sess, model_path)
saver = tf.train.Saver() 由類(lèi)創(chuàng)建對(duì)象saver,用于保存和調(diào)用學(xué)習(xí)到的網(wǎng)絡(luò)參數(shù),,參數(shù)保存在checkpoints里
save_path = saver.save(sess, model_path) 保存學(xué)習(xí)到的網(wǎng)絡(luò)參數(shù)到model_path路徑中
load_path = saver.restore(sess, model_path) 調(diào)用model_path路徑中的保存的網(wǎng)絡(luò)參數(shù)到graph中
二 代碼例程
- '''''
- Save and Restore a model using TensorFlow.
- This example is using the MNIST database of handwritten digits
- (http://yann./exdb/mnist/)
-
- Author: Aymeric Damien
- Project: https://github.com/aymericdamien/TensorFlow-Examples/
- '''
-
- # Import MINST data
- from tensorflow.examples.tutorials.mnist import input_data
- mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
-
- import tensorflow as tf
-
- # Parameters
- learning_rate = 0.001
- batch_size = 100
- display_step = 1
- model_path = "/home/lei/TensorFlow-Examples-master/examples/4_Utils/model.ckpt"
-
- # Network Parameters
- n_hidden_1 = 256 # 1st layer number of features
- n_hidden_2 = 256 # 2nd layer number of features
- n_input = 784 # MNIST data input (img shape: 28*28)
- n_classes = 10 # MNIST total classes (0-9 digits)
-
- # tf Graph input
- x = tf.placeholder("float", [None, n_input])
- y = tf.placeholder("float", [None, n_classes])
-
-
- # Create model
- def multilayer_perceptron(x, weights, biases):
- # Hidden layer with RELU activation
- layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
- layer_1 = tf.nn.relu(layer_1)
- # Hidden layer with RELU activation
- layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
- layer_2 = tf.nn.relu(layer_2)
- # Output layer with linear activation
- out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
- return out_layer
-
- # Store layers weight & bias
- weights = {
- 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
- 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
- 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
- }
- biases = {
- 'b1': tf.Variable(tf.random_normal([n_hidden_1])),
- 'b2': tf.Variable(tf.random_normal([n_hidden_2])),
- 'out': tf.Variable(tf.random_normal([n_classes]))
- }
-
- # Construct model
- pred = multilayer_perceptron(x, weights, biases)
-
- # Define loss and optimizer
- cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
- optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
-
- # Initializing the variables
- init = tf.initialize_all_variables()
-
- # 'Saver' op to save and restore all the variables
- saver = tf.train.Saver()
-
- # Running first session
- print "Starting 1st session..."
- with tf.Session() as sess:
- # Initialize variables
- sess.run(init)
-
- # Training cycle
- for epoch in range(3):
- avg_cost = 0.
- total_batch = int(mnist.train.num_examples/batch_size)
- # Loop over all batches
- for i in range(total_batch):
- batch_x, batch_y = mnist.train.next_batch(batch_size)
- # Run optimization op (backprop) and cost op (to get loss value)
- _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
- y: batch_y})
- # Compute average loss
- avg_cost += c / total_batch
- # Display logs per epoch step
- if epoch % display_step == 0:
- print "Epoch:", '%04d' % (epoch+1), "cost=", \
- "{:.9f}".format(avg_cost)
- print "First Optimization Finished!"
-
- # Test model
- correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
- # Calculate accuracy
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
- print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})
-
- # Save model weights to disk
- save_path = saver.save(sess, model_path)
- print "Model saved in file: %s" % save_path
-
- # Running a new session
- print "Starting 2nd session..."
- with tf.Session() as sess:
- # Initialize variables
- sess.run(init)
-
- # Restore model weights from previously saved model
- load_path = saver.restore(sess, model_path)
- print "Model restored from file: %s" % save_path
-
- # Resume training
- for epoch in range(7):
- avg_cost = 0.
- total_batch = int(mnist.train.num_examples / batch_size)
- # Loop over all batches
- for i in range(total_batch):
- batch_x, batch_y = mnist.train.next_batch(batch_size)
- # Run optimization op (backprop) and cost op (to get loss value)
- _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
- y: batch_y})
- # Compute average loss
- avg_cost += c / total_batch
- # Display logs per epoch step
- if epoch % display_step == 0:
- print "Epoch:", '%04d' % (epoch + 1), "cost=", \
- "{:.9f}".format(avg_cost)
- print "Second Optimization Finished!"
-
- # Test model
- correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
- # Calculate accuracy
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
- print "Accuracy:", accuracy.eval(
- {x: mnist.test.images, y: mnist.test.labels})
三 運(yùn)行結(jié)果
參考資料:
https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/4_Utils/save_restore_model.py
https://www./versions/r0.9/api_docs/Python/state_ops.html#Saver
|