From 3c8dab14e6ffe1cfbb6215e535afb17d3cff4bed Mon Sep 17 00:00:00 2001 From: muddlebee Date: Sat, 17 Aug 2024 03:48:56 +0530 Subject: [PATCH 1/3] fix(8.1) : error in Chap08\8_1_MNIST_1D_Performance.ipynb --- .../Chap08/8_1_MNIST_1D_Performance.ipynb | 157 ++++++++++-------- 1 file changed, 85 insertions(+), 72 deletions(-) diff --git a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb index fedcf24..1c01370 100644 --- a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb +++ b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb @@ -1,28 +1,10 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "gpuType": "T4", - "authorship_tag": "ABX9TyOuKMUcKfOIhIL2qTX9jJCy", - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - }, - "accelerator": "GPU" - }, "cells": [ { "cell_type": "markdown", "metadata": { - "id": "view-in-github", - "colab_type": "text" + "colab_type": "text", + "id": "view-in-github" }, "source": [ "\"Open" @@ -30,6 +12,9 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "L6chybAVFJW2" + }, "source": [ "# **Notebook 8.1: MNIST_1D_Performance**\n", "\n", @@ -38,25 +23,27 @@ "Work through the cells below, running each cell in turn. In various places you will see the words \"TO DO\". Follow the instructions at these places and make predictions about what is going to happen or write code to complete the functions.\n", "\n", "Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions." - ], - "metadata": { - "id": "L6chybAVFJW2" - } + ] }, { "cell_type": "code", - "source": [ - "# Run this if you're in a Colab to install MNIST 1D repository\n", - "%pip install git+https://github.com/greydanus/mnist1d" - ], + "execution_count": null, "metadata": { "id": "ifVjS4cTOqKz" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "# Run this if you're in a Colab to install MNIST 1D repository\n", + "%pip install git+https://github.com/greydanus/mnist1d" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "qyE7G1StPIqO" + }, + "outputs": [], "source": [ "import torch, torch.nn as nn\n", "from torch.utils.data import TensorDataset, DataLoader\n", @@ -64,26 +51,30 @@ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import mnist1d" - ], - "metadata": { - "id": "qyE7G1StPIqO" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", - "source": [ - "Let's generate a training and test dataset using the MNIST1D code. The dataset gets saved as a .pkl file so it doesn't have to be regenerated each time." - ], "metadata": { "id": "F7LNq72SP6jO" - } + }, + "source": [ + "Let's generate a training and test dataset using the MNIST1D code. The dataset gets saved as a .pkl file so it doesn't have to be regenerated each time." + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "YLxf7dJfPaqw" + }, + "outputs": [], "source": [ - "!mkdir ./sample_data\n", + "import os\n", + "\n", + "# Create directory in a cross-platform way\n", + "os.makedirs('./sample_data', exist_ok=True)\n", + "\n", "\n", "args = mnist1d.data.get_dataset_args()\n", "data = mnist1d.data.get_dataset(args, path='./sample_data/mnist1d_data.pkl', download=False, regenerate=False)\n", @@ -93,15 +84,15 @@ "print(\"Examples in training set: {}\".format(len(data['y'])))\n", "print(\"Examples in test set: {}\".format(len(data['y_test'])))\n", "print(\"Length of each example: {}\".format(data['x'].shape[-1]))" - ], - "metadata": { - "id": "YLxf7dJfPaqw" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "FxaB5vc0uevl" + }, + "outputs": [], "source": [ "D_i = 40 # Input dimensions\n", "D_k = 100 # Hidden dimensions\n", @@ -122,15 +113,24 @@ "\n", "# Call the function you just defined\n", "model.apply(weights_init)\n" - ], - "metadata": { - "id": "FxaB5vc0uevl" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "_rX6N3VyyQTY" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 24, train loss 1.584953, train error 62.60, test loss 1.665801, test error 67.90\n", + "Epoch 25, train loss 1.586464, train error 63.05, test loss 1.666717, test error 68.00\n" + ] + } + ], "source": [ "# choose cross entropy loss function (equation 5.24)\n", "loss_function = torch.nn.CrossEntropyLoss()\n", @@ -139,9 +139,9 @@ "# object that decreases learning rate by half every 10 epochs\n", "scheduler = StepLR(optimizer, step_size=10, gamma=0.5)\n", "x_train = torch.tensor(data['x'].astype('float32'))\n", - "y_train = torch.tensor(data['y'].transpose().astype('long'))\n", + "y_train = torch.tensor(data['y'].transpose().astype('int64'))\n", "x_test= torch.tensor(data['x_test'].astype('float32'))\n", - "y_test = torch.tensor(data['y_test'].astype('long'))\n", + "y_test = torch.tensor(data['y_test'].astype('int64'))\n", "\n", "# load the data into a class that creates the batches\n", "data_loader = DataLoader(TensorDataset(x_train,y_train), batch_size=100, shuffle=True, worker_init_fn=np.random.seed(1))\n", @@ -186,15 +186,15 @@ "\n", " # tell scheduler to consider updating learning rate\n", " scheduler.step()" - ], - "metadata": { - "id": "_rX6N3VyyQTY" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "yI-l6kA_EH9G" + }, + "outputs": [], "source": [ "# Plot the results\n", "fig, ax = plt.subplots()\n", @@ -215,25 +215,38 @@ "ax.set_title('Train loss %3.2f, Test loss %3.2f'%(losses_train[-1],losses_test[-1]))\n", "ax.legend()\n", "plt.show()" - ], - "metadata": { - "id": "yI-l6kA_EH9G" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": { + "id": "q-yT6re6GZS4" + }, "source": [ "**TO DO**\n", "\n", "Play with the model -- try changing the number of layers, hidden units, learning rate, batch size, momentum or anything else you like. See if you can improve the test results.\n", "\n", "Is it a good idea to optimize the hyperparameters in this way? Will the final result be a good estimate of the true test performance?" - ], - "metadata": { - "id": "q-yT6re6GZS4" - } + ] } - ] + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "authorship_tag": "ABX9TyOuKMUcKfOIhIL2qTX9jJCy", + "gpuType": "T4", + "include_colab_link": true, + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 } From b423a67855de0874bd823cb5301d8351cd12de13 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Sat, 17 Aug 2024 03:50:15 +0530 Subject: [PATCH 2/3] fix(8.1) : error in Chap08\8_1_MNIST_1D_Performance.ipynb --- Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb index 1c01370..69b4f91 100644 --- a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb +++ b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb @@ -117,20 +117,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "id": "_rX6N3VyyQTY" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 24, train loss 1.584953, train error 62.60, test loss 1.665801, test error 67.90\n", - "Epoch 25, train loss 1.586464, train error 63.05, test loss 1.666717, test error 68.00\n" - ] - } - ], + "outputs": [], "source": [ "# choose cross entropy loss function (equation 5.24)\n", "loss_function = torch.nn.CrossEntropyLoss()\n", From ccedbb72e72121ef055ef0ce98fed99eb10852f0 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Sat, 17 Aug 2024 19:20:02 +0530 Subject: [PATCH 3/3] fix(8.1) : error in Chap08\8_1_MNIST_1D_Performance.ipynb --- Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb index 69b4f91..a6b8208 100644 --- a/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb +++ b/Notebooks/Chap08/8_1_MNIST_1D_Performance.ipynb @@ -70,14 +70,8 @@ }, "outputs": [], "source": [ - "import os\n", - "\n", - "# Create directory in a cross-platform way\n", - "os.makedirs('./sample_data', exist_ok=True)\n", - "\n", - "\n", "args = mnist1d.data.get_dataset_args()\n", - "data = mnist1d.data.get_dataset(args, path='./sample_data/mnist1d_data.pkl', download=False, regenerate=False)\n", + "data = mnist1d.data.get_dataset(args, path='./mnist1d_data.pkl', download=False, regenerate=False)\n", "\n", "# The training and test input and outputs are in\n", "# data['x'], data['y'], data['x_test'], and data['y_test']\n",