{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:32:35.699586Z","iopub.execute_input":"2026-08-05T10:32:35.700327Z","iopub.status.idle":"2026-08-05T10:32:35.704671Z","shell.execute_reply.started":"2026-08-05T10:32:35.700292Z","shell.execute_reply":"2026-08-05T10:32:35.703657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:39:31.240712Z","iopub.execute_input":"2026-08-05T10:39:31.241027Z","iopub.status.idle":"2026-08-05T10:39:34.003930Z","shell.execute_reply.started":"2026-08-05T10:39:31.241000Z","shell.execute_reply":"2026-08-05T10:39:34.002969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:40:31.341966Z","iopub.execute_input":"2026-08-05T10:40:31.342289Z","iopub.status.idle":"2026-08-05T10:40:31.380971Z","shell.execute_reply.started":"2026-08-05T10:40:31.342262Z","shell.execute_reply":"2026-08-05T10:40:31.380044Z"}},"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].T\nY_train = data_train[0]\nX_train = data_train[1:n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:45:30.266808Z","iopub.execute_input":"2026-08-05T10:45:30.267156Z","iopub.status.idle":"2026-08-05T10:45:30.984996Z","shell.execute_reply.started":"2026-08-05T10:45:30.267128Z","shell.execute_reply":"2026-08-05T10:45:30.983990Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:45:42.623993Z","iopub.execute_input":"2026-08-05T10:45:42.624986Z","iopub.status.idle":"2026-08-05T10:45:42.630781Z","shell.execute_reply.started":"2026-08-05T10:45:42.624945Z","shell.execute_reply":"2026-08-05T10:45:42.629851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train[:, 0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T10:47:34.324310Z","iopub.execute_input":"2026-08-05T10:47:34.324661Z","iopub.status.idle":"2026-08-05T10:47:34.331421Z","shell.execute_reply.started":"2026-08-05T10:47:34.324632Z","shell.execute_reply":"2026-08-05T10:47:34.330536Z"}},"outputs":[],"execution_count":null},{"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":"2026-08-05T11:14:44.308517Z","iopub.execute_input":"2026-08-05T11:14:44.309590Z","iopub.status.idle":"2026-08-05T11:14:44.320765Z","shell.execute_reply.started":"2026-08-05T11:14:44.309552Z","shell.execute_reply":"2026-08-05T11:14:44.319896Z"}},"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)\ndef 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":"2026-08-05T11:14:48.160773Z","iopub.execute_input":"2026-08-05T11:14:48.161090Z","iopub.status.idle":"2026-08-05T11:14:48.172417Z","shell.execute_reply.started":"2026-08-05T11:14:48.161064Z","shell.execute_reply":"2026-08-05T11:14:48.171239Z"}},"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":"2026-08-05T11:14:50.845175Z","iopub.execute_input":"2026-08-05T11:14:50.845512Z","iopub.status.idle":"2026-08-05T11:17:18.851934Z","shell.execute_reply.started":"2026-08-05T11:14:50.845485Z","shell.execute_reply":"2026-08-05T11:17:18.851252Z"}},"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":"2026-08-05T11:18:30.010020Z","iopub.execute_input":"2026-08-05T11:18:30.010400Z","iopub.status.idle":"2026-08-05T11:18:30.018401Z","shell.execute_reply.started":"2026-08-05T11:18:30.010368Z","shell.execute_reply":"2026-08-05T11:18:30.017353Z"}},"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)\ntest_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":"2026-08-05T11:18:46.962456Z","iopub.execute_input":"2026-08-05T11:18:46.962812Z","iopub.status.idle":"2026-08-05T11:18:47.976272Z","shell.execute_reply.started":"2026-08-05T11:18:46.962780Z","shell.execute_reply":"2026-08-05T11:18:47.975518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dev_predictions = make_predictions(X_dev, W1, b1, W2, b2)\nget_accuracy(dev_predictions, Y_dev)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-05T11:19:05.104283Z","iopub.execute_input":"2026-08-05T11:19:05.104654Z","iopub.status.idle":"2026-08-05T11:19:05.124228Z","shell.execute_reply.started":"2026-08-05T11:19:05.104593Z","shell.execute_reply":"2026-08-05T11:19:05.123571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}