{"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\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:48.965597Z","iopub.execute_input":"2025-07-07T08:07:48.96591Z","iopub.status.idle":"2025-07-07T08:07:48.971203Z","shell.execute_reply.started":"2025-07-07T08:07:48.965886Z","shell.execute_reply":"2025-07-07T08:07:48.970209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:48.972624Z","iopub.execute_input":"2025-07-07T08:07:48.973236Z","iopub.status.idle":"2025-07-07T08:07:51.305943Z","shell.execute_reply.started":"2025-07-07T08:07:48.97321Z","shell.execute_reply":"2025-07-07T08:07:51.305116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data)\n\ndata_dev = data[0:1000].T\ny_dev = data_dev[0]\nx_dev = data_dev[1:n]\n\ndata_train = data[1000:m]\ny_train = data_train[0]\nx_train = data_train[1:n]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:51.307762Z","iopub.execute_input":"2025-07-07T08:07:51.308153Z","iopub.status.idle":"2025-07-07T08:07:52.031243Z","shell.execute_reply.started":"2025-07-07T08:07:51.308121Z","shell.execute_reply":"2025-07-07T08:07:52.030309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_parameters():\n    w1 = np.random.randn(10,784)\n    b1 = np.random.randn(10,1)\n    w2 = np.random.randn(10,10)\n    b2 = np.random.randn(10,1)\n    return w1,b1,w2,b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.032279Z","iopub.execute_input":"2025-07-07T08:07:52.032608Z","iopub.status.idle":"2025-07-07T08:07:52.038155Z","shell.execute_reply.started":"2025-07-07T08:07:52.032579Z","shell.execute_reply":"2025-07-07T08:07:52.037164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def ReLU(z):\n    return np.maximum(0,z)\ndef softmax(z):\n    return np.exp(z)/np.sum(np.exp(z))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.039926Z","iopub.execute_input":"2025-07-07T08:07:52.040222Z","iopub.status.idle":"2025-07-07T08:07:52.060632Z","shell.execute_reply.started":"2025-07-07T08:07:52.0402Z","shell.execute_reply":"2025-07-07T08:07:52.05938Z"}},"outputs":[],"execution_count":null},{"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 z2,a1,z2,a2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.061691Z","iopub.execute_input":"2025-07-07T08:07:52.061961Z","iopub.status.idle":"2025-07-07T08:07:52.080525Z","shell.execute_reply.started":"2025-07-07T08:07:52.061911Z","shell.execute_reply":"2025-07-07T08:07:52.079548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def one_hot(y):\n    y = y.flatten()  # ensure it's 1D\n    n_classes = 10  # assuming 10 digits\n    one_hot_y = np.zeros((n_classes, y.size))\n    one_hot_y[y, np.arange(y.size)] = 1\n    return one_hot_y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.081578Z","iopub.execute_input":"2025-07-07T08:07:52.081885Z","iopub.status.idle":"2025-07-07T08:07:52.104002Z","shell.execute_reply.started":"2025-07-07T08:07:52.081857Z","shell.execute_reply":"2025-07-07T08:07:52.102866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def backward_prop(z1, a1, z2, a2, w1, w2, x, y):\n    m = x.shape[1]  # Number of samples\n    one_hot_y = one_hot(y)\n    \n    dz2 = a2 - one_hot_y\n    dw2 = (1 / m) * dz2.dot(a1.T)\n    db2 = (1 / m) * np.sum(dz2, axis=1, keepdims=True)\n    \n    dz1 = w2.T.dot(dz2) * ReLU_deriv(z1)\n    dw1 = (1 / m) * dz1.dot(x.T)\n    db1 = (1 / m) * np.sum(dz1, axis=1, keepdims=True)\n    \n    return dw1, db1, dw2, db2\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.105118Z","iopub.execute_input":"2025-07-07T08:07:52.105437Z","iopub.status.idle":"2025-07-07T08:07:52.123675Z","shell.execute_reply.started":"2025-07-07T08:07:52.10541Z","shell.execute_reply":"2025-07-07T08:07:52.122547Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.124676Z","iopub.execute_input":"2025-07-07T08:07:52.125115Z","iopub.status.idle":"2025-07-07T08:07:52.153713Z","shell.execute_reply.started":"2025-07-07T08:07:52.125064Z","shell.execute_reply":"2025-07-07T08:07:52.15223Z"}},"outputs":[],"execution_count":null},{"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, alpha, iterations):\n    w1, b1, w2, b2 = init_parameters()\n    for i in range(iterations):\n        z1, a1, z2, a2 = forward_prop(w1, b1, w2, b2, x)\n        dw1, db1, dw2, db2 = backward_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 % 10 == 0:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(a2)\n            print(get_accuracy(predictions, y))\n    return w1, b1, w2, b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.154728Z","iopub.execute_input":"2025-07-07T08:07:52.155169Z","iopub.status.idle":"2025-07-07T08:07:52.181877Z","shell.execute_reply.started":"2025-07-07T08:07:52.155139Z","shell.execute_reply":"2025-07-07T08:07:52.180671Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"w1, b1, w2, b2 = gradient_descent(x_train, y_train, 0.10, 500)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T08:07:52.184353Z","iopub.execute_input":"2025-07-07T08:07:52.184808Z","iopub.status.idle":"2025-07-07T08:07:52.232121Z","shell.execute_reply.started":"2025-07-07T08:07:52.184771Z","shell.execute_reply":"2025-07-07T08:07:52.2306Z"}},"outputs":[],"execution_count":null}]}