|
265 | 265 | "cell_type": "markdown",
|
266 | 266 | "metadata": {},
|
267 | 267 | "source": [
|
268 |
| - "### **3) Backpropagation**" |
| 268 | + "## **3) Backpropagation**" |
269 | 269 | ]
|
270 | 270 | },
|
271 | 271 | {
|
|
527 | 527 | "cell_type": "markdown",
|
528 | 528 | "metadata": {},
|
529 | 529 | "source": [
|
530 |
| - "### 4) Improvements or Optimization (Gradient Descent)" |
| 530 | + "## **4) Improvements or Optimization (Gradient Descent)**" |
531 | 531 | ]
|
532 | 532 | },
|
533 | 533 | {
|
|
571 | 571 | "source": [
|
572 | 572 | "<hr>\n",
|
573 | 573 | "\n",
|
574 |
| - "## Lets build a Neural Network" |
| 574 | + "## Lets build a Neural Network\n", |
| 575 | + "\n", |
| 576 | + "A simple neural network with one hidden layer." |
575 | 577 | ]
|
576 | 578 | },
|
577 | 579 | {
|
578 | 580 | "cell_type": "code",
|
579 |
| - "execution_count": 1, |
| 581 | + "execution_count": 2, |
580 | 582 | "metadata": {},
|
581 | 583 | "outputs": [],
|
582 | 584 | "source": [
|
|
746 | 748 | },
|
747 | 749 | {
|
748 | 750 | "cell_type": "code",
|
749 |
| - "execution_count": 2, |
| 751 | + "execution_count": 3, |
750 | 752 | "metadata": {},
|
751 | 753 | "outputs": [],
|
752 | 754 | "source": [
|
|
833 | 835 | },
|
834 | 836 | {
|
835 | 837 | "cell_type": "code",
|
836 |
| - "execution_count": 14, |
| 838 | + "execution_count": 4, |
837 | 839 | "metadata": {},
|
838 | 840 | "outputs": [
|
839 | 841 | {
|
840 | 842 | "name": "stdout",
|
841 | 843 | "output_type": "stream",
|
842 | 844 | "text": [
|
843 |
| - "Accuracy: 94.17%\n" |
| 845 | + "Accuracy: 93.06%\n" |
844 | 846 | ]
|
845 | 847 | },
|
846 | 848 | {
|
|
909 | 911 | "print(f\"Accuracy: {accuracy:.2%}\")"
|
910 | 912 | ]
|
911 | 913 | },
|
| 914 | + { |
| 915 | + "cell_type": "markdown", |
| 916 | + "metadata": {}, |
| 917 | + "source": [ |
| 918 | + "### Predict Single Digit" |
| 919 | + ] |
| 920 | + }, |
| 921 | + { |
| 922 | + "cell_type": "code", |
| 923 | + "execution_count": 71, |
| 924 | + "metadata": {}, |
| 925 | + "outputs": [ |
| 926 | + { |
| 927 | + "name": "stdout", |
| 928 | + "output_type": "stream", |
| 929 | + "text": [ |
| 930 | + "Input Single Digit 8x8 image vector : \n", |
| 931 | + "+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+\n", |
| 932 | + "| Pix1 | Pix2 | Pix3 | Pix4 | Pix5 | Pix6 | Pix7 | Pix8 |\n", |
| 933 | + "+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+\n", |
| 934 | + "| 0.00 | 0.00 | 0.12 | 0.81 | 0.50 | 0.00 | 0.00 | 0.00 |\n", |
| 935 | + "| 0.00 | 0.00 | 0.38 | 1.00 | 1.00 | 0.38 | 0.00 | 0.00 |\n", |
| 936 | + "| 0.00 | 0.00 | 0.31 | 0.94 | 0.81 | 0.69 | 0.00 | 0.00 |\n", |
| 937 | + "| 0.00 | 0.00 | 0.00 | 0.44 | 1.00 | 0.94 | 0.00 | 0.00 |\n", |
| 938 | + "| 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.88 | 0.21 | 0.00 |\n", |
| 939 | + "| 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.44 | 0.69 | 0.00 |\n", |
| 940 | + "| 0.00 | 0.00 | 0.00 | 0.19 | 0.25 | 0.25 | 1.00 | 0.15 |\n", |
| 941 | + "| 0.00 | 0.00 | 0.12 | 0.94 | 0.81 | 0.88 | 0.81 | 0.12 |\n", |
| 942 | + "+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+::::::::::::+\n", |
| 943 | + "Predicted Value : 9\n", |
| 944 | + "Actual Digit Image with Label :\n" |
| 945 | + ] |
| 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oKbRaGBrawtDQ0PI5/OYnp7G6uoqbm9v/zJv85zLwsKCMWNza5nNwGdyctKYcUXqA+T4+DguLi7QbrcRBAGur6/x+fmJsbExF/VlCmfn2X19fRgYGMDb2xt837d6KuxXI/VuxPd9JEmCiYkJPD4+Ym9vD5VKBZubmy7qyxSpO7vVamFnZweVSgXr6+uYm5uD7/vo7u52UV+mSN3Z1WoV1WrVRS2ZR9dGiEg2EavdyI8nQcIwTP2DNhfabZaojOPYmOl0Ok6+x1Rzu90GYFG31qnhrVNj9QCTVmD6nsRyBSYr2cINOkASkWwikk1EsolINhHJJiLZRH4HEPMVOAcglpkAAAAASUVORK5CYII=", |
| 950 | + "text/plain": [ |
| 951 | + "<Figure size 600x600 with 1 Axes>" |
| 952 | + ] |
| 953 | + }, |
| 954 | + "metadata": {}, |
| 955 | + "output_type": "display_data" |
| 956 | + } |
| 957 | + ], |
| 958 | + "source": [ |
| 959 | + "import numpy as np\n", |
| 960 | + "from dataclasses import dataclass\n", |
| 961 | + "import pandas as pd\n", |
| 962 | + "from tabulate import tabulate\n", |
| 963 | + "from prettytable import PrettyTable\n", |
| 964 | + "\n", |
| 965 | + "def show_input_vector(vector):\n", |
| 966 | + " reshaped_X = vector.reshape(8, 8)\n", |
| 967 | + " df = pd.DataFrame(reshaped_X)\n", |
| 968 | + " fixed_width_df = df.map(lambda x: f\"{x:5.2f}\")\n", |
| 969 | + "\n", |
| 970 | + " t = PrettyTable(\n", |
| 971 | + " ['Pix1', 'Pix2', 'Pix3', 'Pix4', 'Pix5', 'Pix6', 'Pix7', 'Pix8'], \n", |
| 972 | + " align='c', \n", |
| 973 | + " horizontal_char=':',\n", |
| 974 | + " max_width=10, \n", |
| 975 | + " min_width=10\n", |
| 976 | + " ) \n", |
| 977 | + " t.add_rows(fixed_width_df.values.tolist())\n", |
| 978 | + " print(t)\n", |
| 979 | + "\n", |
| 980 | + "@dataclass\n", |
| 981 | + "class SingleDigit:\n", |
| 982 | + " images: np.array\n", |
| 983 | + " target: np.array\n", |
| 984 | + "\n", |
| 985 | + "test_idx = 31\n", |
| 986 | + "X_single_input = X[test_idx]\n", |
| 987 | + "y_single_label = y[test_idx]\n", |
| 988 | + "singleDigit = SingleDigit(\n", |
| 989 | + " # Shape of digits.images is (samples_n, 8, 8) where 8x8 are pixels\n", |
| 990 | + " images = np.array([digits.images[test_idx]]),\n", |
| 991 | + " target = np.array([digits.target[test_idx]])\n", |
| 992 | + ")\n", |
| 993 | + " \n", |
| 994 | + "predict = np.argmax(nn.forward(X_single_input), axis=1)\n", |
| 995 | + "\n", |
| 996 | + "print(f\"Input Single Digit 8x8 image vector : \")\n", |
| 997 | + "show_input_vector(X_single_input)\n", |
| 998 | + "print(f\"Predicted Value : \", predict[0])\n", |
| 999 | + "print(f\"Actual Digit Image with Label :\")\n", |
| 1000 | + "show_digits(singleDigit, 1)" |
| 1001 | + ] |
| 1002 | + }, |
912 | 1003 | {
|
913 | 1004 | "cell_type": "markdown",
|
914 | 1005 | "metadata": {},
|
|
0 commit comments