Skip to content

Commit 22cc58c

Browse files
committed
Vectorize model evaluation in mnist tutorial.
1 parent cabbdd1 commit 22cc58c

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

content/tutorial-deep-learning-on-mnist.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ weights_2 = 0.2 * np.random.random((hidden_size, num_labels)) - 0.1
396396
```
397397

398398
**5.** Set up the neural network's learning experiment with a training loop and start the training process.
399+
Note that the model is evaluated at each epoch by running the model on test
400+
set, thus the model improvement can be tracked vs. epoch.
399401

400402
Start the training process:
401403

@@ -410,6 +412,11 @@ store_test_accurate_pred = []
410412
# This is a training loop.
411413
# Run the learning experiment for a defined number of epochs (iterations).
412414
for j in range(epochs):
415+
416+
#################
417+
# Training step #
418+
#################
419+
413420
# Set the initial loss/error and the number of accurate predictions to zero.
414421
training_loss = 0.0
415422
training_accurate_predictions = 0
@@ -458,26 +465,26 @@ for j in range(epochs):
458465
store_training_loss.append(training_loss)
459466
store_training_accurate_pred.append(training_accurate_predictions)
460467
461-
# Evaluate on the test set:
462-
# 1. Set the initial error and the number of accurate predictions to zero.
463-
test_loss = 0.0
464-
test_accurate_predictions = 0
465-
466-
# 2. Start testing the model by evaluating on the test image dataset.
467-
for i in range(len(test_images)):
468-
# 1. Pass the test images through the input layer.
469-
layer_0 = test_images[i]
470-
# 2. Compute the weighted sum of the test image inputs in and
471-
# pass the hidden layer's output through ReLU.
472-
layer_1 = relu(np.dot(layer_0, weights_1))
473-
# 3. Compute the weighted sum of the hidden layer's inputs.
474-
# Produce a 10-dimensional vector with 10 scores.
475-
layer_2 = np.dot(layer_1, weights_2)
468+
################
469+
# Testing step #
470+
################
471+
472+
# Evaluate model performance on the test set at each epoch.
473+
474+
# Unlike the training step, the weights are not modified for each image
475+
# (or batch). Therefore the model can be applied to the test images in a
476+
# vectorized manner, eliminating the need to loop over each image
477+
# individually:
478+
479+
results = relu(test_images @ weights_1) @ weights_2
480+
481+
# Measure the error between the actual label (truth) and prediction values.
482+
test_loss = np.sum((test_labels - results)**2)
476483
477-
# 4. Measure the error between the actual label (truth) and prediction values.
478-
test_loss += np.sum((test_labels[i] - layer_2) ** 2)
479-
# 5. Increment the accurate prediction count.
480-
test_accurate_predictions += int(np.argmax(layer_2) == np.argmax(test_labels[i]))
484+
# Measure prediction accuracy on test set
485+
test_accurate_predictions = np.sum(
486+
np.argmax(results, axis=1) == np.argmax(test_labels, axis=1)
487+
)
481488
482489
# Store test set losses and accurate predictions.
483490
store_test_loss.append(test_loss)

0 commit comments

Comments
 (0)