{"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"},{"sourceId":1313369,"sourceType":"kernelVersion"}],"dockerImageVersionId":30732,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import random\nrandom.seed(10)\n\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\ndata = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-10T12:26:13.065138Z","iopub.execute_input":"2024-07-10T12:26:13.065785Z","iopub.status.idle":"2024-07-10T12:26:16.103762Z","shell.execute_reply.started":"2024-07-10T12:26:13.065750Z","shell.execute_reply":"2024-07-10T12:26:16.102777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm,n = data.shape\n\ndata_test = data[0:12600].T\ny_test = data_test[0]\nX_test = data_test[1:n]\nX_test = X_test / 255.\n\ndata_train = data[12600:m].T\ny_train = data_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255.\n\nprint(\"Shape of X_train: \", X_train.shape)\nprint(\"Shape of X_test: \", X_test.shape)\nprint(\"Shape of y_train: \", y_train.shape)\nprint(\"Shape of y_test: \", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.105790Z","iopub.execute_input":"2024-07-10T12:26:16.106139Z","iopub.status.idle":"2024-07-10T12:26:16.360695Z","shell.execute_reply.started":"2024-07-10T12:26:16.106110Z","shell.execute_reply":"2024-07-10T12:26:16.359648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Initializing parameters between [-0.5,0.5]\ndef init_params():\n    w1 = np.random.rand(15,784) - 0.5\n    b1 = np.random.rand(15,1) - 0.5\n    w2 = np.random.rand(10,15) - 0.5\n    b2 = np.random.rand(10,1) - 0.5\n    return w1,b1,w2,b2","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.362128Z","iopub.execute_input":"2024-07-10T12:26:16.362871Z","iopub.status.idle":"2024-07-10T12:26:16.369507Z","shell.execute_reply.started":"2024-07-10T12:26:16.362830Z","shell.execute_reply":"2024-07-10T12:26:16.368464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Activation functions\ndef ReLU(Z):\n    return np.maximum(0,Z)\n\ndef softmax(Z):\n    a = np.exp(Z)\n    sum = 0\n    for i in range(len(a)):\n        sum += a[i]\n    return a/sum","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.370865Z","iopub.execute_input":"2024-07-10T12:26:16.371237Z","iopub.status.idle":"2024-07-10T12:26:16.381496Z","shell.execute_reply.started":"2024-07-10T12:26:16.371209Z","shell.execute_reply":"2024-07-10T12:26:16.380439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Forward Propogation\ndef forward_prop(X,w1,b1,w2,b2):\n    Z1 = w1.dot(X) + b1\n    A1 = ReLU(Z1)\n    Z2 = w2.dot(A1) + b2\n    A2 = softmax(Z2)\n    return Z1,A1,Z2,A2","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.384185Z","iopub.execute_input":"2024-07-10T12:26:16.384532Z","iopub.status.idle":"2024-07-10T12:26:16.394275Z","shell.execute_reply.started":"2024-07-10T12:26:16.384486Z","shell.execute_reply":"2024-07-10T12:26:16.393333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ReLU derivative\ndef ReLU_deriv(Z):\n    return Z > 0\n\n#One hot matrix encoding for Y\ndef one_hot(Y):\n    one_hot_y = np.zeros((Y.size,10))\n    for i in range(len(Y)):\n        one_hot_y[i,Y[i]] = 1\n    return one_hot_y.T","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.395503Z","iopub.execute_input":"2024-07-10T12:26:16.395777Z","iopub.status.idle":"2024-07-10T12:26:16.406026Z","shell.execute_reply.started":"2024-07-10T12:26:16.395752Z","shell.execute_reply":"2024-07-10T12:26:16.404897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Backward Propogation\ndef backward_prop(X,Y,w1,A1,w2,A2,Z1,Z2):\n    one_hot_Y = one_hot(Y)\n    dZ2 = A2 - one_hot_Y\n    dw2 = dZ2.dot(A1.T) / m\n    db2 = np.sum(dZ2) / m\n    dZ1 = w2.T.dot(dZ2) * ReLU_deriv(Z1)\n    dw1 = dZ1.dot(X.T) / m\n    db1 = np.sum(dZ1) / m\n    return dw1,db1,dw2,db2\n\n#Update parameters\ndef update_params(w1,b1,w2,b2,dw1,db1,dw2,db2,alpha):\n    w1 = w1 - alpha * dw1\n    b1 = b1 - alpha * db1\n    w2 = w2 - alpha * dw2\n    b2 = b2 - alpha * db2\n    return w1,b1,w2,b2","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.407289Z","iopub.execute_input":"2024-07-10T12:26:16.407590Z","iopub.status.idle":"2024-07-10T12:26:16.420339Z","shell.execute_reply.started":"2024-07-10T12:26:16.407564Z","shell.execute_reply":"2024-07-10T12:26:16.419324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction\ndef get_predictions(A):\n    return np.argmax(A,0)\n\n#Accuracy\ndef get_accuracy(predictions,Y):\n    return np.sum(predictions == Y) / Y.size\n\n#Gradient descent\ndef gradient_descent(X,Y,alpha,iterations):\n    w1,b1,w2,b2 = init_params()\n    for i in range(iterations):\n        Z1,A1,Z2,A2 = forward_prop(X,w1,b1,w2,b2)\n        dw1,db1,dw2,db2 = backward_prop(X,Y,w1,A1,w2,A2,Z1,Z2)\n        w1,b1,w2,b2 = update_params(w1,b1,w2,b2,dw1,db1,dw2,db2,alpha)\n        if i % 20 == 0:\n            predictions = get_predictions(A2)\n            print(\"Iterations: \",i)\n            print(\"Accuracy: \",get_accuracy(predictions,Y))\n    return w1,b1,w2,b2","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.421393Z","iopub.execute_input":"2024-07-10T12:26:16.421694Z","iopub.status.idle":"2024-07-10T12:26:16.432336Z","shell.execute_reply.started":"2024-07-10T12:26:16.421668Z","shell.execute_reply":"2024-07-10T12:26:16.431346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w1,b1,w2,b2 = gradient_descent(X_train,y_train,1,1000)","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:26:16.433481Z","iopub.execute_input":"2024-07-10T12:26:16.433854Z","iopub.status.idle":"2024-07-10T12:28:25.870437Z","shell.execute_reply.started":"2024-07-10T12:26:16.433818Z","shell.execute_reply":"2024-07-10T12:28:25.868985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"_,_,_,A2_test = forward_prop(X_test,w1,b1,w2,b2)\nprediction_test = get_predictions(A2_test)\nprint(\"Accuracy for test sets: \", get_accuracy(prediction_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-07-10T12:31:04.119770Z","iopub.execute_input":"2024-07-10T12:31:04.120525Z","iopub.status.idle":"2024-07-10T12:31:04.155902Z","shell.execute_reply.started":"2024-07-10T12:31:04.120492Z","shell.execute_reply":"2024-07-10T12:31:04.154718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}