{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nnp.random.seed(0)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:42.912146Z","iopub.execute_input":"2025-06-16T17:30:42.913173Z","iopub.status.idle":"2025-06-16T17:30:42.917630Z","shell.execute_reply.started":"2025-06-16T17:30:42.913133Z","shell.execute_reply":"2025-06-16T17:30:42.916663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:42.919167Z","iopub.execute_input":"2025-06-16T17:30:42.919433Z","iopub.status.idle":"2025-06-16T17:30:44.917492Z","shell.execute_reply.started":"2025-06-16T17:30:42.919399Z","shell.execute_reply":"2025-06-16T17:30:44.916505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:44.918377Z","iopub.execute_input":"2025-06-16T17:30:44.918647Z","iopub.status.idle":"2025-06-16T17:30:44.932260Z","shell.execute_reply.started":"2025-06-16T17:30:44.918620Z","shell.execute_reply":"2025-06-16T17:30:44.931477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:44.933946Z","iopub.execute_input":"2025-06-16T17:30:44.934190Z","iopub.status.idle":"2025-06-16T17:30:44.989205Z","shell.execute_reply.started":"2025-06-16T17:30:44.934170Z","shell.execute_reply":"2025-06-16T17:30:44.988535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(df)\nm, n = data.shape\nprint(m, n)\nnp.random.shuffle(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:44.989911Z","iopub.execute_input":"2025-06-16T17:30:44.990184Z","iopub.status.idle":"2025-06-16T17:30:45.917681Z","shell.execute_reply.started":"2025-06-16T17:30:44.990156Z","shell.execute_reply":"2025-06-16T17:30:45.916819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dev = data[0:1000].T\nY_dev = data_dev[0]\nX_dev = data_dev[1:n]\n\ndata_train = data[1000:m].T\n\nY_train = data_train[0] \nX_train = data_train[1:n] \n\nX_train = X_train / 255.0\nX_dev = X_dev / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:45.918587Z","iopub.execute_input":"2025-06-16T17:30:45.918816Z","iopub.status.idle":"2025-06-16T17:30:46.437871Z","shell.execute_reply.started":"2025-06-16T17:30:45.918796Z","shell.execute_reply":"2025-06-16T17:30:46.437031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_dev.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:46.438796Z","iopub.execute_input":"2025-06-16T17:30:46.439149Z","iopub.status.idle":"2025-06-16T17:30:46.443683Z","shell.execute_reply.started":"2025-06-16T17:30:46.439121Z","shell.execute_reply":"2025-06-16T17:30:46.442828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_parameters():\n    W1 = np.random.randn(64, 784) * np.sqrt(2 / 784) \n    b1 = np.zeros((64, 1))\n    W2 = np.random.randn(64, 64) * np.sqrt(2 / 64) \n    b2 = np.zeros((64, 1)) \n    W3 = np.random.randn(10, 64) * np.sqrt(2 / 64) \n    b3 = np.zeros((10, 1)) \n    return W1, b1, W2, b2, W3, b3\n\ndef ReLU(z):\n    return np.maximum(z, 0)\n    # return np.where(z > 0, z, 0.1 * z)\n\ndef Leaky_ReLU(z):\n    # return np.maximum(z, 0)\n    return np.where(z > 0, z, 0.4 * z)\n\ndef sigmoid(z):\n    return 1 / (1 + np.exp(-z))\n\ndef deriv_sigmoid(z):\n    s = sigmoid(z)\n    return s * (1 - s)\n\ndef softmax(z):\n    exp_shifted = np.exp(z - np.max(z, axis=0, keepdims=True))\n    return exp_shifted / np.sum(exp_shifted, axis=0, keepdims=True)\n\ndef deriv_ReLU(z):\n    return (z>0)\n    # return np.where(z>0, 1, 0.1)\n\ndef deriv_Leaky_ReLU(z):\n    # return (z>0)\n    return np.where(z>0, 1, 0.4)\n    \ndef forward_prop(X, W1, b1, W2, b2, W3, b3):\n    Z1 = np.dot(W1, X) + b1\n    A1 = ReLU(Z1)\n    Z2 = np.dot(W2, A1) + b2\n    A2 = Leaky_ReLU(Z2)\n    Z3 = np.dot(W3, A2) + b3\n    A3 = softmax(Z3)\n    \n    return Z1, Z2, Z3, A1, A2, A3\n    \n\ndef one_hot(Y):\n    one_hot_Y = np.zeros((Y.max() + 1, Y.size))\n    one_hot_Y[Y, np.arange(Y.size)] = 1\n    # one_hot_Y = one_hot_Y.T\n    return one_hot_Y\n\ndef back_prop(Z1, A1, Z2, A2, Z3, A3, W3, W2, X, Y):\n    one_hot_Y = one_hot(Y)\n    m = Y.size\n    dZ3 = (A3 - one_hot_Y)\n    dw3 = (1/m) * np.dot(dZ3, A2.T)\n    db3 = 1/m * np.sum(dZ3, axis=1, keepdims=True)\n    \n    dZ2 = np.dot(W3.T, dZ3) * deriv_Leaky_ReLU(Z2)\n    dw2 = (1/m) * np.dot(dZ2, A1.T)\n    db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)\n\n    dZ1 = np.dot(W2.T, dZ2) * deriv_ReLU(Z1)\n    dw1 = (1/m) * np.dot(dZ1, X.T)\n    db1 = (1/m) * np.sum(dZ1, axis=1, keepdims=True)\n    return dw1, db1, dw2, db2, dw3, db3\n\n\ndef update_param(W1, W2, W3, b1, b2, b3, dw1, dw2, dw3, db1, db2, db3, alpha):\n    W1 = W1 - (alpha * dw1)\n    W2 = W2 - (alpha * dw2)\n    W3 = W3 - (alpha* dw3)\n    b1 = b1 - (alpha * db1)\n    b2 = b2 - (alpha * db2)\n    b3 = b3 - (alpha * db3)\n    return W1, b1, W2, b2, W3, b3\n\ndef get_predictions(A3):\n    return np.argmax(A3, 0)\n\ndef get_accuracy(predictions, Y):\n    # print(predictions, Y)\n    return np.mean(predictions == Y)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:46.444619Z","iopub.execute_input":"2025-06-16T17:30:46.444938Z","iopub.status.idle":"2025-06-16T17:30:46.461370Z","shell.execute_reply.started":"2025-06-16T17:30:46.444918Z","shell.execute_reply":"2025-06-16T17:30:46.460366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gradient_descent(X, Y, iterations, alpha):\n    W1, b1, W2, b2, W3, b3 = init_parameters()\n    # Y = one_hot_Y(Y)\n    for i in range(0, iterations):\n        Z1, Z2, Z3, A1, A2, A3 = forward_prop(X, W1, b1, W2, b2, W3, b3)\n        dw1, db1, dw2, db2, dw3, db3 = back_prop(Z1, A1, Z2, A2, Z3, A3, W3, W2, X, Y)\n        W1, b1, W2, b2, W3, b3 = update_param(W1, W2, W3, b1, b2, b3, dw1, dw2, dw3, db1, db2, db3, alpha)\n        \n        if i%10 == 0:\n            print(\"iterations : \", i)\n            accuracy = get_accuracy(get_predictions(A3), Y)\n            print(\"Accuracy : \", accuracy)\n\n    \n    predictions = get_predictions(A3)\n    accuracy = get_accuracy(predictions, Y)\n    print(\"Z1 : \", np.max(Z1), \" Z2 : \", np.max(Z2), \"Z3 : \", np.max(Z3))\n    print(\"Final Accuracy after \", i, \"iterations : \", accuracy)\n    return W1, b1, W2, b2, W3, b3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:46.463799Z","iopub.execute_input":"2025-06-16T17:30:46.464280Z","iopub.status.idle":"2025-06-16T17:30:46.484031Z","shell.execute_reply.started":"2025-06-16T17:30:46.464258Z","shell.execute_reply":"2025-06-16T17:30:46.483086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1, b1, W2, b2, W3, b3 = gradient_descent(X_train, Y_train, 1000, 0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:30:46.485000Z","iopub.execute_input":"2025-06-16T17:30:46.485344Z","iopub.status.idle":"2025-06-16T17:35:02.291900Z","shell.execute_reply.started":"2025-06-16T17:30:46.485319Z","shell.execute_reply":"2025-06-16T17:35:02.290875Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_predictions(X, Y, W1, b1, W2, b2, W3, b3):\n    Z1, Z2, Z3, A1, A2, A3 = forward_prop(X, W1, b1, W2, b2, W3, b3) \n    predictions = get_predictions(A3)\n    accuracy = get_accuracy(predictions, Y)\n    print(\"The dev data accuracy is : \" ,accuracy)\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:35:02.293052Z","iopub.execute_input":"2025-06-16T17:35:02.293382Z","iopub.status.idle":"2025-06-16T17:35:02.298936Z","shell.execute_reply.started":"2025-06-16T17:35:02.293353Z","shell.execute_reply":"2025-06-16T17:35:02.298078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dev_predictions = make_predictions(X_dev, Y_dev, W1, b1, W2, b2, W3, b3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T18:34:06.137554Z","iopub.execute_input":"2025-06-16T18:34:06.137889Z","iopub.status.idle":"2025-06-16T18:34:06.147562Z","shell.execute_reply.started":"2025-06-16T18:34:06.137867Z","shell.execute_reply":"2025-06-16T18:34:06.146682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def test_prediction(index, W1, b1, W2, b2, W3, b3):\n    current_image = X_train[:, index, None]\n    label = Y_train[index]\n    \n    # Forward propagation\n    Z1, Z2, Z3, A1, A2, A3 = forward_prop(current_image, W1, b1, W2, b2, W3, b3)\n    prediction = get_predictions(A3)[0]\n\n    print(\"Prediction: \", prediction)\n    print(\"Label: \", label)\n\n    # Reshape and display the image\n    current_image = current_image.reshape((28, 28)) * 255\n    plt.gray()\n    plt.imshow(current_image, interpolation='nearest')\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T18:34:09.524277Z","iopub.execute_input":"2025-06-16T18:34:09.524944Z","iopub.status.idle":"2025-06-16T18:34:09.530598Z","shell.execute_reply.started":"2025-06-16T18:34:09.524918Z","shell.execute_reply":"2025-06-16T18:34:09.529732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(11):\n    test_prediction(i, W1, b1, W2, b2, W3, b3)\n# test_prediction(0, W1, b1, W2, b2, W3, b3)\n# test_prediction(1, W1, b1, W2, b2, W3, b3)\n# test_prediction(2, W1, b1, W2, b2, W3, b3)\n# test_prediction(3, W1, b1, W2, b2, W3, b3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T17:48:28.828024Z","iopub.execute_input":"2025-06-16T17:48:28.828360Z","iopub.status.idle":"2025-06-16T17:48:30.348405Z","shell.execute_reply.started":"2025-06-16T17:48:28.828338Z","shell.execute_reply":"2025-06-16T17:48:30.347476Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = np.array(test_data).T / 255.0  # Normalize and transpose\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T18:40:08.500156Z","iopub.execute_input":"2025-06-16T18:40:08.500613Z","iopub.status.idle":"2025-06-16T18:40:11.905132Z","shell.execute_reply.started":"2025-06-16T18:40:08.500589Z","shell.execute_reply":"2025-06-16T18:40:11.904157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": np.arange(1, dev_predictions.shape[0] + 1),\n    \"Label\": dev_predictions\n})\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-16T18:40:12.046889Z","iopub.execute_input":"2025-06-16T18:40:12.047332Z","iopub.status.idle":"2025-06-16T18:40:12.054733Z","shell.execute_reply.started":"2025-06-16T18:40:12.047299Z","shell.execute_reply":"2025-06-16T18:40:12.053872Z"}},"outputs":[],"execution_count":null}]}