{"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\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-26T13:58:23.381114Z","iopub.execute_input":"2022-07-26T13:58:23.381685Z","iopub.status.idle":"2022-07-26T13:58:23.386777Z","shell.execute_reply.started":"2022-07-26T13:58:23.381640Z","shell.execute_reply":"2022-07-26T13:58:23.385726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:58:24.944053Z","iopub.execute_input":"2022-07-26T13:58:24.944941Z","iopub.status.idle":"2022-07-26T13:58:27.712291Z","shell.execute_reply.started":"2022-07-26T13:58:24.944901Z","shell.execute_reply":"2022-07-26T13:58:27.711329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = np.array(df)\nm, n = df.shape\nnp.random.shuffle(df)          #Shuffle before taking dev and test data\n\ndev_data = df[:1000].T         #Development Data\ny_dev = dev_data[0]\nX_dev = dev_data[1:n] / 255\n\ntest_data = df[1000:m].T       #Test Data\ny_test = test_data[0]\nX_test = test_data[1:n] /255","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:58:27.714048Z","iopub.execute_input":"2022-07-26T13:58:27.714676Z","iopub.status.idle":"2022-07-26T13:58:28.522907Z","shell.execute_reply.started":"2022-07-26T13:58:27.714636Z","shell.execute_reply":"2022-07-26T13:58:28.521960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Weight initialization\ndef weight_init():\n    w_1 = np.random.randn(10,784)*np.sqrt(2/784)\n    b_1 = np.random.randn(10,1)*np.sqrt(2/784)\n    w_2 = np.random.randn(10,10)*np.sqrt(2/784)\n    b_2 = np.random.randn(10,1)*np.sqrt(2/784)\n    return w_1, b_1, w_2, b_2\n\n#Forward Propegation\ndef forward_prop(X, w_1, b_1, w_2, b_2):\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 = softmax(z_2)\n    return z_1, a_1, z_2, a_2\n\n#Relu activation function\ndef relu(z):\n    return np.maximum(z, 0)\n\n#Softmax activation function\ndef softmax(z):\n    z = np.exp(z)/sum(np.exp(z))\n    return z\n    \n#Y in a vector\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    \n#Backward Propegation\ndef back_prop(y, a_2, a_1, z_1, w_2,X):\n    one_hot_y = one_hot(y)\n    loss = a_2 - one_hot_y\n    dw_2 = 1/m * loss.dot(a_1.T)\n    db_2 = 1/m * np.sum(loss)\n    dz_1 = w_2.T.dot(loss) * relu_deriv(z_1)\n    dw_1 = 1/m * dz_1.dot(X.T)\n    db_1 = 1/m * np.sum(dz_1)\n    return dw_1, db_1, dw_2, db_2\n\n#Derivative of relu\ndef relu_deriv(z):\n    return z > 0\n\n#Parameters update\ndef update_params(dw_1, db_1, dw_2, db_2, w_1, b_1, w_2, b_2, alpha):\n    w_1 += -alpha*dw_1\n    b_1 += -alpha*db_1\n    w_2 += -alpha*dw_2\n    b_2 += -alpha*db_2\n    return w_1, b_1, w_2, b_2\n\n#Gets Predictions\ndef get_predictions(A2):\n    return np.argmax(A2, 0)\n\n#Gets Accuracy\ndef get_accuracy(predictions, Y):\n    print(predictions, Y)\n    return np.sum(predictions == Y) / Y.size\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:06:49.486782Z","iopub.execute_input":"2022-07-26T14:06:49.487713Z","iopub.status.idle":"2022-07-26T14:06:49.502838Z","shell.execute_reply.started":"2022-07-26T14:06:49.487673Z","shell.execute_reply":"2022-07-26T14:06:49.502224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gradient_descent(X, Y, iterations, alpha):\n    w_1, b_1, w_2, b_2 = weight_init()\n    for i in range(iterations):\n        z_1, a_1, z_2, a_2 = forward_prop(X, w_1, b_1, w_2, b_2)\n        dw_1, db_1, dw_2, db_2 = back_prop(Y, a_2, a_1, z_1,w_2,X)\n        w_1, b_1, w_2, b_2 = update_params(dw_1, db_1, dw_2, db_2, w_1, b_1, w_2, b_2, alpha)\n        if i % 100 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(a_2)\n            print(get_accuracy(predictions, Y))\n    return w_1, b_1, w_2, b_2\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:06:54.429936Z","iopub.execute_input":"2022-07-26T14:06:54.430485Z","iopub.status.idle":"2022-07-26T14:06:54.437620Z","shell.execute_reply.started":"2022-07-26T14:06:54.430452Z","shell.execute_reply":"2022-07-26T14:06:54.436681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_test, y_test, 1000, 0.1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:19:21.683744Z","iopub.execute_input":"2022-07-26T14:19:21.684050Z","iopub.status.idle":"2022-07-26T14:22:05.602244Z","shell.execute_reply.started":"2022-07-26T14:19:21.683996Z","shell.execute_reply":"2022-07-26T14:22:05.601256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(X, W1, b1, W2, b2)\n    predictions = get_predictions(A2)\n    return predictions\n\ndef test_prediction(index, W1, b1, W2, b2):\n    current_image = X_test[:, index, None]\n    prediction = make_predictions(X_test[:, index, None], W1, b1, W2, b2)\n    label = y_test[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":"2022-07-26T14:15:47.474470Z","iopub.execute_input":"2022-07-26T14:15:47.474891Z","iopub.status.idle":"2022-07-26T14:15:47.482481Z","shell.execute_reply.started":"2022-07-26T14:15:47.474861Z","shell.execute_reply":"2022-07-26T14:15:47.481870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction(0, W1, b1, W2, b2)\ntest_prediction(1, W1, b1, W2, b2)\ntest_prediction(2, W1, b1, W2, b2)\ntest_prediction(3, W1, b1, W2, b2)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:15:48.142113Z","iopub.execute_input":"2022-07-26T14:15:48.142664Z","iopub.status.idle":"2022-07-26T14:15:48.734095Z","shell.execute_reply.started":"2022-07-26T14:15:48.142618Z","shell.execute_reply":"2022-07-26T14:15:48.733444Z"},"trusted":true},"execution_count":null,"outputs":[]}]}