{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30732,"isInternetEnabled":true,"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)\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\nimport os\nfor 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","execution":{"iopub.status.busy":"2024-07-05T05:17:10.046267Z","iopub.execute_input":"2024-07-05T05:17:10.046956Z","iopub.status.idle":"2024-07-05T05:17:11.119143Z","shell.execute_reply.started":"2024-07-05T05:17:10.046918Z","shell.execute_reply":"2024-07-05T05:17:11.118085Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport random","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:20:31.67119Z","iopub.execute_input":"2024-07-05T05:20:31.671564Z","iopub.status.idle":"2024-07-05T05:20:31.6767Z","shell.execute_reply.started":"2024-07-05T05:20:31.671536Z","shell.execute_reply":"2024-07-05T05:20:31.675441Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:17:58.205197Z","iopub.execute_input":"2024-07-05T05:17:58.205906Z","iopub.status.idle":"2024-07-05T05:18:01.680882Z","shell.execute_reply.started":"2024-07-05T05:17:58.205869Z","shell.execute_reply":"2024-07-05T05:18:01.679794Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:18:02.524375Z","iopub.execute_input":"2024-07-05T05:18:02.524757Z","iopub.status.idle":"2024-07-05T05:18:02.551764Z","shell.execute_reply.started":"2024-07-05T05:18:02.524727Z","shell.execute_reply":"2024-07-05T05:18:02.550814Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\n\nm, n = data.shape\nnp.random.shuffle(data)\n\ndata_train = data[0:33600].T\ndata_test = data[33600:m].T\n\nY_train = data_train[0].reshape(1,33600)\nX_train = data_train[1:] / 255.\n\nY_test = data_test[0].reshape(1,8400)\nX_test = data_test[1:] / 255.","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:19:42.71755Z","iopub.execute_input":"2024-07-05T05:19:42.717936Z","iopub.status.idle":"2024-07-05T05:19:43.51844Z","shell.execute_reply.started":"2024-07-05T05:19:42.717906Z","shell.execute_reply":"2024-07-05T05:19:43.51735Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"m, n = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:19:44.607677Z","iopub.execute_input":"2024-07-05T05:19:44.608053Z","iopub.status.idle":"2024-07-05T05:19:44.61252Z","shell.execute_reply.started":"2024-07-05T05:19:44.608024Z","shell.execute_reply":"2024-07-05T05:19:44.611478Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape)\nprint(Y_train.shape)\nprint(X_test.shape)\nprint(Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:19:45.876072Z","iopub.execute_input":"2024-07-05T05:19:45.876473Z","iopub.status.idle":"2024-07-05T05:19:45.882415Z","shell.execute_reply.started":"2024-07-05T05:19:45.876443Z","shell.execute_reply":"2024-07-05T05:19:45.88139Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:19:47.746614Z","iopub.execute_input":"2024-07-05T05:19:47.747003Z","iopub.status.idle":"2024-07-05T05:19:47.754474Z","shell.execute_reply.started":"2024-07-05T05:19:47.746971Z","shell.execute_reply":"2024-07-05T05:19:47.753579Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"index = random.randrange(0, X_train.shape[1])\nplt.imshow(X_train[:, index].reshape(28,28,1 ))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:20:34.242613Z","iopub.execute_input":"2024-07-05T05:20:34.242996Z","iopub.status.idle":"2024-07-05T05:20:34.439393Z","shell.execute_reply.started":"2024-07-05T05:20:34.242965Z","shell.execute_reply":"2024-07-05T05:20:34.438167Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W = {}\nb = {}\nZ = {}\nA = {}\ndW = {}\ndb = {}\ndZ = {}","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:29:54.488558Z","iopub.execute_input":"2024-07-05T05:29:54.489302Z","iopub.status.idle":"2024-07-05T05:29:54.493866Z","shell.execute_reply.started":"2024-07-05T05:29:54.489268Z","shell.execute_reply":"2024-07-05T05:29:54.492847Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_params():\n    t=0\n    W[1] = (np.random.randn(10,784) - t) / np.sqrt(784)\n    b[1] = (np.zeros((10,1)))\n    W[2] = (np.random.randn(10,10) - t) / np.sqrt(10)\n    b[2] = (np.zeros((10,1)))\n    W[3] = (np.random.randn(10,10) - t) / np.sqrt(10)\n    b[3] = (np.zeros((10,1)))\n\ndef ReLU(Z):\n    return np.maximum(0, Z)\n\ndef softmax(Z):\n    exp_Z = np.exp(Z)\n    return exp_Z / np.sum(exp_Z, 0)\n    \ndef forward_prop(X): \n    Z[1] = W[1].dot(X) + b[1]\n    A[1] = ReLU(Z[1])\n    \n    Z[2] = W[2].dot(A[1]) + b[2]\n    A[2] = ReLU(Z[2])\n    \n    Z[3] = W[3].dot(A[2])+b[3]\n    A[3] = softmax(Z[3])\n    \ndef 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    one_hot_Y = one_hot_Y.T\n    return one_hot_Y\n\ndef deriv_ReLU(Z):\n    return np.array(Z > 0, dtype = 'float')\n\ndef back_prop(X, Y):\n    one_hot_Y = one_hot(Y)\n    \n    dZ[3] = A[3] - Y\n    dW[3] = (1. / m) * np.dot(dZ[3], A[2].T)\n    db[3] = (1. / m) * np.sum(dZ[3], axis=1, keepdims=True)\n    \n    dZ[2] = np.dot(W[3].T, dZ[3]) * deriv_ReLU(Z[2])\n    dW[2] = (1. / m) * np.dot(dZ[2], A[1].T)\n    db[2] = (1. / m) * np.sum(dZ[2], axis=1, keepdims=True)\n    \n    dZ[1] = np.dot(W[2].T, dZ[2]) * deriv_ReLU(Z[1])\n    dW[1] = (1. / m) * np.dot(dZ[1], X.T)\n    db[1] = (1. / m) * np.sum(dZ[1], axis=1, keepdims=True)\n    \ndef update_params(lr):\n    for i in range(1,4):\n        W[i] -= lr * dW[i]\n        b[i] -= lr *i]","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:58:17.208006Z","iopub.execute_input":"2024-07-05T05:58:17.208418Z","iopub.status.idle":"2024-07-05T05:58:17.22454Z","shell.execute_reply.started":"2024-07-05T05:58:17.20839Z","shell.execute_reply":"2024-07-05T05:58:17.223292Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_predictions():\n    return np.argmax(A[3], 0)\n\ndef get_accuracy(Y):\n    preds = get_predictions()\n    print(preds, Y)\n    return np.sum(preds == Y) / Y.size\n\ndef gradient_descent(X, Y, iterations, lr):\n    init_params()\n    for i in range(iterations):\n        forward_prop(X)\n        back_prop(X, Y)\n        update_params(lr)\n        \n        if i%50 == 0:\n            print(\"iteration number: \", i+1)\n            print(\"Accuracy: \", get_accuracy(Y))","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:58:18.411032Z","iopub.execute_input":"2024-07-05T05:58:18.411796Z","iopub.status.idle":"2024-07-05T05:58:18.418744Z","shell.execute_reply.started":"2024-07-05T05:58:18.411757Z","shell.execute_reply":"2024-07-05T05:58:18.417355Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gradient_descent(X_train, Y_train, 10, 0.1)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:58:44.41011Z","iopub.execute_input":"2024-07-05T05:58:44.410477Z","iopub.status.idle":"2024-07-05T05:58:45.265734Z","shell.execute_reply.started":"2024-07-05T05:58:44.410448Z","shell.execute_reply":"2024-07-05T05:58:45.264206Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:58:36.224489Z","iopub.execute_input":"2024-07-05T05:58:36.224854Z","iopub.status.idle":"2024-07-05T05:58:36.234329Z","shell.execute_reply.started":"2024-07-05T05:58:36.224827Z","shell.execute_reply":"2024-07-05T05:58:36.233049Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Z","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:31:28.729328Z","iopub.execute_input":"2024-07-05T05:31:28.73007Z","iopub.status.idle":"2024-07-05T05:31:28.738285Z","shell.execute_reply.started":"2024-07-05T05:31:28.730035Z","shell.execute_reply":"2024-07-05T05:31:28.737243Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dZ","metadata":{"execution":{"iopub.status.busy":"2024-07-05T05:31:29.848027Z","iopub.execute_input":"2024-07-05T05:31:29.848816Z","iopub.status.idle":"2024-07-05T05:31:29.856838Z","shell.execute_reply.started":"2024-07-05T05:31:29.848779Z","shell.execute_reply":"2024-07-05T05:31:29.855895Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W","metadata":{"execution":{"iopub.status.busy":"2024-07-04T15:50:00.774807Z","iopub.status.idle":"2024-07-04T15:50:00.775259Z","shell.execute_reply.started":"2024-07-04T15:50:00.77505Z","shell.execute_reply":"2024-07-04T15:50:00.775069Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}