{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:51.172949Z","iopub.execute_input":"2025-10-10T22:21:51.173547Z","iopub.status.idle":"2025-10-10T22:21:51.182398Z","shell.execute_reply.started":"2025-10-10T22:21:51.173517Z","shell.execute_reply":"2025-10-10T22:21:51.180628Z"}},"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-10-10T22:21:51.183712Z","iopub.execute_input":"2025-10-10T22:21:51.184066Z","iopub.status.idle":"2025-10-10T22:21:53.812763Z","shell.execute_reply.started":"2025-10-10T22:21:51.184013Z","shell.execute_reply":"2025-10-10T22:21:53.811819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:53.813992Z","iopub.execute_input":"2025-10-10T22:21:53.814515Z","iopub.status.idle":"2025-10-10T22:21:53.830287Z","shell.execute_reply.started":"2025-10-10T22:21:53.814485Z","shell.execute_reply":"2025-10-10T22:21:53.829246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:53.832464Z","iopub.execute_input":"2025-10-10T22:21:53.832767Z","iopub.status.idle":"2025-10-10T22:21:56.118957Z","shell.execute_reply.started":"2025-10-10T22:21:53.832744Z","shell.execute_reply":"2025-10-10T22:21:56.117538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:56.120443Z","iopub.execute_input":"2025-10-10T22:21:56.120766Z","iopub.status.idle":"2025-10-10T22:21:56.153177Z","shell.execute_reply.started":"2025-10-10T22:21:56.120731Z","shell.execute_reply":"2025-10-10T22:21:56.151576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#data = np.array(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:56.154324Z","iopub.execute_input":"2025-10-10T22:21:56.154679Z","iopub.status.idle":"2025-10-10T22:21:56.177883Z","shell.execute_reply.started":"2025-10-10T22:21:56.154649Z","shell.execute_reply":"2025-10-10T22:21:56.176559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:56.179138Z","iopub.execute_input":"2025-10-10T22:21:56.179414Z","iopub.status.idle":"2025-10-10T22:21:56.217252Z","shell.execute_reply.started":"2025-10-10T22:21:56.179392Z","shell.execute_reply":"2025-10-10T22:21:56.216117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\nnp.random.shuffle(data) # shuffle before splitting into dev and training sets\n\ndata_dev = data[0:1000].T\nY_dev = data_dev[0]\nX_dev = data_dev[1:n]\nX_dev = X_dev / 255.\n\ndata_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":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:56.218318Z","iopub.execute_input":"2025-10-10T22:21:56.218653Z","iopub.status.idle":"2025-10-10T22:21:57.248195Z","shell.execute_reply.started":"2025-10-10T22:21:56.218624Z","shell.execute_reply":"2025-10-10T22:21:57.247169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train[:, 0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T22:21:57.249284Z","iopub.execute_input":"2025-10-10T22:21:57.249817Z","iopub.status.idle":"2025-10-10T22:21:57.256338Z","shell.execute_reply.started":"2025-10-10T22:21:57.249774Z","shell.execute_reply":"2025-10-10T22:21:57.255205Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"array([2, 1, 4, ..., 9, 7, 8])\nOur NN will have a simple two-layer architecture. Input layer 𝑎[0] will have 784 units corresponding to the 784 pixels in each 28x28 input image. A hidden layer 𝑎[1] will have 10 units with ReLU activation, and finally our output layer 𝑎[2] will have 10 units corresponding to the ten digit classes with softmax activation.\n\nForward propagation\n\n𝑍[1]=𝑊[1]𝑋+𝑏[1]\n𝐴[1]=𝑔ReLU(𝑍[1]))\n𝑍[2]=𝑊[2]𝐴[1]+𝑏[2]\n𝐴[2]=𝑔softmax(𝑍[2])\nBackward propagation\n\n𝑑𝑍[2]=𝐴[2]−𝑌\n𝑑𝑊[2]=1𝑚𝑑𝑍[2]𝐴[1]𝑇\n𝑑𝐵[2]=1𝑚Σ𝑑𝑍[2]\n𝑑𝑍[1]=𝑊[2]𝑇𝑑𝑍[2].∗𝑔[1]′(𝑧[1])\n𝑑𝑊[1]=1𝑚𝑑𝑍[1]𝐴[0]𝑇\n𝑑𝐵[1]=1𝑚Σ𝑑𝑍[1]\nParameter updates\n\n𝑊[2]:=𝑊[2]−𝛼𝑑𝑊[2]\n𝑏[2]:=𝑏[2]−𝛼𝑑𝑏[2]\n𝑊[1]:=𝑊[1]−𝛼𝑑𝑊[1]\n𝑏[1]:=𝑏[1]−𝛼𝑑𝑏[1]\nVars and shapes\n\nForward prop\n\n𝐴[0]=𝑋: 784 x m\n𝑍[1]∼𝐴[1]: 10 x m\n𝑊[1]: 10 x 784 (as 𝑊[1]𝐴[0]∼𝑍[1])\n𝐵[1]: 10 x 1\n𝑍[2]∼𝐴[2]: 10 x m\n𝑊[1]: 10 x 10 (as 𝑊[2]𝐴[1]∼𝑍[2])\n𝐵[2]: 10 x 1\nBackprop\n\n𝑑𝑍[2]: 10 x m ( 𝐴[2])\n𝑑𝑊[2]: 10 x 10\n𝑑𝐵[2]: 10 x 1\n𝑑𝑍[1]: 10 x m ( 𝐴[1])\n𝑑𝑊[1]: 10 x 10\n𝑑𝐵[1]: 10 x 1","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\n\ndef ReLU(Z):\n    return np.maximum(Z, 0)\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 ReLU_deriv(Z):\n    return Z > 0\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 backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\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) * ReLU_deriv(Z1)\n    dW1 = 1 / m * dZ1.dot(X.T)\n    db1 = 1 / m * np.sum(dZ1)\n    return dW1, db1, dW2, db2\n\ndef 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-10-10T22:21:57.259665Z","iopub.execute_input":"2025-10-10T22:21:57.259993Z","iopub.status.idle":"2025-10-10T22:21:57.279381Z","shell.execute_reply.started":"2025-10-10T22:21:57.259967Z","shell.execute_reply":"2025-10-10T22:21:57.277913Z"}},"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_params()\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-10-10T22:21:57.280341Z","iopub.execute_input":"2025-10-10T22:21:57.280694Z","iopub.status.idle":"2025-10-10T22:21:57.301676Z","shell.execute_reply.started":"2025-10-10T22:21:57.280664Z","shell.execute_reply":"2025-10-10T22:21:57.300172Z"}},"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-10-10T22:21:57.302509Z","iopub.execute_input":"2025-10-10T22:21:57.302889Z","iopub.status.idle":"2025-10-10T22:22:39.393472Z","shell.execute_reply.started":"2025-10-10T22:21:57.302769Z","shell.execute_reply":"2025-10-10T22:22:39.391533Z"}},"outputs":[],"execution_count":null},{"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-10-10T22:22:39.394979Z","iopub.execute_input":"2025-10-10T22:22:39.395419Z","iopub.status.idle":"2025-10-10T22:22:39.404990Z","shell.execute_reply.started":"2025-10-10T22:22:39.395382Z","shell.execute_reply":"2025-10-10T22:22:39.403952Z"}},"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-10-10T22:22:39.406322Z","iopub.execute_input":"2025-10-10T22:22:39.406670Z","iopub.status.idle":"2025-10-10T22:22:40.232464Z","shell.execute_reply.started":"2025-10-10T22:22:39.406639Z","shell.execute_reply":"2025-10-10T22:22:40.231512Z"}},"outputs":[],"execution_count":null}]}