{"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 \nimport pandas as pd \nfrom matplotlib import pyplot as plt \n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:20.690483Z","iopub.execute_input":"2025-07-29T01:11:20.690853Z","iopub.status.idle":"2025-07-29T01:11:20.696725Z","shell.execute_reply.started":"2025-07-29T01:11:20.690824Z","shell.execute_reply":"2025-07-29T01:11:20.695493Z"}},"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-29T01:11:20.698686Z","iopub.execute_input":"2025-07-29T01:11:20.698964Z","iopub.status.idle":"2025-07-29T01:11:21.986593Z","shell.execute_reply.started":"2025-07-29T01:11:20.698946Z","shell.execute_reply":"2025-07-29T01:11:21.984517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:21.988053Z","iopub.execute_input":"2025-07-29T01:11:21.988313Z","iopub.status.idle":"2025-07-29T01:11:22.000150Z","shell.execute_reply.started":"2025-07-29T01:11:21.988293Z","shell.execute_reply":"2025-07-29T01:11:21.999071Z"}},"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]\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\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:22.001254Z","iopub.execute_input":"2025-07-29T01:11:22.001477Z","iopub.status.idle":"2025-07-29T01:11:22.453043Z","shell.execute_reply.started":"2025-07-29T01:11:22.001461Z","shell.execute_reply":"2025-07-29T01:11:22.451297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef init_params():\n    # Previous: W1 = np.random.rand(10, 784)-0.5\n    W1 = np.random.rand(64, 784)-0.5 # Change 10 to 64\n    b1 = np.random.rand(64,1)-0.5   # Change 10 to 64\n    # Previous: W2 = np.random.rand(10, 10)-0.5\n    W2 = np.random.rand(10, 64)-0.5 # Change second 10 to 64\n    b2 = np.random.rand(10,1)-0.5\n    return W1, b1, W2, b2\n\n#Relu function\ndef ReLu(Z):\n    return np.maximum(0,Z)\n    \n#softmax fucnction\ndef softmax(Z):\n    A = np.exp(Z)/sum(np.exp(Z))\n    return A\n    \n#forward propogation\ndef forward_propogation(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    \n#one hot encoding\ndef ohe(Y):\n    num_classes = Y.max() + 1\n    ohe_Y = np.zeros((Y.size, num_classes))\n    ohe_Y[np.arange(Y.size), Y] = 1\n    ohe_Y = ohe_Y.T\n    return ohe_Y\n\n#ReLu derivative function\ndef deriv_ReLu(Z):\n    return Z>0 \n\n#backward propogation\ndef backward_propogation(Z1,A1,Z2,A2,W1,W2,X,Y):\n    m=Y.size\n    ohe_Y = ohe(Y)\n    dZ2 = A2 - ohe_Y\n    dW2 = 1/m*dZ2.dot(A1.T)\n    db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)\n    dZ1 = W2.T.dot(dZ2)\n    dW1 = 1/m*dZ1.dot(X.T)\n    db1 = (1/m) * np.sum(dZ1, axis=1, keepdims=True)\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\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:22.455144Z","iopub.execute_input":"2025-07-29T01:11:22.455449Z","iopub.status.idle":"2025-07-29T01:11:22.466681Z","shell.execute_reply.started":"2025-07-29T01:11:22.455424Z","shell.execute_reply":"2025-07-29T01:11:22.464731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\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,iterations,alpha):\n    W1,b1,W2,b2 = init_params()\n    for i in range(iterations):\n        Z1,A1,Z2,A2 = forward_propogation(W1,b1,W2,b2,X)\n        dW1,db1,dW2,db2 = backward_propogation(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%50==0):\n            print(\"Iteration:\",i)\n            print(\"Accuracy\",get_accuracy(get_predictions(A2),Y))\n    return W1,b1,W2,b2\n        \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:22.468902Z","iopub.execute_input":"2025-07-29T01:11:22.469293Z","iopub.status.idle":"2025-07-29T01:11:22.495770Z","shell.execute_reply.started":"2025-07-29T01:11:22.469264Z","shell.execute_reply":"2025-07-29T01:11:22.494381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1,b1,W2,b2 = gradient_descent(X_train, Y_train,500,0.10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:11:22.496818Z","iopub.execute_input":"2025-07-29T01:11:22.497058Z","iopub.status.idle":"2025-07-29T01:12:55.495085Z","shell.execute_reply.started":"2025-07-29T01:11:22.497040Z","shell.execute_reply":"2025-07-29T01:12:55.493733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_propogation(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-07-29T01:12:55.496421Z","iopub.execute_input":"2025-07-29T01:12:55.496765Z","iopub.status.idle":"2025-07-29T01:12:55.503520Z","shell.execute_reply.started":"2025-07-29T01:12:55.496733Z","shell.execute_reply":"2025-07-29T01:12:55.502464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(Y_dev[8])\ntest_prediction(8, W1, b1, W2, b2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T01:15:56.948341Z","iopub.execute_input":"2025-07-29T01:15:56.948618Z","iopub.status.idle":"2025-07-29T01:15:57.060772Z","shell.execute_reply.started":"2025-07-29T01:15:56.948596Z","shell.execute_reply":"2025-07-29T01:15:57.059218Z"}},"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":"2025-07-29T01:12:55.928825Z","iopub.execute_input":"2025-07-29T01:12:55.928979Z","iopub.status.idle":"2025-07-29T01:12:55.942952Z","shell.execute_reply.started":"2025-07-29T01:12:55.928965Z","shell.execute_reply":"2025-07-29T01:12:55.941841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}