From 3e94ca90daf54159da9f766064e16de01076fc8e Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Thu, 11 Mar 2021 21:20:23 -0800 Subject: [PATCH 1/2] Vectorize model evaluation in mnist tutorial. --- content/tutorial-deep-learning-on-mnist.md | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index d7070e44..b49d9b53 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -405,6 +405,8 @@ weights_2 = 0.2 * np.random.random((hidden_size, num_labels)) - 0.1 ``` **5.** Set up the neural network's learning experiment with a training loop and start the training process. +Note that the model is evaluated at each epoch by running the model on test +set, thus the model improvement can be tracked vs. epoch. Start the training process: @@ -419,6 +421,11 @@ store_test_accurate_pred = [] # This is a training loop. # Run the learning experiment for a defined number of epochs (iterations). for j in range(epochs): + + ################# + # Training step # + ################# + # Set the initial loss/error and the number of accurate predictions to zero. training_loss = 0.0 training_accurate_predictions = 0 @@ -467,26 +474,26 @@ for j in range(epochs): store_training_loss.append(training_loss) store_training_accurate_pred.append(training_accurate_predictions) - # Evaluate on the test set: - # 1. Set the initial error and the number of accurate predictions to zero. - test_loss = 0.0 - test_accurate_predictions = 0 - - # 2. Start testing the model by evaluating on the test image dataset. - for i in range(len(test_images)): - # 1. Pass the test images through the input layer. - layer_0 = test_images[i] - # 2. Compute the weighted sum of the test image inputs in and - # pass the hidden layer's output through ReLU. - layer_1 = relu(np.dot(layer_0, weights_1)) - # 3. Compute the weighted sum of the hidden layer's inputs. - # Produce a 10-dimensional vector with 10 scores. - layer_2 = np.dot(layer_1, weights_2) + ################ + # Testing step # + ################ + + # Evaluate model performance on the test set at each epoch. + + # Unlike the training step, the weights are not modified for each image + # (or batch). Therefore the model can be applied to the test images in a + # vectorized manner, eliminating the need to loop over each image + # individually: + + results = relu(test_images @ weights_1) @ weights_2 + + # Measure the error between the actual label (truth) and prediction values. + test_loss = np.sum((test_labels - results)**2) - # 4. Measure the error between the actual label (truth) and prediction values. - test_loss += np.sum((test_labels[i] - layer_2) ** 2) - # 5. Increment the accurate prediction count. - test_accurate_predictions += int(np.argmax(layer_2) == np.argmax(test_labels[i])) + # Measure prediction accuracy on test set + test_accurate_predictions = np.sum( + np.argmax(results, axis=1) == np.argmax(test_labels, axis=1) + ) # Store test set losses and accurate predictions. store_test_loss.append(test_loss) From 4bfe1eb5062c01874d78fe719d2eef301254cb51 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Thu, 11 Mar 2021 21:25:33 -0800 Subject: [PATCH 2/2] Update wording and numbering in code comments. --- content/tutorial-deep-learning-on-mnist.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index b49d9b53..e9479712 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -405,8 +405,8 @@ weights_2 = 0.2 * np.random.random((hidden_size, num_labels)) - 0.1 ``` **5.** Set up the neural network's learning experiment with a training loop and start the training process. -Note that the model is evaluated at each epoch by running the model on test -set, thus the model improvement can be tracked vs. epoch. +Note that the model is evaluated against the test set at each epoch to track +its performance over the training epochs. Start the training process: @@ -474,9 +474,9 @@ for j in range(epochs): store_training_loss.append(training_loss) store_training_accurate_pred.append(training_accurate_predictions) - ################ - # Testing step # - ################ + ################### + # Evaluation step # + ################### # Evaluate model performance on the test set at each epoch. @@ -499,7 +499,7 @@ for j in range(epochs): store_test_loss.append(test_loss) store_test_accurate_pred.append(test_accurate_predictions) - # 3. Display the error and accuracy metrics in the output. + # Summarize error and accuracy metrics at each epoch print("\n" + \ "Epoch: " + str(j) + \ " Training set error:" + str(training_loss/ float(len(training_images)))[0:5] +\