Skip to content

Commit 3e94ca9

Browse files
committed
Vectorize model evaluation in mnist tutorial.
1 parent 9046071 commit 3e94ca9

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

Diff for: content/tutorial-deep-learning-on-mnist.md

+26-19
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ weights_2 = 0.2 * np.random.random((hidden_size, num_labels)) - 0.1
405405
```
406406

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

409411
Start the training process:
410412

@@ -419,6 +421,11 @@ store_test_accurate_pred = []
419421
# This is a training loop.
420422
# Run the learning experiment for a defined number of epochs (iterations).
421423
for j in range(epochs):
424+
425+
#################
426+
# Training step #
427+
#################
428+
422429
# Set the initial loss/error and the number of accurate predictions to zero.
423430
training_loss = 0.0
424431
training_accurate_predictions = 0
@@ -467,26 +474,26 @@ for j in range(epochs):
467474
store_training_loss.append(training_loss)
468475
store_training_accurate_pred.append(training_accurate_predictions)
469476
470-
# Evaluate on the test set:
471-
# 1. Set the initial error and the number of accurate predictions to zero.
472-
test_loss = 0.0
473-
test_accurate_predictions = 0
474-
475-
# 2. Start testing the model by evaluating on the test image dataset.
476-
for i in range(len(test_images)):
477-
# 1. Pass the test images through the input layer.
478-
layer_0 = test_images[i]
479-
# 2. Compute the weighted sum of the test image inputs in and
480-
# pass the hidden layer's output through ReLU.
481-
layer_1 = relu(np.dot(layer_0, weights_1))
482-
# 3. Compute the weighted sum of the hidden layer's inputs.
483-
# Produce a 10-dimensional vector with 10 scores.
484-
layer_2 = np.dot(layer_1, weights_2)
477+
################
478+
# Testing step #
479+
################
480+
481+
# Evaluate model performance on the test set at each epoch.
482+
483+
# Unlike the training step, the weights are not modified for each image
484+
# (or batch). Therefore the model can be applied to the test images in a
485+
# vectorized manner, eliminating the need to loop over each image
486+
# individually:
487+
488+
results = relu(test_images @ weights_1) @ weights_2
489+
490+
# Measure the error between the actual label (truth) and prediction values.
491+
test_loss = np.sum((test_labels - results)**2)
485492
486-
# 4. Measure the error between the actual label (truth) and prediction values.
487-
test_loss += np.sum((test_labels[i] - layer_2) ** 2)
488-
# 5. Increment the accurate prediction count.
489-
test_accurate_predictions += int(np.argmax(layer_2) == np.argmax(test_labels[i]))
493+
# Measure prediction accuracy on test set
494+
test_accurate_predictions = np.sum(
495+
np.argmax(results, axis=1) == np.argmax(test_labels, axis=1)
496+
)
490497
491498
# Store test set losses and accurate predictions.
492499
store_test_loss.append(test_loss)

0 commit comments

Comments
 (0)