{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T15:29:15.877149Z","iopub.execute_input":"2025-04-20T15:29:15.877573Z","iopub.status.idle":"2025-04-20T15:29:16.232584Z","shell.execute_reply.started":"2025-04-20T15:29:15.877539Z","shell.execute_reply":"2025-04-20T15:29:16.231718Z"}},"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-04-20T15:29:16.863814Z","iopub.execute_input":"2025-04-20T15:29:16.864335Z","iopub.status.idle":"2025-04-20T15:29:20.498348Z","shell.execute_reply.started":"2025-04-20T15:29:16.864306Z","shell.execute_reply":"2025-04-20T15:29:20.497494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T15:29:20.499709Z","iopub.execute_input":"2025-04-20T15:29:20.500003Z","iopub.status.idle":"2025-04-20T15:29:20.543637Z","shell.execute_reply.started":"2025-04-20T15:29:20.499981Z","shell.execute_reply":"2025-04-20T15:29:20.542541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=np.array(data)\nm,n=data.shape\nnp.random.shuffle(data)\ndata_dev=data[0:1000].T\ny_dev=data_dev[0]\nx_dev=data_dev[1:n]\nx_dev=x_dev /255.0\ndata_train=data[1000:m].T\ny_train=data_train[0]\nx_train=data_train[1:n]\nx_train=x_train /255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T15:29:23.432450Z","iopub.execute_input":"2025-04-20T15:29:23.432772Z","iopub.status.idle":"2025-04-20T15:29:24.312575Z","shell.execute_reply.started":"2025-04-20T15:29:23.432744Z","shell.execute_reply":"2025-04-20T15:29:24.311760Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T15:29:25.441635Z","iopub.execute_input":"2025-04-20T15:29:25.442514Z","iopub.status.idle":"2025-04-20T15:29:25.448047Z","shell.execute_reply.started":"2025-04-20T15:29:25.442483Z","shell.execute_reply":"2025-04-20T15:29:25.447167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train[:,0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T15:29:27.876549Z","iopub.execute_input":"2025-04-20T15:29:27.877479Z","iopub.status.idle":"2025-04-20T15:29:27.882656Z","shell.execute_reply.started":"2025-04-20T15:29:27.877449Z","shell.execute_reply":"2025-04-20T15:29:27.881892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def initialize_para ():\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 / 784)\n    b2=np.zeros((10,1)) \n    return w1,b1,w2,b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:38.662314Z","iopub.execute_input":"2025-04-20T16:04:38.662611Z","iopub.status.idle":"2025-04-20T16:04:38.667792Z","shell.execute_reply.started":"2025-04-20T16:04:38.662589Z","shell.execute_reply":"2025-04-20T16:04:38.666913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def RELU (z):\n    return np.maximum(0,z)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:42.859926Z","iopub.execute_input":"2025-04-20T16:04:42.860312Z","iopub.status.idle":"2025-04-20T16:04:42.865555Z","shell.execute_reply.started":"2025-04-20T16:04:42.860284Z","shell.execute_reply":"2025-04-20T16:04:42.864488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def softmax(z):\n    exp_z=np.exp(z - np.max(z,axis=0, keepdims=True))\n    return exp_z / np.sum(exp_z,axis=0, keepdims=True)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:43.339593Z","iopub.execute_input":"2025-04-20T16:04:43.339924Z","iopub.status.idle":"2025-04-20T16:04:43.345036Z","shell.execute_reply.started":"2025-04-20T16:04:43.339900Z","shell.execute_reply":"2025-04-20T16:04:43.344003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def forword_propagation(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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:43.758040Z","iopub.execute_input":"2025-04-20T16:04:43.758340Z","iopub.status.idle":"2025-04-20T16:04:43.763320Z","shell.execute_reply.started":"2025-04-20T16:04:43.758321Z","shell.execute_reply":"2025-04-20T16:04:43.762272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def 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\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:44.164558Z","iopub.execute_input":"2025-04-20T16:04:44.164887Z","iopub.status.idle":"2025-04-20T16:04:44.170328Z","shell.execute_reply.started":"2025-04-20T16:04:44.164864Z","shell.execute_reply":"2025-04-20T16:04:44.169427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def derivative_RELU(z):\n    return z >0 \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:44.586994Z","iopub.execute_input":"2025-04-20T16:04:44.587922Z","iopub.status.idle":"2025-04-20T16:04:44.591762Z","shell.execute_reply.started":"2025-04-20T16:04:44.587885Z","shell.execute_reply":"2025-04-20T16:04:44.590875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def back_propagation(z1,a1,z2,a2,w2,x,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,axis=1,keepdims=True)\n    dz1=w2.dot(dz2)* derivative_RELU(z1)\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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:45.005174Z","iopub.execute_input":"2025-04-20T16:04:45.005484Z","iopub.status.idle":"2025-04-20T16:04:45.011468Z","shell.execute_reply.started":"2025-04-20T16:04:45.005462Z","shell.execute_reply":"2025-04-20T16:04:45.010575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def update_para(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-04-20T16:04:45.409084Z","iopub.execute_input":"2025-04-20T16:04:45.409395Z","iopub.status.idle":"2025-04-20T16:04:45.414331Z","shell.execute_reply.started":"2025-04-20T16:04:45.409372Z","shell.execute_reply":"2025-04-20T16:04:45.413380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_predictions (a2):\n    return np.argmax(a2,0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:49.719035Z","iopub.execute_input":"2025-04-20T16:04:49.719370Z","iopub.status.idle":"2025-04-20T16:04:49.724864Z","shell.execute_reply.started":"2025-04-20T16:04:49.719348Z","shell.execute_reply":"2025-04-20T16:04:49.723646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_accuracy (predictions,y):\n    print(predictions,y)\n    return (np.sum(predictions == y) / y.size)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:50.148971Z","iopub.execute_input":"2025-04-20T16:04:50.149262Z","iopub.status.idle":"2025-04-20T16:04:50.154154Z","shell.execute_reply.started":"2025-04-20T16:04:50.149243Z","shell.execute_reply":"2025-04-20T16:04:50.153056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gradient_decent(x,y,iterations,alpha):\n    w1,b1,w2,b2 = initialize_para()\n    for i in range (iterations):\n        z1,a1,z2,a2= forword_propagation(w1,b1,w2,b2,x)\n        dw1,db1,dw2,db2=back_propagation(z1,a1,z2,a2,w2,x,y)\n        w1,b1,w2,b2=update_para(w1,b1,w2,b2,dw1,db1,dw2,db2,alpha)\n        if (i % 10 == 0):\n            print(\"iterations : \",i  )\n            print(\"accuarcy : \",get_accuracy(get_predictions(a2),y))\n    return w1,b1,w2,b2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:04:50.944898Z","iopub.execute_input":"2025-04-20T16:04:50.945271Z","iopub.status.idle":"2025-04-20T16:04:50.951652Z","shell.execute_reply.started":"2025-04-20T16:04:50.945246Z","shell.execute_reply":"2025-04-20T16:04:50.950535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"w1,b1,w2,b2=gradient_decent(x_train,y_train,500,0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-20T16:05:15.876448Z","iopub.execute_input":"2025-04-20T16:05:15.876792Z","iopub.status.idle":"2025-04-20T16:05:54.760357Z","shell.execute_reply.started":"2025-04-20T16:05:15.876766Z","shell.execute_reply":"2025-04-20T16:05:54.759604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"##### ","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}