{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ndataset_train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nXtrain=dataset_train.iloc[:,1:]\nYtrain=dataset_train.iloc[:,0]\n\n#print(Ytrain.head())\nx_train=np.array(np.divide(Xtrain,255))\ny_train=np.array([[1 if i==j else 0 for i in range(10)] for j in Ytrain])\n\ndef train(x_train,y_train,epoch) :\n\n    def relu(x):\n        return np.maximum(x, 0)\n\n    def softmax(x):\n        ex=np.exp(x/np.max(x))\n        su=np.array([np.sum(ex,axis=1)]).T\n        return ex/su\n\n    w1 = np.random.normal(0, 1, (784, 16)) * np.sqrt(2.0 / (784 + 16))\n    w2 = np.random.normal(0, 1, (16, 16)) * np.sqrt(2.0 / (16 + 16))\n    w3 = np.random.normal(0, 1, (16, 10)) * np.sqrt(2.0 / (16 + 10))\n\n\n    for i in range(epoch):\n\n        batch = 30\n        c=batch\n        lr = 0.001\n        b = 0\n\n        for j in range(len(x_train) // batch):\n            (x,y) = (x_train[b:c, ], y_train[b:c, ])\n\n            b = b + batch\n            c = c + batch\n\n            z1 = np.dot(x, w1)\n            a1 = relu(z1)\n\n            z2 = np.dot(a1, w2)\n            a2 = relu(z2)\n            \n            z3 = np.dot(a2, w3)\n            a3 = softmax(z3)\n\n            loss = (a3 - y) ** 2\n\n            \n            \n            dloss = (a3 - y) * 2\n            dsoft = z3 * (1 - z3)\n            \n            dz3a2 = w3.T\n            dz3w3 = a2.T\n            \n            da2 = np.where(z2 > 0, 1, 0)\n            \n            dz2w2 = a1.T\n            dz2a1 = w2.T\n            \n            da1 = np.where(z1 > 0, 1, 0)\n            dz1w1 = x.T\n\n            dw1 = (np.dot(dz1w1, ((np.dot(((np.dot((dloss * dsoft), dz3a2))* da2), dz2a1)) * da1)) )/ batch\n            dw2 = np.dot(dz2w2, (np.dot((dloss * dsoft), dz3a2) * da2)) / batch\n            dw3 = np.dot(dz3w3, dloss * dsoft) / batch\n\n            w1 = w1 - (lr * dw1)\n            w2 = w2 - (lr * dw2)\n            w3 = w3 - (lr * dw3)\n\n    return w1,w2,w3\n\n\nw1,w2,w3=train(x_train=x_train,y_train=y_train,epoch=5)\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-18T08:06:29.044034Z","iopub.execute_input":"2024-03-18T08:06:29.044725Z","iopub.status.idle":"2024-03-18T08:06:33.754952Z","shell.execute_reply.started":"2024-03-18T08:06:29.044694Z","shell.execute_reply":"2024-03-18T08:06:33.754153Z"},"trusted":true},"execution_count":null,"outputs":[]}]}