microsoft/AI-For-Beginners
Publicmirrored fromhttps://github.com/microsoft/AI-For-BeginnersAvailable
lessons/3-NeuralNetworks/04-OwnFramework/lab/MyFW_MNIST.ipynb
170lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "# MNIST Digit Classification with our own Framework\n", |
| 8 | "\n", |
| 9 | "Lab Assignment from [AI for Beginners Curriculum](https://github.com/microsoft/ai-for-beginners).\n", |
| 10 | "\n", |
| 11 | "### Reading the Dataset\n", |
| 12 | "\n", |
| 13 | "This code download the dataset from the repository on the internet. You can also manually copy the dataset from `/data` directory of AI Curriculum repo." |
| 14 | ] |
| 15 | }, |
| 16 | { |
| 17 | "cell_type": "code", |
| 18 | "execution_count": 4, |
| 19 | "metadata": { |
| 20 | "tags": [] |
| 21 | }, |
| 22 | "outputs": [ |
| 23 | { |
| 24 | "name": "stderr", |
| 25 | "output_type": "stream", |
| 26 | "text": [ |
| 27 | " % Total % Received % Xferd Average Speed Time Time Time Current\n", |
| 28 | " Dload Upload Total Spent Left Speed\n", |
| 29 | "\n", |
| 30 | " 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n", |
| 31 | "100 9.9M 100 9.9M 0 0 9.9M 0 0:00:01 --:--:-- 0:00:01 15.8M\n" |
| 32 | ] |
| 33 | } |
| 34 | ], |
| 35 | "source": [ |
| 36 | "!rm *.pkl\n", |
| 37 | "!wget https://raw.githubusercontent.com/microsoft/AI-For-Beginners/main/data/mnist.pkl.gz\n", |
| 38 | "!gzip -d mnist.pkl.gz" |
| 39 | ] |
| 40 | }, |
| 41 | { |
| 42 | "cell_type": "code", |
| 43 | "execution_count": 3, |
| 44 | "metadata": {}, |
| 45 | "outputs": [], |
| 46 | "source": [ |
| 47 | "import pickle\n", |
| 48 | "with open('mnist.pkl','rb') as f:\n", |
| 49 | " MNIST = pickle.load(f)" |
| 50 | ] |
| 51 | }, |
| 52 | { |
| 53 | "cell_type": "code", |
| 54 | "execution_count": 4, |
| 55 | "metadata": {}, |
| 56 | "outputs": [], |
| 57 | "source": [ |
| 58 | "labels = MNIST['Train']['Labels']\n", |
| 59 | "data = MNIST['Train']['Features']" |
| 60 | ] |
| 61 | }, |
| 62 | { |
| 63 | "cell_type": "markdown", |
| 64 | "metadata": {}, |
| 65 | "source": [ |
| 66 | "Let's see what is the shape of data that we have:" |
| 67 | ] |
| 68 | }, |
| 69 | { |
| 70 | "cell_type": "code", |
| 71 | "execution_count": 5, |
| 72 | "metadata": {}, |
| 73 | "outputs": [ |
| 74 | { |
| 75 | "data": { |
| 76 | "text/plain": [ |
| 77 | "(42000, 784)" |
| 78 | ] |
| 79 | }, |
| 80 | "execution_count": 5, |
| 81 | "metadata": {}, |
| 82 | "output_type": "execute_result" |
| 83 | } |
| 84 | ], |
| 85 | "source": [ |
| 86 | "data.shape" |
| 87 | ] |
| 88 | }, |
| 89 | { |
| 90 | "cell_type": "markdown", |
| 91 | "metadata": {}, |
| 92 | "source": [ |
| 93 | "### Splitting the Data\n", |
| 94 | "\n", |
| 95 | "We will use Scikit Learn to split the data between training and test dataset:" |
| 96 | ] |
| 97 | }, |
| 98 | { |
| 99 | "cell_type": "code", |
| 100 | "execution_count": 6, |
| 101 | "metadata": {}, |
| 102 | "outputs": [ |
| 103 | { |
| 104 | "name": "stdout", |
| 105 | "output_type": "stream", |
| 106 | "text": [ |
| 107 | "Train samples: 33600, test samples: 8400\n" |
| 108 | ] |
| 109 | } |
| 110 | ], |
| 111 | "source": [ |
| 112 | "from sklearn.model_selection import train_test_split\n", |
| 113 | "\n", |
| 114 | "features_train, features_test, labels_train, labels_test = train_test_split(data,labels,test_size=0.2)\n", |
| 115 | "\n", |
| 116 | "print(f\"Train samples: {len(features_train)}, test samples: {len(features_test)}\")" |
| 117 | ] |
| 118 | }, |
| 119 | { |
| 120 | "cell_type": "markdown", |
| 121 | "metadata": {}, |
| 122 | "source": [ |
| 123 | "### Instructions\n", |
| 124 | "\n", |
| 125 | "1. Take the framework code from the lesson and paste it into this notebook, or (even better) into a separate Python module\n", |
| 126 | "1. Define and train one-layered perceptron, observing training and validation accuracy during training\n", |
| 127 | "1. Try to understand if overfitting took place, and adjust layer parameters to improve accuracy\n", |
| 128 | "1. Repeat previous steps for 2- and 3-layered perceptrons. Try to experiment with different activation functions between layers.\n", |
| 129 | "1. Try to answer the following questions:\n", |
| 130 | " - Does the inter-layer activation function affect network performance?\n", |
| 131 | " - Do we need 2- or 3-layered network for this task?\n", |
| 132 | " - Did you experience any problems training the network? Especially as the number of layers increased.\n", |
| 133 | " - How do weights of the network behave during training? You may plot max abs value of weights vs. epoch to understand the relation." |
| 134 | ] |
| 135 | }, |
| 136 | { |
| 137 | "cell_type": "code", |
| 138 | "execution_count": null, |
| 139 | "metadata": {}, |
| 140 | "outputs": [], |
| 141 | "source": [] |
| 142 | } |
| 143 | ], |
| 144 | "metadata": { |
| 145 | "kernelspec": { |
| 146 | "display_name": "Python 3.7.4 64-bit (conda)", |
| 147 | "metadata": { |
| 148 | "interpreter": { |
| 149 | "hash": "86193a1ab0ba47eac1c69c1756090baa3b420b3eea7d4aafab8b85f8b312f0c5" |
| 150 | } |
| 151 | }, |
| 152 | "name": "python3" |
| 153 | }, |
| 154 | "language_info": { |
| 155 | "codemirror_mode": { |
| 156 | "name": "ipython", |
| 157 | "version": 3 |
| 158 | }, |
| 159 | "file_extension": ".py", |
| 160 | "mimetype": "text/x-python", |
| 161 | "name": "python", |
| 162 | "nbconvert_exporter": "python", |
| 163 | "pygments_lexer": "ipython3", |
| 164 | "version": "3.9.5" |
| 165 | }, |
| 166 | "orig_nbformat": 2 |
| 167 | }, |
| 168 | "nbformat": 4, |
| 169 | "nbformat_minor": 2 |
| 170 | } |
| 171 | |