如果你希望系統性的了解神經網絡,請參考零基礎入門深度學習系列 ,下面我會粗略的介紹一下本文中實現神經網絡需要了解的知識。
什么是深度神經網絡?
神經網絡包含三層:輸入層(X)、隱藏層和輸出層:f(x)
每層之間每個節點都是完全連接的,其中包含權重(W)。每層都存在一個偏移值(b)。
每一層節點的計算方式如下:
其中g()代表激活函數,o()代表softmax輸出函數。
使用Flow Graph的方式來表達如何正向推導神經網絡,可以表達如下:
x: 輸入值
a(x):表示每個隱藏層的pre-activation的數據,由前一個隱藏層數據(h)、權重(w)和偏移值(b)計算而來
h(x):表示每個隱藏層的數據
f(x):表示輸出層數據
激活函數ReLUs
激活函數有很多種類,例如sigmoid、tanh、ReLUs,對於深度神經網絡而言,目前最流行的是ReLUs。
關於幾種激活函數的對比可以參見:常用激活函數的總結與比較
ReLUs函數如下:
反向傳播
現在,我們需要知道一個神經網絡的每個連接上的權值是如何得到的。我們可以說神經網絡是一個模型,那么這些權值就是模型的參數,也就是模型要學習的東西。然而,一個神經網絡的連接方式、網絡的層數、每層的節點數這些參數,則不是學習出來的,而是人為事先設置的。對於這些人為設置的參數,我們稱之為超參數(Hyper-Parameters)。
接下來,我們將要介紹神經網絡的訓練算法:反向傳播算法。
具體內容請參考:零基礎入門深度學習(3) - 神經網絡和反向傳播算法
SGD
梯度下降算法是一種不斷調整參數值從而達到減少Loss function的方法,通過不斷迭代而獲得最佳的權重值。梯度下降傳統上是每次迭代都使用全部訓練數據來進行參數調整,隨機梯度下降則是使用少量訓練數據來進行調整。
關於GD和SGD的區別可以參考:GD(梯度下降)和SGD(隨機梯度下降)
正則化和Dropout
正則化和Dropout都是一些防止過度擬合的方法,詳細介紹可以參考:正則化方法:L1和L2 regularization、數據集擴增、dropout
正則化:通過在Loss function中加入對權重(w)的懲罰,可以限制權重值變得非常大
Dropout: 通過隨機拋棄一些節點,使得神經網絡更加多樣性,然后組合起來獲得的結果更加通用。
好吧,基本的概念大概介紹了一遍,開始擼代碼啦。
請先參考深度學習實踐系列(1)- 從零搭建notMNIST邏輯回歸模型,獲得notMNIST.pickle的訓練數據。
1. 引用第三方庫
# These are all the modules we'll be using later. Make sure you can import them # before proceeding further. from __future__ import print_function import numpy as np import tensorflow as tf from six.moves import cPickle as pickle from six.moves import range
2. 讀取數據
pickle_file = 'notMNIST.pickle' with open(pickle_file, 'rb') as f: save = pickle.load(f) train_dataset = save['train_dataset'] train_labels = save['train_labels'] valid_dataset = save['valid_dataset'] valid_labels = save['valid_labels'] test_dataset = save['test_dataset'] test_labels = save['test_labels'] del save # hint to help gc free up memory print('Training set', train_dataset.shape, train_labels.shape) print('Validation set', valid_dataset.shape, valid_labels.shape) print('Test set', test_dataset.shape, test_labels.shape)
Training set (200000, 28, 28) (200000,) Validation set (10000, 28, 28) (10000,) Test set (10000, 28, 28) (10000,)
3. 調整數據格式以便后續訓練
image_size = 28 num_labels = 10 def reformat(dataset, labels): dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) # Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...] labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32) return dataset, labels train_dataset, train_labels = reformat(train_dataset, train_labels) valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) test_dataset, test_labels = reformat(test_dataset, test_labels) print('Training set', train_dataset.shape, train_labels.shape) print('Validation set', valid_dataset.shape, valid_labels.shape) print('Test set', test_dataset.shape, test_labels.shape)
Training set (200000, 784) (200000, 10) Validation set (10000, 784) (10000, 10) Test set (10000, 784) (10000, 10)
4. 定義神經網絡
# With gradient descent training, even this much data is prohibitive. # Subset the training data for faster turnaround. train_subset = 10000 graph = tf.Graph() with graph.as_default(): # Input data. # Load the training, validation and test data into constants that are # attached to the graph. tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) tf_train_labels = tf.constant(train_labels[:train_subset]) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. # These are the parameters that we are going to be training. The weight # matrix will be initialized using random values following a (truncated) # normal distribution. The biases get initialized to zero. weights = tf.Variable( tf.truncated_normal([image_size * image_size, num_labels])) biases = tf.Variable(tf.zeros([num_labels])) # Training computation. # We multiply the inputs with the weight matrix, and add biases. We compute # the softmax and cross-entropy (it's one operation in TensorFlow, because # it's very common, and it can be optimized). We take the average of this # cross-entropy across all training examples: that's our loss. logits = tf.matmul(tf_train_dataset, weights) + biases loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # Optimizer. # We are going to find the minimum of this loss using gradient descent. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. # These are not part of training, but merely here so that we can report # accuracy figures as we train. train_prediction = tf.nn.softmax(logits) valid_prediction = tf.nn.softmax( tf.matmul(tf_valid_dataset, weights) + biases) test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)
5. 使用梯度下降(GD)訓練神經網絡
num_steps = 801 def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) with tf.Session(graph=graph) as session: # This is a one-time operation which ensures the parameters get initialized as # we described in the graph: random weights for the matrix, zeros for the # biases. tf.global_variables_initializer().run() print('Initialized') for step in range(num_steps): # Run the computations. We tell .run() that we want to run the optimizer, # and get the loss value and the training predictions returned as numpy # arrays. _, l, predictions = session.run([optimizer, loss, train_prediction]) if (step % 100 == 0): print('Loss at step %d: %f' % (step, l)) print('Training accuracy: %.1f%%' % accuracy( predictions, train_labels[:train_subset, :])) # Calling .eval() on valid_prediction is basically like calling run(), but # just to get that one numpy array. Note that it recomputes all its graph # dependencies. print('Validation accuracy: %.1f%%' % accuracy( valid_prediction.eval(), valid_labels)) print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))
Initialized Loss at step 0: 16.516306 Training accuracy: 11.4% Validation accuracy: 11.7% Loss at step 100: 2.269041 Training accuracy: 71.8% Validation accuracy: 70.2% Loss at step 200: 1.816886 Training accuracy: 74.8% Validation accuracy: 72.6% Loss at step 300: 1.574824 Training accuracy: 76.0% Validation accuracy: 73.6% Loss at step 400: 1.415523 Training accuracy: 77.1% Validation accuracy: 73.9% Loss at step 500: 1.299691 Training accuracy: 78.0% Validation accuracy: 74.4% Loss at step 600: 1.209450 Training accuracy: 78.6% Validation accuracy: 74.6% Loss at step 700: 1.135888 Training accuracy: 79.0% Validation accuracy: 74.9% Loss at step 800: 1.074228 Training accuracy: 79.5% Validation accuracy: 75.0% Test accuracy: 82.3%
6. 使用隨機梯度下降(SGD)算法
batch_size = 128 graph = tf.Graph() with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed # at run time with a training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. weights = tf.Variable( tf.truncated_normal([image_size * image_size, num_labels])) biases = tf.Variable(tf.zeros([num_labels])) # Training computation. logits = tf.matmul(tf_train_dataset, weights) + biases loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # Optimizer. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. train_prediction = tf.nn.softmax(logits) valid_prediction = tf.nn.softmax( tf.matmul(tf_valid_dataset, weights) + biases) test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) num_steps = 3001 with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() print("Initialized") for step in range(num_steps): # Pick an offset within the training data, which has been randomized. # Note: we could use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # Generate a minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # Prepare a dictionary telling the session where to feed the minibatch. # The key of the dictionary is the placeholder node of the graph to be fed, # and the value is the numpy array to feed to it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} _, l, predictions = session.run( [optimizer, loss, train_prediction], feed_dict=feed_dict) if (step % 500 == 0): print("Minibatch loss at step %d: %f" % (step, l)) print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("Validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
Initialized Minibatch loss at step 0: 18.121506 Minibatch accuracy: 11.7% Validation accuracy: 15.0% Minibatch loss at step 500: 1.192153 Minibatch accuracy: 80.5% Validation accuracy: 76.1% Minibatch loss at step 1000: 1.309419 Minibatch accuracy: 75.8% Validation accuracy: 76.8% Minibatch loss at step 1500: 0.739157 Minibatch accuracy: 83.6% Validation accuracy: 77.3% Minibatch loss at step 2000: 0.854160 Minibatch accuracy: 85.2% Validation accuracy: 77.5% Minibatch loss at step 2500: 1.045702 Minibatch accuracy: 76.6% Validation accuracy: 78.8% Minibatch loss at step 3000: 0.940078 Minibatch accuracy: 79.7% Validation accuracy: 78.8% Test accuracy: 85.8%
7. 使用ReLUs激活函數
batch_size = 128 hidden_layer_size = 1024 graph = tf.Graph() with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed # at run time with a training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. # Hidden layer (RELU magic) weights_hidden = tf.Variable( tf.truncated_normal([image_size * image_size, hidden_layer_size])) biases_hidden = tf.Variable(tf.zeros([hidden_layer_size])) hidden = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) # Output layer weights_output = tf.Variable( tf.truncated_normal([hidden_layer_size, num_labels])) biases_output = tf.Variable(tf.zeros([num_labels])) # Training computation. logits = tf.matmul(hidden, weights_output) + biases_output loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # Optimizer. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. # Creation of hidden layer of RELU for the validation and testing process train_prediction = tf.nn.softmax(logits) hidden_validation = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) valid_prediction = tf.nn.softmax( tf.matmul(hidden_validation, weights_output) + biases_output) hidden_prediction = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) test_prediction = tf.nn.softmax(tf.matmul(hidden_prediction, weights_output) + biases_output) num_steps = 3001 def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) with tf.Session(graph=graph) as session: tf.initialize_all_variables().run() print("Initialized") for step in range(num_steps): # Pick an offset within the training data, which has been randomized. # Note: we could use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # Generate a minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # Prepare a dictionary telling the session where to feed the minibatch. # The key of the dictionary is the placeholder node of the graph to be fed, # and the value is the numpy array to feed to it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} _, l, predictions = session.run( [optimizer, loss, train_prediction], feed_dict=feed_dict) if (step % 500 == 0): print("Minibatch loss at step %d: %f" % (step, l)) print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("Validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
Initialized Minibatch loss at step 0: 282.291931 Minibatch accuracy: 14.1% Validation accuracy: 32.1% Minibatch loss at step 500: 18.090569 Minibatch accuracy: 82.0% Validation accuracy: 79.7% Minibatch loss at step 1000: 15.504422 Minibatch accuracy: 75.0% Validation accuracy: 80.8% Minibatch loss at step 1500: 5.314545 Minibatch accuracy: 87.5% Validation accuracy: 80.6% Minibatch loss at step 2000: 3.442260 Minibatch accuracy: 86.7% Validation accuracy: 81.5% Minibatch loss at step 2500: 2.226066 Minibatch accuracy: 83.6% Validation accuracy: 82.6% Minibatch loss at step 3000: 2.228517 Minibatch accuracy: 83.6% Validation accuracy: 82.5% Test accuracy: 89.6%
8. 正則化
import math batch_size = 128 hidden_layer_size = 1024 # Doubled because half of the results are discarded regularization_beta = 5e-4 graph = tf.Graph() with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed # at run time with a training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. # Hidden layer (RELU magic) weights_hidden_1 = tf.Variable( tf.truncated_normal([image_size * image_size, hidden_layer_size], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_hidden_1 = tf.Variable(tf.zeros([hidden_layer_size])) hidden_1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden_1) + biases_hidden_1) weights_hidden_2 = tf.Variable(tf.truncated_normal([hidden_layer_size, hidden_layer_size], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_hidden_2 = tf.Variable(tf.zeros([hidden_layer_size])) hidden_2 = tf.nn.relu(tf.matmul(hidden_1, weights_hidden_2) + biases_hidden_2) # Output layer weights_output = tf.Variable( tf.truncated_normal([hidden_layer_size, num_labels], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_output = tf.Variable(tf.zeros([num_labels])) # Training computation. logits = tf.matmul(hidden_2, weights_output) + biases_output loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # L2 regularization on hidden and output weights and biases regularizers = (tf.nn.l2_loss(weights_hidden_1) + tf.nn.l2_loss(biases_hidden_1) + tf.nn.l2_loss(weights_hidden_2) + tf.nn.l2_loss(biases_hidden_2) + tf.nn.l2_loss(weights_output) + tf.nn.l2_loss(biases_output)) loss = loss + regularization_beta * regularizers # Optimizer. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. # Creation of hidden layer of RELU for the validation and testing process train_prediction = tf.nn.softmax(logits) hidden_validation_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden_1) + biases_hidden_1) hidden_validation_2 = tf.nn.relu(tf.matmul(hidden_validation_1, weights_hidden_2) + biases_hidden_2) valid_prediction = tf.nn.softmax( tf.matmul(hidden_validation_2, weights_output) + biases_output) hidden_prediction_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden_1) + biases_hidden_1) hidden_prediction_2 = tf.nn.relu(tf.matmul(hidden_prediction_1, weights_hidden_2) + biases_hidden_2) test_prediction = tf.nn.softmax(tf.matmul(hidden_prediction_2, weights_output) + biases_output) num_steps = 3001 def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) with tf.Session(graph=graph) as session: tf.initialize_all_variables().run() print("Initialized") for step in range(num_steps): # Pick an offset within the training data, which has been randomized. # Note: we could use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # Generate a minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # Prepare a dictionary telling the session where to feed the minibatch. # The key of the dictionary is the placeholder node of the graph to be fed, # and the value is the numpy array to feed to it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} _, l, predictions = session.run( [optimizer, loss, train_prediction], feed_dict=feed_dict) if (step % 500 == 0): print("Minibatch loss at step %d: %f" % (step, l)) print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("Validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
Initialized Minibatch loss at step 0: 2.769384 Minibatch accuracy: 8.6% Validation accuracy: 34.8% Minibatch loss at step 500: 0.735681 Minibatch accuracy: 89.1% Validation accuracy: 86.2% Minibatch loss at step 1000: 0.791112 Minibatch accuracy: 85.9% Validation accuracy: 86.9% Minibatch loss at step 1500: 0.523572 Minibatch accuracy: 93.0% Validation accuracy: 88.1% Minibatch loss at step 2000: 0.487140 Minibatch accuracy: 95.3% Validation accuracy: 88.5% Minibatch loss at step 2500: 0.529468 Minibatch accuracy: 89.8% Validation accuracy: 88.4% Minibatch loss at step 3000: 0.531258 Minibatch accuracy: 86.7% Validation accuracy: 88.9% Test accuracy: 94.7%
9. Dropout
import math batch_size = 128 hidden_layer_size = 2048 # Doubled because half of the results are discarded regularization_beta = 5e-4 graph = tf.Graph() with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed # at run time with a training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. # Hidden layer (RELU magic) weights_hidden_1 = tf.Variable( tf.truncated_normal([image_size * image_size, hidden_layer_size], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_hidden_1 = tf.Variable(tf.zeros([hidden_layer_size])) hidden_1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden_1) + biases_hidden_1) weights_hidden_2 = tf.Variable(tf.truncated_normal([hidden_layer_size, hidden_layer_size], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_hidden_2 = tf.Variable(tf.zeros([hidden_layer_size])) hidden_2 = tf.nn.relu(tf.matmul(tf.nn.dropout(hidden_1, 0.5), weights_hidden_2) + biases_hidden_2) # Output layer weights_output = tf.Variable( tf.truncated_normal([hidden_layer_size, num_labels], stddev=1 / math.sqrt(float(image_size * image_size)))) biases_output = tf.Variable(tf.zeros([num_labels])) # Training computation. logits = tf.matmul(tf.nn.dropout(hidden_2, 0.5), weights_output) + biases_output loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # L2 regularization on hidden and output weights and biases regularizers = (tf.nn.l2_loss(weights_hidden_1) + tf.nn.l2_loss(biases_hidden_1) + tf.nn.l2_loss(weights_hidden_2) + tf.nn.l2_loss(biases_hidden_2) + tf.nn.l2_loss(weights_output) + tf.nn.l2_loss(biases_output)) loss = loss + regularization_beta * regularizers # Optimizer. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. # Creation of hidden layer of RELU for the validation and testing process train_prediction = tf.nn.softmax(logits) hidden_validation_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden_1) + biases_hidden_1) hidden_validation_2 = tf.nn.relu(tf.matmul(hidden_validation_1, weights_hidden_2) + biases_hidden_2) valid_prediction = tf.nn.softmax( tf.matmul(hidden_validation_2, weights_output) + biases_output) hidden_prediction_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden_1) + biases_hidden_1) hidden_prediction_2 = tf.nn.relu(tf.matmul(hidden_prediction_1, weights_hidden_2) + biases_hidden_2) test_prediction = tf.nn.softmax(tf.matmul(hidden_prediction_2, weights_output) + biases_output) num_steps = 5001 def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) with tf.Session(graph=graph) as session: tf.initialize_all_variables().run() print("Initialized") for step in range(num_steps): # Pick an offset within the training data, which has been randomized. # Note: we could use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # Generate a minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # Prepare a dictionary telling the session where to feed the minibatch. # The key of the dictionary is the placeholder node of the graph to be fed, # and the value is the numpy array to feed to it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} _, l, predictions = session.run( [optimizer, loss, train_prediction], feed_dict=feed_dict) if (step % 500 == 0): print("Minibatch loss at step %d: %f" % (step, l)) print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("Validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
WARNING:tensorflow:From <ipython-input-12-3684c7218154>:8: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. Initialized Minibatch loss at step 0: 4.059163 Minibatch accuracy: 7.8% Validation accuracy: 31.5% Minibatch loss at step 500: 1.626858 Minibatch accuracy: 86.7% Validation accuracy: 84.8% Minibatch loss at step 1000: 1.492026 Minibatch accuracy: 82.0% Validation accuracy: 85.8% Minibatch loss at step 1500: 1.139689 Minibatch accuracy: 92.2% Validation accuracy: 87.1% Minibatch loss at step 2000: 0.970064 Minibatch accuracy: 93.0% Validation accuracy: 87.1% Minibatch loss at step 2500: 0.963178 Minibatch accuracy: 87.5% Validation accuracy: 87.6% Minibatch loss at step 3000: 0.870884 Minibatch accuracy: 87.5% Validation accuracy: 87.6% Minibatch loss at step 3500: 0.898399 Minibatch accuracy: 85.2% Validation accuracy: 87.7% Minibatch loss at step 4000: 0.737084 Minibatch accuracy: 91.4% Validation accuracy: 88.0% Minibatch loss at step 4500: 0.646125 Minibatch accuracy: 88.3% Validation accuracy: 87.7% Minibatch loss at step 5000: 0.685591 Minibatch accuracy: 88.3% Validation accuracy: 88.6% Test accuracy: 94.4%
最后總結一下各種算法的訓練表現,可以看出使用正則化和Dropout后訓練效果明顯變好,最后趨近於95%的准確率了。