{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-28T10:57:58.872383Z","iopub.execute_input":"2022-08-28T10:57:58.872901Z","iopub.status.idle":"2022-08-28T10:57:58.878981Z","shell.execute_reply.started":"2022-08-28T10:57:58.872861Z","shell.execute_reply":"2022-08-28T10:57:58.877721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:57:58.881396Z","iopub.execute_input":"2022-08-28T10:57:58.882177Z","iopub.status.idle":"2022-08-28T10:58:01.496533Z","shell.execute_reply.started":"2022-08-28T10:57:58.882126Z","shell.execute_reply":"2022-08-28T10:58:01.495347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:01.499145Z","iopub.execute_input":"2022-08-28T10:58:01.499537Z","iopub.status.idle":"2022-08-28T10:58:01.520006Z","shell.execute_reply.started":"2022-08-28T10:58:01.499501Z","shell.execute_reply":"2022-08-28T10:58:01.518562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random .shuffle(data)\n\ndata_dev = data[0:1000].T\nX_dev = data_dev[1:n]\nY_dev = data_dev[0]\n\ndata_train = data[1000:m].T\nX_train = data_train[1:n]\nY_train = data_train[0]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:01.521964Z","iopub.execute_input":"2022-08-28T10:58:01.522465Z","iopub.status.idle":"2022-08-28T10:58:02.154428Z","shell.execute_reply.started":"2022-08-28T10:58:01.522418Z","shell.execute_reply":"2022-08-28T10:58:02.153234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.randn(10,784)\n    b1 = np.random.randn(10,1)\n    \n    W2 = np.random.randn(10, 10)\n    b2 = np.random.randn(10,1)\n    return W1, b1, W2, b2\n","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.157351Z","iopub.execute_input":"2022-08-28T10:58:02.157802Z","iopub.status.idle":"2022-08-28T10:58:02.164476Z","shell.execute_reply.started":"2022-08-28T10:58:02.157761Z","shell.execute_reply":"2022-08-28T10:58:02.163116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Tanh(Z):\n    return np.tanh(Z)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.166177Z","iopub.execute_input":"2022-08-28T10:58:02.167062Z","iopub.status.idle":"2022-08-28T10:58:02.176476Z","shell.execute_reply.started":"2022-08-28T10:58:02.167028Z","shell.execute_reply":"2022-08-28T10:58:02.175446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ReLU(Z):\n    return np.maximum(Z,0)","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.178428Z","iopub.execute_input":"2022-08-28T10:58:02.178921Z","iopub.status.idle":"2022-08-28T10:58:02.187743Z","shell.execute_reply.started":"2022-08-28T10:58:02.178872Z","shell.execute_reply":"2022-08-28T10:58:02.186701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def forward_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 = Tanh(Z2)\n    \n    return Z1, A1, Z2, A2\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.18926Z","iopub.execute_input":"2022-08-28T10:58:02.18988Z","iopub.status.idle":"2022-08-28T10:58:02.199309Z","shell.execute_reply.started":"2022-08-28T10:58:02.18982Z","shell.execute_reply":"2022-08-28T10:58:02.198152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    one_hot_Y = one_hot_Y.T\n    return one_hot_Y","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.200791Z","iopub.execute_input":"2022-08-28T10:58:02.201402Z","iopub.status.idle":"2022-08-28T10:58:02.216533Z","shell.execute_reply.started":"2022-08-28T10:58:02.201347Z","shell.execute_reply":"2022-08-28T10:58:02.215419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deriv_sigmoid(Z):\n    \n     return 1-(Sigmoid(Z))**2","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.218303Z","iopub.execute_input":"2022-08-28T10:58:02.218898Z","iopub.status.idle":"2022-08-28T10:58:02.227399Z","shell.execute_reply.started":"2022-08-28T10:58:02.218857Z","shell.execute_reply":"2022-08-28T10:58:02.226202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deriv_ReLU(Z):\n    return Z > 0","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.230708Z","iopub.execute_input":"2022-08-28T10:58:02.231104Z","iopub.status.idle":"2022-08-28T10:58:02.239366Z","shell.execute_reply.started":"2022-08-28T10:58:02.231072Z","shell.execute_reply":"2022-08-28T10:58:02.238167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def back_prop(W1,  W2, Z1, Z2, A1, A2, X, Y):\n    m = Y.size\n    one_hot_Y = one_hot(Y)\n    \n    dZ2 = 2*(A2 - one_hot_Y)\n    dW2 = 1/m*dZ2.dot(A1.T)\n    db2 = 1/m*np.sum(dZ2,1)\n    \n    dZ1 = W2.T.dot(dZ2)*deriv_ReLU(Z1)\n    dW1 = 1/m*dZ1.dot(X.T)\n    db1 = 1/m*np.sum(dZ1,1)\n    \n    return  dW1, db1, dW2, db2\n\ndef update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):\n    W1 = W1 - alpha*dW1\n    \n    W2 = W2 - alpha*dW2\n    \n    b1 = b1 - alpha*np.reshape(db1, (10,1))\n    \n    b2 = b2 - alpha*np.reshape(db2, (10,1))\n    \n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.241198Z","iopub.execute_input":"2022-08-28T10:58:02.242126Z","iopub.status.idle":"2022-08-28T10:58:02.251669Z","shell.execute_reply.started":"2022-08-28T10:58:02.242078Z","shell.execute_reply":"2022-08-28T10:58:02.25074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predictions(A2):\n    return np.argmax(A2, 0)\n\ndef get_accuracy(predictions, Y):\n    print(predictions, Y)\n    return np.sum(predictions == Y) / Y.size\n","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.253563Z","iopub.execute_input":"2022-08-28T10:58:02.254518Z","iopub.status.idle":"2022-08-28T10:58:02.265945Z","shell.execute_reply.started":"2022-08-28T10:58:02.254471Z","shell.execute_reply":"2022-08-28T10:58:02.264445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gradient_descent(X, Y, iterations, alpha):\n    \n    W1, b1, W2, b2 = init_params()\n    \n    for i in range(iterations):  \n        \n        Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X)\n    \n        dW1, db1, dW2, db2 = back_prop(W1,  W2, Z1, Z2, A1, A2, X, Y)\n        \n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        \n        if i % 10 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(A2)\n            print(get_accuracy(predictions, Y))\n    return W1, b1, W2, b2\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.267796Z","iopub.execute_input":"2022-08-28T10:58:02.268975Z","iopub.status.idle":"2022-08-28T10:58:02.279419Z","shell.execute_reply.started":"2022-08-28T10:58:02.268927Z","shell.execute_reply":"2022-08-28T10:58:02.277738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 500, 0.5)","metadata":{"execution":{"iopub.status.busy":"2022-08-28T10:58:02.28124Z","iopub.execute_input":"2022-08-28T10:58:02.281642Z","iopub.status.idle":"2022-08-28T10:58:23.360492Z","shell.execute_reply.started":"2022-08-28T10:58:02.281601Z","shell.execute_reply":"2022-08-28T10:58:23.358601Z"},"trusted":true},"execution_count":null,"outputs":[]}]}