{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Load Data**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndf = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:33.386609Z","iopub.execute_input":"2025-06-23T18:52:33.386972Z","iopub.status.idle":"2025-06-23T18:52:35.917828Z","shell.execute_reply.started":"2025-06-23T18:52:33.386946Z","shell.execute_reply":"2025-06-23T18:52:35.916683Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Prep","metadata":{}},{"cell_type":"markdown","source":"## Validation + Training Set","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:35.919800Z","iopub.execute_input":"2025-06-23T18:52:35.920222Z","iopub.status.idle":"2025-06-23T18:52:35.938524Z","shell.execute_reply.started":"2025-06-23T18:52:35.920195Z","shell.execute_reply":"2025-06-23T18:52:35.937005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = np.array(df)\nm, n = df.shape\nnp.random.shuffle(df)\n\ndf_dev = df[0:1000].T\nY_dev = df_dev[0]\nX_dev = df_dev[1:n]\nX_dev = X_dev / 255.\n\ndf_train = df[1000:m].T\nY_train = df_train[0]\nX_train = df_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:35.939696Z","iopub.execute_input":"2025-06-23T18:52:35.940086Z","iopub.status.idle":"2025-06-23T18:52:36.889071Z","shell.execute_reply.started":"2025-06-23T18:52:35.940060Z","shell.execute_reply":"2025-06-23T18:52:36.888111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.890907Z","iopub.execute_input":"2025-06-23T18:52:36.891234Z","iopub.status.idle":"2025-06-23T18:52:36.897075Z","shell.execute_reply.started":"2025-06-23T18:52:36.891210Z","shell.execute_reply":"2025-06-23T18:52:36.896110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[1000:1005].T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.898075Z","iopub.execute_input":"2025-06-23T18:52:36.898366Z","iopub.status.idle":"2025-06-23T18:52:36.916724Z","shell.execute_reply.started":"2025-06-23T18:52:36.898336Z","shell.execute_reply":"2025-06-23T18:52:36.915865Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Build Model**","metadata":{}},{"cell_type":"markdown","source":"## Initalize Parameters ","metadata":{}},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.917706Z","iopub.execute_input":"2025-06-23T18:52:36.918172Z","iopub.status.idle":"2025-06-23T18:52:36.935114Z","shell.execute_reply.started":"2025-06-23T18:52:36.918147Z","shell.execute_reply":"2025-06-23T18:52:36.934181Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Develop Neural Network","metadata":{}},{"cell_type":"code","source":"def ReLU(Z):\n    return np.maximum(0, Z)\n\ndef softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A\n\ndef 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\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 Z > 0\n\ndef back_prop(Z1, A1, Z2, A2, W2, X, Y):\n    m = X.shape[1]\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, 1, keepdims=True)\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, keepdims=True)\n    return dW1, db1, dW2, db2\n\ndef update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):\n    W1 -= alpha * dW1\n    b1 -= alpha * db1\n    W2 -= alpha * dW2\n    b2 -= alpha * db2\n    return W1, b1, W2, b2\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.936152Z","iopub.execute_input":"2025-06-23T18:52:36.936416Z","iopub.status.idle":"2025-06-23T18:52:36.953932Z","shell.execute_reply.started":"2025-06-23T18:52:36.936395Z","shell.execute_reply":"2025-06-23T18:52:36.953023Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Implement Gradient","metadata":{}},{"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, W2, X, Y)\n        W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        if(i % 10 == 0):\n            print(\"Iteration: \", i)\n            print(\"Accuracy: \", get_accuracy(get_predictions(A2), Y))\n    return W1, b1, W2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.954946Z","iopub.execute_input":"2025-06-23T18:52:36.955207Z","iopub.status.idle":"2025-06-23T18:52:36.978529Z","shell.execute_reply.started":"2025-06-23T18:52:36.955187Z","shell.execute_reply":"2025-06-23T18:52:36.977539Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Run Model**","metadata":{}},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 200, 0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:52:36.979541Z","iopub.execute_input":"2025-06-23T18:52:36.979868Z","iopub.status.idle":"2025-06-23T18:52:52.149520Z","shell.execute_reply.started":"2025-06-23T18:52:36.979837Z","shell.execute_reply":"2025-06-23T18:52:52.148259Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Display Predictions","metadata":{}},{"cell_type":"code","source":"def 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":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:54:12.941766Z","iopub.execute_input":"2025-06-23T18:54:12.942218Z","iopub.status.idle":"2025-06-23T18:54:12.950187Z","shell.execute_reply.started":"2025-06-23T18:54:12.942189Z","shell.execute_reply":"2025-06-23T18:54:12.948741Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T18:54:30.960715Z","iopub.execute_input":"2025-06-23T18:54:30.961643Z","iopub.status.idle":"2025-06-23T18:54:31.557791Z","shell.execute_reply.started":"2025-06-23T18:54:30.961614Z","shell.execute_reply":"2025-06-23T18:54:31.556731Z"}},"outputs":[],"execution_count":null}]}