{"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":31192,"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)\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-12-10T22:18:36.353587Z","iopub.execute_input":"2025-12-10T22:18:36.353939Z","iopub.status.idle":"2025-12-10T22:18:36.364997Z","shell.execute_reply.started":"2025-12-10T22:18:36.353915Z","shell.execute_reply":"2025-12-10T22:18:36.363734Z"}},"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-12-10T22:18:36.366725Z","iopub.execute_input":"2025-12-10T22:18:36.368341Z","iopub.status.idle":"2025-12-10T22:18:38.780404Z","shell.execute_reply.started":"2025-12-10T22:18:36.368297Z","shell.execute_reply":"2025-12-10T22:18:38.779412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:38.781187Z","iopub.execute_input":"2025-12-10T22:18:38.781421Z","iopub.status.idle":"2025-12-10T22:18:38.796607Z","shell.execute_reply.started":"2025-12-10T22:18:38.781402Z","shell.execute_reply":"2025-12-10T22:18:38.795464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:38.799266Z","iopub.execute_input":"2025-12-10T22:18:38.799621Z","iopub.status.idle":"2025-12-10T22:18:38.823292Z","shell.execute_reply.started":"2025-12-10T22:18:38.799585Z","shell.execute_reply":"2025-12-10T22:18:38.821639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=np.array(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:38.824372Z","iopub.execute_input":"2025-12-10T22:18:38.824710Z","iopub.status.idle":"2025-12-10T22:18:39.006502Z","shell.execute_reply.started":"2025-12-10T22:18:38.824682Z","shell.execute_reply":"2025-12-10T22:18:39.005405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"m,n=data.shape\nprint(m,n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.007836Z","iopub.execute_input":"2025-12-10T22:18:39.008111Z","iopub.status.idle":"2025-12-10T22:18:39.013696Z","shell.execute_reply.started":"2025-12-10T22:18:39.008089Z","shell.execute_reply":"2025-12-10T22:18:39.012478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.random.shuffle(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.014802Z","iopub.execute_input":"2025-12-10T22:18:39.015134Z","iopub.status.idle":"2025-12-10T22:18:39.641091Z","shell.execute_reply.started":"2025-12-10T22:18:39.015106Z","shell.execute_reply":"2025-12-10T22:18:39.639728Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Splitting the data ","metadata":{}},{"cell_type":"code","source":"data_conv=data[0:1000].T        ## so now data shape (784,m),each column represent an image \nX_val=data_conv[1:n]          ## each row represent feature( row0-->label ,row1-->pixel1,...)\nY_val=data_conv[0] ## label\nX_val=X_val/255.0\n\ndata_train=data[1000:].T\nX_train=data_train[1:n]\nY_train=data_train[0]\nX_train=X_train/255.0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.643048Z","iopub.execute_input":"2025-12-10T22:18:39.643454Z","iopub.status.idle":"2025-12-10T22:18:39.853029Z","shell.execute_reply.started":"2025-12-10T22:18:39.643414Z","shell.execute_reply":"2025-12-10T22:18:39.852135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_val","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.857082Z","iopub.execute_input":"2025-12-10T22:18:39.857395Z","iopub.status.idle":"2025-12-10T22:18:39.867301Z","shell.execute_reply.started":"2025-12-10T22:18:39.857372Z","shell.execute_reply":"2025-12-10T22:18:39.865391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train[: ,0] ## first column (first image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.868709Z","iopub.execute_input":"2025-12-10T22:18:39.869079Z","iopub.status.idle":"2025-12-10T22:18:39.897079Z","shell.execute_reply.started":"2025-12-10T22:18:39.869054Z","shell.execute_reply":"2025-12-10T22:18:39.895850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train[: ,0].shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.897901Z","iopub.execute_input":"2025-12-10T22:18:39.898139Z","iopub.status.idle":"2025-12-10T22:18:39.922885Z","shell.execute_reply.started":"2025-12-10T22:18:39.898112Z","shell.execute_reply":"2025-12-10T22:18:39.921647Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Initialization the parameters ","metadata":{}},{"cell_type":"code","source":"\n def init_parameters1 ():\n     \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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.924016Z","iopub.execute_input":"2025-12-10T22:18:39.924264Z","iopub.status.idle":"2025-12-10T22:18:39.944706Z","shell.execute_reply.started":"2025-12-10T22:18:39.924243Z","shell.execute_reply":"2025-12-10T22:18:39.943639Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## He initialization ","metadata":{}},{"cell_type":"code","source":"def init_parameters ():\n    W1 = np.random.randn(10, 784) * np.sqrt(2/784)\n    b1 = np.zeros((10,1))\n    W2 = np.random.randn(10, 10) * np.sqrt(2/10)\n    b2 = np.zeros((10,1))\n    return W1,b1,W2,b2\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.946008Z","iopub.execute_input":"2025-12-10T22:18:39.946323Z","iopub.status.idle":"2025-12-10T22:18:39.968073Z","shell.execute_reply.started":"2025-12-10T22:18:39.946290Z","shell.execute_reply":"2025-12-10T22:18:39.966784Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Activation functions","metadata":{}},{"cell_type":"code","source":"def ReLU(Z):\n    return np.maximum(0,Z)\n\ndef softmax(Z):\n    A= np.exp(Z)/sum(np.exp(Z))\n    return A \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.969112Z","iopub.execute_input":"2025-12-10T22:18:39.969394Z","iopub.status.idle":"2025-12-10T22:18:39.991280Z","shell.execute_reply.started":"2025-12-10T22:18:39.969371Z","shell.execute_reply":"2025-12-10T22:18:39.989999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Forward Propagatin","metadata":{}},{"cell_type":"code","source":"def forward_prop(W1,b1,W2,b2,X):\n    Z1=W1.dot(X)+b1        ## same as W1@X --> matrix multiplication\n    A1= ReLU(Z1)\n\n    Z2=W2.dot(A1)+b2  \n    A2=softmax(Z2)\n    return Z1,A1,Z2,A2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:39.992572Z","iopub.execute_input":"2025-12-10T22:18:39.992885Z","iopub.status.idle":"2025-12-10T22:18:40.022611Z","shell.execute_reply.started":"2025-12-10T22:18:39.992838Z","shell.execute_reply":"2025-12-10T22:18:40.021093Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## One_hot_encoding","metadata":{}},{"cell_type":"code","source":"def one_hot(Y):\n    one_hot_Y= np.zeros((Y.size, Y.max() +1))  ## shape( #images, #classes =10)\n    one_hot_Y[np.arange(Y.size),Y] =1\n    one_hot_Y=one_hot_Y.T\n    return one_hot_Y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.024199Z","iopub.execute_input":"2025-12-10T22:18:40.024475Z","iopub.status.idle":"2025-12-10T22:18:40.051119Z","shell.execute_reply.started":"2025-12-10T22:18:40.024453Z","shell.execute_reply":"2025-12-10T22:18:40.049848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train.max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.052215Z","iopub.execute_input":"2025-12-10T22:18:40.052492Z","iopub.status.idle":"2025-12-10T22:18:40.078243Z","shell.execute_reply.started":"2025-12-10T22:18:40.052471Z","shell.execute_reply":"2025-12-10T22:18:40.076737Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Back Propagation","metadata":{}},{"cell_type":"code","source":"def deriv_ReLU(Z):\n    return Z > 0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.079366Z","iopub.execute_input":"2025-12-10T22:18:40.079715Z","iopub.status.idle":"2025-12-10T22:18:40.101736Z","shell.execute_reply.started":"2025-12-10T22:18:40.079691Z","shell.execute_reply":"2025-12-10T22:18:40.100682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def back_prop(Z1,A1,Z2,A2,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\n    dZ1= W2.T.dot(dZ2) * deriv_ReLU(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_param(W1,W2,b1,b2,dW1,dW2,db1,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,W2,b1,b2\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.102924Z","iopub.execute_input":"2025-12-10T22:18:40.103240Z","iopub.status.idle":"2025-12-10T22:18:40.128662Z","shell.execute_reply.started":"2025-12-10T22:18:40.103213Z","shell.execute_reply":"2025-12-10T22:18:40.127318Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Gradient Descent","metadata":{}},{"cell_type":"code","source":"def get_accuracy(predictions,Y):\n    print(predictions,Y)\n    return np.sum(predictions==Y) / Y.size\n\ndef get_predictions(A2):\n    return np.argmax(A2,axis=0)  ## get max per column\n    \ndef gradient_descent(X,Y,iterations,alpha):\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=back_prop(Z1,A1,Z2,A2,W2,X,Y)\n        W1,W2,b1,b2=update_param(W1,W2,b1,b2,dW1,dW2,db1,db2,alpha)\n        if i%10==0: # after 10 iterations \n            print('iteration:',i)\n            predictions=get_predictions(A2)\n            print(\"Accuracy:\",get_accuracy(predictions,Y))\n    return W1,b1,W2,b2\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.130282Z","iopub.execute_input":"2025-12-10T22:18:40.130938Z","iopub.status.idle":"2025-12-10T22:18:40.158390Z","shell.execute_reply.started":"2025-12-10T22:18:40.130895Z","shell.execute_reply":"2025-12-10T22:18:40.157227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 500,0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:18:40.159814Z","iopub.execute_input":"2025-12-10T22:18:40.160697Z","iopub.status.idle":"2025-12-10T22:19:20.971462Z","shell.execute_reply.started":"2025-12-10T22:18:40.160652Z","shell.execute_reply":"2025-12-10T22:19:20.970627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gradient_descent1(X,Y,iterations,alpha):\n    W1,b1,W2,b2=init_parameters1()\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,W2,b1,b2=update_param(W1,W2,b1,b2,dW1,dW2,db1,db2,alpha)\n        if i%10==0: # after 10 iterations \n            print('iteration:',i)\n            predictions=get_predictions(A2)\n            print(\"Accuracy:\",get_accuracy(predictions,Y))\n    return W1,b1,W2,b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:19:20.972401Z","iopub.execute_input":"2025-12-10T22:19:20.972716Z","iopub.status.idle":"2025-12-10T22:19:20.980815Z","shell.execute_reply.started":"2025-12-10T22:19:20.972686Z","shell.execute_reply":"2025-12-10T22:19:20.979933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent1(X_train, Y_train, 500,0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T22:19:20.981777Z","iopub.execute_input":"2025-12-10T22:19:20.982128Z","iopub.status.idle":"2025-12-10T22:20:00.497196Z","shell.execute_reply.started":"2025-12-10T22:19:20.982098Z","shell.execute_reply":"2025-12-10T22:20:00.495621Z"}},"outputs":[],"execution_count":null}]}