訓練一個神經網絡的目的是啥?不就是有朝一日讓它有用武之地嗎?但是,在別處使用訓練好的網絡,得先把網絡的參數(就是那些variables)保存下來,怎麼保存呢?其實,tensorflow已經給咱們提供了很方便的API,來幫助咱們實現訓練參數的存儲與讀取,若是想了解詳情,請看晦澀難懂的官方API,接下來我簡單介紹一下個人理解。html
保存與讀取數據全靠下面這個類實現:python
class tf.train.Saver
當咱們須要存儲數據時,下面2條指令就夠了git
saver = tf.train.Saver()
save_path = saver.save(sess, model_path)
saver = tf.train.Saver()
load_path = saver.restore(sess, model_path)
和存儲數據神似啊!再也不贅述。小程序
下面是重點!關於tf.train.Saver()使用的幾點當心得!api
爲了對數據存儲和讀取有更直觀的認識,我本身寫了兩個實驗小程序,下面是第一個,訓練網絡並存儲數據,用的MNIST數據集網絡
import tensorflow as tf import sys # load MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('data', one_hot=True) # 一些 hyper parameters activation = tf.nn.relu batch_size = 100 iteration = 20000 hidden1_units = 30 # 注意!這裏是存儲路徑! model_path = sys.path[0] + '/simple_mnist.ckpt' X = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) W_fc1 = tf.Variable(tf.truncated_normal([784, hidden1_units], stddev=0.2)) b_fc1 = tf.Variable(tf.zeros([hidden1_units])) W_fc2 = tf.Variable(tf.truncated_normal([hidden1_units, 10], stddev=0.2)) b_fc2 = tf.Variable(tf.zeros([10])) def inference(img): fc1 = activation(tf.nn.bias_add(tf.matmul(img, W_fc1), b_fc1)) logits = tf.nn.bias_add(tf.matmul(fc1, W_fc2), b_fc2) return logits def loss(logits, labels): cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, labels) loss = tf.reduce_mean(cross_entropy) return loss def evaluation(logits, labels): correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return accuracy logits = inference(X) loss = loss(logits, y_) train_op = tf.train.AdamOptimizer(1e-4).minimize(loss) accuracy = evaluation(logits, y_) # 先實例化一個Saver()類 saver = tf.train.Saver() init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for i in xrange(iteration): batch = mnist.train.next_batch(batch_size) if i%1000 == 0 and i: train_accuracy = sess.run(accuracy, feed_dict={X: batch[0], y_: batch[1]}) print "step %d, train accuracy %g" %(i, train_accuracy) sess.run(train_op, feed_dict={X: batch[0], y_: batch[1]}) print '[+] Test accuracy is %f' % sess.run(accuracy, feed_dict={X: mnist.test.images, y_: mnist.test.labels}) # 存儲訓練好的variables save_path = saver.save(sess, model_path) print "[+] Model saved in file: %s" % save_path
接下來是讀取數據並作測試!session
import tensorflow as tf import sys from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('data', one_hot=True) activation = tf.nn.relu hidden1_units = 30 model_path = sys.path[0] + '/simple_mnist.ckpt' X = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) W_fc1 = tf.Variable(tf.truncated_normal([784, hidden1_units], stddev=0.2)) b_fc1 = tf.Variable(tf.zeros([hidden1_units])) W_fc2 = tf.Variable(tf.truncated_normal([hidden1_units, 10], stddev=0.2)) b_fc2 = tf.Variable(tf.zeros([10])) def inference(img): fc1 = activation(tf.nn.bias_add(tf.matmul(img, W_fc1), b_fc1)) logits = tf.nn.bias_add(tf.matmul(fc1, W_fc2), b_fc2) return logits def evaluation(logits, labels): correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return accuracy logits = inference(X) accuracy = evaluation(logits, y_) saver = tf.train.Saver() with tf.Session() as sess: # 讀取以前訓練好的數據 load_path = saver.restore(sess, model_path) print "[+] Model restored from %s" % load_path print '[+] Test accuracy is %f' % sess.run(accuracy, feed_dict={X: mnist.test.images, y_: mnist.test.labels})
轉:https://www.jianshu.com/p/83fa3aa2d0e9函數