{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Neural Network to accurately recognize the value of a hand-written digit\n\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-14T03:00:23.857511Z","iopub.execute_input":"2023-09-14T03:00:23.857905Z","iopub.status.idle":"2023-09-14T03:00:23.863161Z","shell.execute_reply.started":"2023-09-14T03:00:23.857837Z","shell.execute_reply":"2023-09-14T03:00:23.861918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:23.868332Z","iopub.execute_input":"2023-09-14T03:00:23.868699Z","iopub.status.idle":"2023-09-14T03:00:26.530845Z","shell.execute_reply.started":"2023-09-14T03:00:23.868669Z","shell.execute_reply":"2023-09-14T03:00:26.529644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:26.532690Z","iopub.execute_input":"2023-09-14T03:00:26.533101Z","iopub.status.idle":"2023-09-14T03:00:26.552565Z","shell.execute_reply.started":"2023-09-14T03:00:26.533069Z","shell.execute_reply":"2023-09-14T03:00:26.551598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:26.553761Z","iopub.execute_input":"2023-09-14T03:00:26.554796Z","iopub.status.idle":"2023-09-14T03:00:27.215920Z","shell.execute_reply.started":"2023-09-14T03:00:26.554760Z","shell.execute_reply":"2023-09-14T03:00:27.214817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dev = data[0:1000].T\nY_dev = data_dev[0]\nX_dev = data_dev[1:n]\nX_dev = X_dev / 255.","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.218241Z","iopub.execute_input":"2023-09-14T03:00:27.218565Z","iopub.status.idle":"2023-09-14T03:00:27.224993Z","shell.execute_reply.started":"2023-09-14T03:00:27.218540Z","shell.execute_reply":"2023-09-14T03:00:27.223622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data[1000:m].T\nY_train = data_train[0]\nX_train = data_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.227019Z","iopub.execute_input":"2023-09-14T03:00:27.227936Z","iopub.status.idle":"2023-09-14T03:00:27.397634Z","shell.execute_reply.started":"2023-09-14T03:00:27.227896Z","shell.execute_reply":"2023-09-14T03:00:27.396288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[:,0].shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.399304Z","iopub.execute_input":"2023-09-14T03:00:27.399641Z","iopub.status.idle":"2023-09-14T03:00:27.406028Z","shell.execute_reply.started":"2023-09-14T03:00:27.399612Z","shell.execute_reply":"2023-09-14T03:00:27.405223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.407118Z","iopub.execute_input":"2023-09-14T03:00:27.408588Z","iopub.status.idle":"2023-09-14T03:00:27.424719Z","shell.execute_reply.started":"2023-09-14T03:00:27.408543Z","shell.execute_reply":"2023-09-14T03:00:27.423548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.426317Z","iopub.execute_input":"2023-09-14T03:00:27.426903Z","iopub.status.idle":"2023-09-14T03:00:27.433239Z","shell.execute_reply.started":"2023-09-14T03:00:27.426849Z","shell.execute_reply":"2023-09-14T03:00:27.432172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ReLU(Z):\n    return np.maximum(Z, 0)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.434873Z","iopub.execute_input":"2023-09-14T03:00:27.435491Z","iopub.status.idle":"2023-09-14T03:00:27.445778Z","shell.execute_reply.started":"2023-09-14T03:00:27.435450Z","shell.execute_reply":"2023-09-14T03:00:27.444923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.449590Z","iopub.execute_input":"2023-09-14T03:00:27.450307Z","iopub.status.idle":"2023-09-14T03:00:27.460805Z","shell.execute_reply.started":"2023-09-14T03:00:27.450263Z","shell.execute_reply":"2023-09-14T03:00:27.459131Z"},"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    Z2 = W2.dot(A1) + b2\n    A2 = softmax(Z2)\n    return Z1, A1, Z2, A2","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.462769Z","iopub.execute_input":"2023-09-14T03:00:27.463213Z","iopub.status.idle":"2023-09-14T03:00:27.473671Z","shell.execute_reply.started":"2023-09-14T03:00:27.463171Z","shell.execute_reply":"2023-09-14T03:00:27.472436Z"},"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":"2023-09-14T03:00:27.475797Z","iopub.execute_input":"2023-09-14T03:00:27.476172Z","iopub.status.idle":"2023-09-14T03:00:27.488699Z","shell.execute_reply.started":"2023-09-14T03:00:27.476142Z","shell.execute_reply":"2023-09-14T03:00:27.487689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deriv_ReLU(Z):\n    return Z > 0","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.489987Z","iopub.execute_input":"2023-09-14T03:00:27.490594Z","iopub.status.idle":"2023-09-14T03:00:27.503392Z","shell.execute_reply.started":"2023-09-14T03:00:27.490564Z","shell.execute_reply":"2023-09-14T03:00:27.501285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def back_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\n    m = Y.size\n    one_hot_Y = one_hot(Y)\n    dZ2 = A2 - one_hot_Y\n    dW2 = 1 / m * dZ2.dot(A1.T)\n    db2 = 1 / m * np.sum(dZ2)\n    dZ1 = W2.T.dot(dZ2) * deriv_ReLU(Z1)\n    dW1 = 1 / m * dZ1.dot(X.T)\n    db1 = 1 / m * np.sum(dZ1)\n    return dW1, db1, dW2, db2","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.504817Z","iopub.execute_input":"2023-09-14T03:00:27.505533Z","iopub.status.idle":"2023-09-14T03:00:27.517342Z","shell.execute_reply.started":"2023-09-14T03:00:27.505493Z","shell.execute_reply":"2023-09-14T03:00:27.515816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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":"2023-09-14T03:00:27.518799Z","iopub.execute_input":"2023-09-14T03:00:27.519239Z","iopub.status.idle":"2023-09-14T03:00:27.530341Z","shell.execute_reply.started":"2023-09-14T03:00:27.519202Z","shell.execute_reply":"2023-09-14T03:00:27.529119Z"},"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\ndef gradient_descent(X, Y, iterations, alpha):\n    W1, b1, W2, b2 = init_params()\n    for i in range(iterations):\n        Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X)\n        dW1, db1, dW2, db2 = back_prop(Z1, A1, Z2, A2, W1, W2, X, Y)\n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        if i % 50 == 0:\n            print(\"Iteration: \", i)\n            print(\"Accuracy: \", get_accuracy(get_predictions(A2), Y))\n    return W1, b1, W2, b2\n            ","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.531803Z","iopub.execute_input":"2023-09-14T03:00:27.532601Z","iopub.status.idle":"2023-09-14T03:00:27.544836Z","shell.execute_reply.started":"2023-09-14T03:00:27.532567Z","shell.execute_reply":"2023-09-14T03:00:27.543837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 500, 0.1)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:00:27.546099Z","iopub.execute_input":"2023-09-14T03:00:27.547035Z","iopub.status.idle":"2023-09-14T03:01:35.591517Z","shell.execute_reply.started":"2023-09-14T03:00:27.547001Z","shell.execute_reply":"2023-09-14T03:01:35.590293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Testing / Making predictions\ndef make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)\n    predictions = get_predictions(A2)\n    return predictions\n\ndef test_prediction(index, W1, b1, W2, b2):\n    current_image = X_train[:, index, None]\n    prediction = make_predictions(X_train[:, index, None], W1, b1, W2, b2)\n    label = Y_train[index]\n    print(\"Prediction: \", prediction)\n    print(\"Label: \", label)\n    \n    current_image = current_image.reshape((28, 28)) * 255\n    plt.gray()\n    plt.imshow(current_image, interpolation='nearest')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:01:35.593198Z","iopub.execute_input":"2023-09-14T03:01:35.594305Z","iopub.status.idle":"2023-09-14T03:01:35.604488Z","shell.execute_reply.started":"2023-09-14T03:01:35.594261Z","shell.execute_reply":"2023-09-14T03:01:35.602885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev_predictions = make_predictions(X_dev, W1, b1, W2, b2)\nget_accuracy(dev_predictions, Y_dev)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:01:35.606889Z","iopub.execute_input":"2023-09-14T03:01:35.607903Z","iopub.status.idle":"2023-09-14T03:01:35.634738Z","shell.execute_reply.started":"2023-09-14T03:01:35.607834Z","shell.execute_reply":"2023-09-14T03:01:35.633603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy on test data is [88]","metadata":{"execution":{"iopub.status.busy":"2023-09-14T03:01:35.636578Z","iopub.execute_input":"2023-09-14T03:01:35.637348Z","iopub.status.idle":"2023-09-14T03:01:35.642879Z","shell.execute_reply.started":"2023-09-14T03:01:35.637307Z","shell.execute_reply":"2023-09-14T03:01:35.641617Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}