{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:19.650566Z","iopub.execute_input":"2025-02-05T05:46:19.651047Z","iopub.status.idle":"2025-02-05T05:46:20.100391Z","shell.execute_reply.started":"2025-02-05T05:46:19.650997Z","shell.execute_reply":"2025-02-05T05:46:20.098995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:20.102213Z","iopub.execute_input":"2025-02-05T05:46:20.102772Z","iopub.status.idle":"2025-02-05T05:46:23.459191Z","shell.execute_reply.started":"2025-02-05T05:46:20.102739Z","shell.execute_reply":"2025-02-05T05:46:23.458132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()\ndata.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:23.461483Z","iopub.execute_input":"2025-02-05T05:46:23.461864Z","iopub.status.idle":"2025-02-05T05:46:23.468748Z","shell.execute_reply.started":"2025-02-05T05:46:23.461833Z","shell.execute_reply":"2025-02-05T05:46:23.467684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data)\n\ndata_dev = data[0:1000].T\nY_dev = data_dev[0]\nX_dev = data_dev[1:n]\nX_dev = X_dev / 255\n\ndata_train = data[1000:m].T\nY_train = data_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:23.470066Z","iopub.execute_input":"2025-02-05T05:46:23.470323Z","iopub.status.idle":"2025-02-05T05:46:24.281776Z","shell.execute_reply.started":"2025-02-05T05:46:23.470301Z","shell.execute_reply":"2025-02-05T05:46:24.280796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_params():\n    w1 = np.random.rand(10, 784) - 0.5\n    b1 = np.random.rand(10, 1) - 0.5\n    w2 = np.random.rand(10, 10) - 0.5\n    b2 = np.random.rand(10, 1) - 0.5\n\n    return w1, b1, w2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.282648Z","iopub.execute_input":"2025-02-05T05:46:24.282932Z","iopub.status.idle":"2025-02-05T05:46:24.287839Z","shell.execute_reply.started":"2025-02-05T05:46:24.282905Z","shell.execute_reply":"2025-02-05T05:46:24.286713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def ReLU(z):\n    return np.maximum(0, z)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.288734Z","iopub.execute_input":"2025-02-05T05:46:24.289059Z","iopub.status.idle":"2025-02-05T05:46:24.306800Z","shell.execute_reply.started":"2025-02-05T05:46:24.289001Z","shell.execute_reply":"2025-02-05T05:46:24.305840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def softmax(z):\n    return np.exp(z) / sum(np.exp(z))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.307857Z","iopub.execute_input":"2025-02-05T05:46:24.308212Z","iopub.status.idle":"2025-02-05T05:46:24.327478Z","shell.execute_reply.started":"2025-02-05T05:46:24.308185Z","shell.execute_reply":"2025-02-05T05:46:24.326427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def fwd_prop(w1, b1, w2, b2, X):\n    z1 = w1.dot(X) + b1\n    a1 = ReLU(z1)\n\n    z2 = w2.dot(a1) + b2\n    a2 = softmax(z2)\n\n    return z1, a1, z2, a2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.328537Z","iopub.execute_input":"2025-02-05T05:46:24.328809Z","iopub.status.idle":"2025-02-05T05:46:24.343251Z","shell.execute_reply.started":"2025-02-05T05:46:24.328785Z","shell.execute_reply":"2025-02-05T05:46:24.342237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def one_hot(Y):\n    one_hot_Y = np.zeros((Y.size, Y.max() + 1))\n    one_hot_Y[np.arange(Y.size), Y] = 1\n\n    one_hot_Y = one_hot_Y.T\n    \n    return one_hot_Y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.346084Z","iopub.execute_input":"2025-02-05T05:46:24.346364Z","iopub.status.idle":"2025-02-05T05:46:24.356998Z","shell.execute_reply.started":"2025-02-05T05:46:24.346339Z","shell.execute_reply":"2025-02-05T05:46:24.356068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def deriv_ReLU(z):\n    return z > 0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.358408Z","iopub.execute_input":"2025-02-05T05:46:24.358694Z","iopub.status.idle":"2025-02-05T05:46:24.372975Z","shell.execute_reply.started":"2025-02-05T05:46:24.358669Z","shell.execute_reply":"2025-02-05T05:46:24.372040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def back_prop(z1, a1, z2, a2, w2, X, Y):\n    # m = Y.size\n    one_hot_Y = one_hot(Y)\n    d_z2 = a2 - one_hot_Y\n    d_w2 = 1 / m * d_z2.dot(a1.T)\n    d_b2 = 1 / m * np.sum(z2)\n\n    d_z1 = w2.T.dot(d_z2) * deriv_ReLU(z1)\n    d_w1 = 1 / m * d_z1.dot(X.T)\n    d_b1 = 1 / m * np.sum(z1)\n\n    return d_w1, d_b1, d_w2, d_b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.374042Z","iopub.execute_input":"2025-02-05T05:46:24.374409Z","iopub.status.idle":"2025-02-05T05:46:24.389231Z","shell.execute_reply.started":"2025-02-05T05:46:24.374371Z","shell.execute_reply":"2025-02-05T05:46:24.388153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def update_params(w1, b1, w2, b2, d_w1, d_b1, d_w2, d_b2, alpha):\n    w1 = w1 - alpha * d_w1\n    b1 = b1 - alpha * d_b1\n    w2 = w2 - alpha * d_w2\n    b2 = b2 - alpha * d_b2\n\n    return w1, b1, w2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.390302Z","iopub.execute_input":"2025-02-05T05:46:24.390656Z","iopub.status.idle":"2025-02-05T05:46:24.403988Z","shell.execute_reply.started":"2025-02-05T05:46:24.390618Z","shell.execute_reply":"2025-02-05T05:46:24.403131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_pred(a2):\n    return np.argmax(a2, 0)\n    \ndef get_acc(pred, Y):\n    return np.sum(pred == Y) / Y.size\n\ndef gradient_descent(X, Y, iters, alpha):\n    w1, b1, w2, b2 = init_params()\n\n    for i in range(iters):\n        z1, a1, z2, a2 = fwd_prop(w1, b1, w2, b2, X)\n        d_w1, d_b1, d_w2, d_b2 = back_prop(z1, a1, z2, a2, w2, X, Y)\n        w1, b1, w2, b2 = update_params(w1, b1, w2, b2, d_w1, d_b1, d_w2, d_b2, alpha)\n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            print(\"Accuracy: \", get_acc(get_pred(a2),Y))\n\n    return w1, b1, w2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.405320Z","iopub.execute_input":"2025-02-05T05:46:24.405745Z","iopub.status.idle":"2025-02-05T05:46:24.421603Z","shell.execute_reply.started":"2025-02-05T05:46:24.405716Z","shell.execute_reply":"2025-02-05T05:46:24.420643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"w1, b1, w2, b2 = gradient_descent(X_train, Y_train, 500, 0.1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:46:24.422706Z","iopub.execute_input":"2025-02-05T05:46:24.423108Z","iopub.status.idle":"2025-02-05T05:47:24.109884Z","shell.execute_reply.started":"2025-02-05T05:46:24.423077Z","shell.execute_reply":"2025-02-05T05:47:24.108511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_pred(X, w1, b1, w2, b2):\n    _, _, _, a2 = fwd_prop(w1, b1, w2, b2, X)\n    return get_pred(a2)\n\ndef __TEST__(idx, w1, b1, w2, b2):\n    cur_img = X_train[:, idx, None]\n\n    prediction = make_pred(X_train[:, idx, None], w1, b1, w2, b2)\n    label = Y_train[idx]\n\n    print(\"Prediction: \", prediction)\n    print(\"Label: \", label)\n\n    cur_img = cur_img.reshape((28,28)) * 255\n    plt.gray()\n    plt.imshow(cur_img, interpolation='nearest')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:47:24.111016Z","iopub.execute_input":"2025-02-05T05:47:24.111482Z","iopub.status.idle":"2025-02-05T05:47:24.122708Z","shell.execute_reply.started":"2025-02-05T05:47:24.111439Z","shell.execute_reply":"2025-02-05T05:47:24.121412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"__TEST__(6186, w1, b1, w2, b2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:50:16.012260Z","iopub.execute_input":"2025-02-05T05:50:16.012680Z","iopub.status.idle":"2025-02-05T05:50:16.245574Z","shell.execute_reply.started":"2025-02-05T05:50:16.012649Z","shell.execute_reply":"2025-02-05T05:50:16.244406Z"}},"outputs":[],"execution_count":null}]}