訓練一個神經網絡的目的是啥?不就是有朝一日讓它有用武之地嗎?可是,在別處使用訓練好的網絡,得先把網絡的參數(就是那些variables)保存下來,怎么保存呢?其實,tensorflow已經給我們提供了很方便的API,來幫助我們實現訓練參數的存儲與讀取,如果想了解詳情,請看晦澀難懂的官方API,接下來我簡單介紹一下我的理解。
保存與讀取數據全靠下面這個類實現:
class tf.train.Saver
當我們需要存儲數據時,下面2條指令就夠了
saver = tf.train.Saver()
save_path = saver.save(sess, model_path)
解釋一下,首先創建一個saver類,然后調用saver的save方法(函數),save需要傳遞兩個參數,一個是你的訓練session,另一個是文件存儲路徑,例如“/tmp/superNet.ckpt”,這個存儲路徑是可以包含文件名的。save方法會返回一個存儲路徑。當然,save方法還有別的參數可以傳遞,這里不再介紹。
然后怎么讀取數據呢?看下面
然后怎么讀取數據呢?看下面
saver = tf.train.Saver()
load_path = saver.restore(sess, model_path)
和存儲數據神似啊!不再贅述。
下面是重點!關於tf.train.Saver()使用的幾點小心得!
- 1、save方法在實現數據讀取時,它僅僅讀數據,關鍵是得有一些提前聲明好的variables來接受這些數據,因此,當save讀取數據到sess時,需要提前聲明與數據匹配的variables,否則程序就報錯了。
- 2、save讀取的數據不需要initialize。
- 3、目前想到的就這么多,隨時補充。
為了對數據存儲和讀取有更直觀的認識,我自己寫了兩個實驗小程序,下面是第一個,訓練網絡並存儲數據,用的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
接下來是讀取數據並做測試!
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