From c3b1b58dd3a4867164d10e6fac499c4380628b37 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Fri, 28 Jul 2023 07:43:13 -0700 Subject: [PATCH 1/4] BUG: Fix incorrect variable in computing eval metrics. --- content/tutorial-deep-learning-on-mnist.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index 63dbdbce..7403213b 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -579,7 +579,7 @@ y_test_error = [ ] x_test_error = range(1, len(store_test_loss) + 1) y_test_accuracy = [ - store_training_accurate_pred[i] / float(len(training_images)) + store_test_accurate_pred[i] / float(len(test_images)) for i in range(len(store_training_accurate_pred)) ] x_test_accuracy = range(1, len(store_test_accurate_pred) + 1) From c3073c15aa632ce966cf1bce1f922658b0555174 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Fri, 28 Jul 2023 07:52:36 -0700 Subject: [PATCH 2/4] ENH: Replace list comps with vectorization. --- content/tutorial-deep-learning-on-mnist.md | 33 +++++++--------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index 7403213b..2479cce9 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -561,38 +561,25 @@ The training process may take many minutes, depending on a number of factors, su After executing the cell above, you can visualize the training and test set errors and accuracy for an instance of this training process. ```{code-cell} +epoch_range = np.arange(epochs) + 1 # Starting from 1 + # The training set metrics. -y_training_error = [ - store_training_loss[i] / float(len(training_images)) - for i in range(len(store_training_loss)) -] -x_training_error = range(1, len(store_training_loss) + 1) -y_training_accuracy = [ - store_training_accurate_pred[i] / float(len(training_images)) - for i in range(len(store_training_accurate_pred)) -] -x_training_accuracy = range(1, len(store_training_accurate_pred) + 1) +y_training_error = np.asarray(store_training_loss) / len(training_images) +y_training_accuracy = np.asarray(store_training_accurate_pred) / len(training_images) # The test set metrics. -y_test_error = [ - store_test_loss[i] / float(len(test_images)) for i in range(len(store_test_loss)) -] -x_test_error = range(1, len(store_test_loss) + 1) -y_test_accuracy = [ - store_test_accurate_pred[i] / float(len(test_images)) - for i in range(len(store_training_accurate_pred)) -] -x_test_accuracy = range(1, len(store_test_accurate_pred) + 1) +y_test_error = np.asarray(store_test_loss) / len(test_images) +y_test_accuracy = np.asarray(store_test_accurate_pred) / len(test_images) # Display the plots. fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5)) axes[0].set_title("Training set error, accuracy") -axes[0].plot(x_training_accuracy, y_training_accuracy, label="Training set accuracy") -axes[0].plot(x_training_error, y_training_error, label="Training set error") +axes[0].plot(epoch_range, y_training_accuracy, label="Training set accuracy") +axes[0].plot(epoch_range, y_training_error, label="Training set error") axes[0].set_xlabel("Epochs") axes[1].set_title("Test set error, accuracy") -axes[1].plot(x_test_accuracy, y_test_accuracy, label="Test set accuracy") -axes[1].plot(x_test_error, y_test_error, label="Test set error") +axes[1].plot(epoch_range, y_test_accuracy, label="Test set accuracy") +axes[1].plot(epoch_range, y_test_error, label="Test set error") axes[1].set_xlabel("Epochs") plt.show() ``` From b27cfd6d6353eaedb4abebcdcd400a6cca663467 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Fri, 28 Jul 2023 08:04:11 -0700 Subject: [PATCH 3/4] ENH: Use dicts and condense plotting. --- content/tutorial-deep-learning-on-mnist.md | 31 +++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index 2479cce9..f71fecd2 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -564,23 +564,28 @@ After executing the cell above, you can visualize the training and test set erro epoch_range = np.arange(epochs) + 1 # Starting from 1 # The training set metrics. -y_training_error = np.asarray(store_training_loss) / len(training_images) -y_training_accuracy = np.asarray(store_training_accurate_pred) / len(training_images) +training_metrics = { + "accuracy": np.asarray(store_training_accurate_pred) / len(training_images), + "error": np.asarray(store_training_loss) / len(training_images), +} # The test set metrics. -y_test_error = np.asarray(store_test_loss) / len(test_images) -y_test_accuracy = np.asarray(store_test_accurate_pred) / len(test_images) +test_metrics = { + "accuracy": np.asarray(store_test_accurate_pred) / len(test_images), + "error": np.asarray(store_test_loss) / len(test_images), +} # Display the plots. -fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5)) -axes[0].set_title("Training set error, accuracy") -axes[0].plot(epoch_range, y_training_accuracy, label="Training set accuracy") -axes[0].plot(epoch_range, y_training_error, label="Training set error") -axes[0].set_xlabel("Epochs") -axes[1].set_title("Test set error, accuracy") -axes[1].plot(epoch_range, y_test_accuracy, label="Test set accuracy") -axes[1].plot(epoch_range, y_test_error, label="Test set error") -axes[1].set_xlabel("Epochs") +fig, axes = plt.subplots(1, 2, figsize=(15, 5)) +for ax, metrics, ttl in zip( + axes, (training_metrics, test_metrics), ("Training set", "Test set") +): + # Plot the metrics + for metric, values in metrics.items(): + ax.plot(epoch_range, values, label=metric.capitalize()) + ax.set_title(ttl) + ax.set_xlabel("Epochs") + ax.legend() plt.show() ``` From 98db273e0d0bddca0f6fa16b956588df77d1b144 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Fri, 28 Jul 2023 10:08:58 -0700 Subject: [PATCH 4/4] Update content/tutorial-deep-learning-on-mnist.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Brigitta Sipőcz --- content/tutorial-deep-learning-on-mnist.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/tutorial-deep-learning-on-mnist.md b/content/tutorial-deep-learning-on-mnist.md index f71fecd2..a41438c5 100644 --- a/content/tutorial-deep-learning-on-mnist.md +++ b/content/tutorial-deep-learning-on-mnist.md @@ -576,14 +576,14 @@ test_metrics = { } # Display the plots. -fig, axes = plt.subplots(1, 2, figsize=(15, 5)) -for ax, metrics, ttl in zip( +fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5)) +for ax, metrics, title in zip( axes, (training_metrics, test_metrics), ("Training set", "Test set") ): # Plot the metrics for metric, values in metrics.items(): ax.plot(epoch_range, values, label=metric.capitalize()) - ax.set_title(ttl) + ax.set_title(title) ax.set_xlabel("Epochs") ax.legend() plt.show()