{"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":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom matplotlib import pyplot as plt\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T15:46:32.430225Z","iopub.execute_input":"2025-08-09T15:46:32.430988Z","iopub.status.idle":"2025-08-09T15:46:32.434530Z","shell.execute_reply.started":"2025-08-09T15:46:32.430963Z","shell.execute_reply":"2025-08-09T15:46:32.433656Z"}},"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-08-09T15:46:36.616867Z","iopub.execute_input":"2025-08-09T15:46:36.617121Z","iopub.status.idle":"2025-08-09T15:46:39.881383Z","shell.execute_reply.started":"2025-08-09T15:46:36.617103Z","shell.execute_reply":"2025-08-09T15:46:39.880574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-09T15:46:53.848714Z","iopub.execute_input":"2025-08-09T15:46:53.849463Z","iopub.status.idle":"2025-08-09T15:46:53.876785Z","shell.execute_reply.started":"2025-08-09T15:46:53.849423Z","shell.execute_reply":"2025-08-09T15:46:53.875974Z"}},"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":"2025-08-09T15:47:06.200745Z","iopub.execute_input":"2025-08-09T15:47:06.201016Z","iopub.status.idle":"2025-08-09T15:47:06.829618Z","shell.execute_reply.started":"2025-08-09T15:47:06.200995Z","shell.execute_reply":"2025-08-09T15:47:06.828967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.rand(10, 784) -.5\n    b1 = np.random.rand(10, 1) -.5\n    W2 = np.random.rand(10, 10) -.5\n    b2 = np.random.rand(10, 1) -.5\n    return W1, b1, W2, b2\n\ndef ReLU(z):\n    return np.maximum(0,z)\n\ndef softmax(z):\n    return np.exp(z) / np.sum(exp(Z))\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]\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, Y):\n    m = y.size\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) * deriv_ReLU(Z1)\n    dW1= 1 / m * dZ1.dot(X.T)\n    db1 = 1/ m * np.sum(dZ2)\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\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_predicitons(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, W1, 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\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}