{"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":31153,"isInternetEnabled":false,"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-10-27T19:26:31.426330Z","iopub.execute_input":"2025-10-27T19:26:31.426802Z","iopub.status.idle":"2025-10-27T19:26:31.846675Z","shell.execute_reply.started":"2025-10-27T19:26:31.426768Z","shell.execute_reply":"2025-10-27T19:26:31.845953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch import optim\nfrom torch.utils.data import DataLoader, Dataset\nimport os \ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:31.847842Z","iopub.execute_input":"2025-10-27T19:26:31.848219Z","iopub.status.idle":"2025-10-27T19:26:33.822510Z","shell.execute_reply.started":"2025-10-27T19:26:31.848191Z","shell.execute_reply":"2025-10-27T19:26:33.821565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"GPU available: {device if torch.cuda.is_available() else 'NO' }\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:33.823544Z","iopub.execute_input":"2025-10-27T19:26:33.824021Z","iopub.status.idle":"2025-10-27T19:26:33.828784Z","shell.execute_reply.started":"2025-10-27T19:26:33.823993Z","shell.execute_reply":"2025-10-27T19:26:33.827906Z"}},"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-10-27T19:26:33.830803Z","iopub.execute_input":"2025-10-27T19:26:33.831106Z","iopub.status.idle":"2025-10-27T19:26:36.185790Z","shell.execute_reply.started":"2025-10-27T19:26:33.831086Z","shell.execute_reply":"2025-10-27T19:26:36.184870Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"Splitting dataset into train and test data\"\nfrom sklearn.model_selection import train_test_split\n\ntrain_df , test_df = train_test_split(data,train_size=0.8,random_state=42)\ntrain_df = train_df.to_numpy()\ntest_df=test_df.to_numpy()\n\ny_train , x_train= train_df[:,0] ,train_df[:,1:].astype(np.float32)\ny_test, x_test = test_df[:, 0], test_df[:, 1:].astype(np.float32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:36.186693Z","iopub.execute_input":"2025-10-27T19:26:36.186940Z","iopub.status.idle":"2025-10-27T19:26:36.934843Z","shell.execute_reply.started":"2025-10-27T19:26:36.186917Z","shell.execute_reply":"2025-10-27T19:26:36.934170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, y_train, x_test, y_test = map(torch.tensor, (x_train, y_train, x_test, y_test))\nx_train, y_train, x_test, y_test = [t.to(device) for t in [x_train, y_train, x_test, y_test]]\n\nx_train/=255.0\nx_test/=255.0\nfrom torch.utils.data import TensorDataset, DataLoader\n\ntrain_ds = TensorDataset(x_train, y_train)\ntest_ds  = TensorDataset(x_test,  y_test)\n\ntrain_dl = DataLoader(train_ds, batch_size=64, shuffle=True, num_workers=2, pin_memory=True)\ntest_dl  = DataLoader(test_ds,  batch_size=64, shuffle=False, num_workers=2, pin_memory=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:36.935703Z","iopub.execute_input":"2025-10-27T19:26:36.936123Z","iopub.status.idle":"2025-10-27T19:26:37.024633Z","shell.execute_reply.started":"2025-10-27T19:26:36.936099Z","shell.execute_reply":"2025-10-27T19:26:37.023925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"Writing Helper Functions\"\nepsilon=1e-6\ndef relu(x):\n    return x.clamp(min=0)\n\ndef batch_norm(x, gamma) :\n    batch_mean = x.mean(dim=0)\n    batch_var = (x).var(dim=0)\n    x_norm = (x - batch_mean) / torch.sqrt(batch_var + eps)\n    return x_norm\n\n\"give x tensor\"\ndef softmax(x):\n    y = torch.exp(x)\n    den_sum= torch.sum(y, dim=1)\n    return y/sum","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.025818Z","iopub.execute_input":"2025-10-27T19:26:37.026103Z","iopub.status.idle":"2025-10-27T19:26:37.031305Z","shell.execute_reply.started":"2025-10-27T19:26:37.026081Z","shell.execute_reply":"2025-10-27T19:26:37.030499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Regression():\n    def __init__(self , inp_shape, out_shape):\n        self.w = torch.distributions.Uniform(-np.sqrt(1/784), np.sqrt(1/784)).sample((out_shape, inp_shape)).to(device).requires_grad_()\n        self.b = torch.distributions.Uniform(-np.sqrt(1/784), np.sqrt(1/784)).sample((out_shape, )).to(device).requires_grad_()\n\n    def __call__(self,x):\n        return x@self.w.t()+self.b","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.032173Z","iopub.execute_input":"2025-10-27T19:26:37.033060Z","iopub.status.idle":"2025-10-27T19:26:37.044618Z","shell.execute_reply.started":"2025-10-27T19:26:37.033030Z","shell.execute_reply":"2025-10-27T19:26:37.043938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Neural_Network():\n    def __init__(self, hidden):\n        self.hidden_features=hidden\n        self.lr1=Regression(784,self.hidden_features)\n        self.lr2=Regression(self.hidden_features,self.hidden_features)\n        self.lr3=Regression(self.hidden_features,10)\n        self.params = [self.lr1.w, self.lr2.w, self.lr3.w, self.lr1.b, self.lr2.b, self.lr3.b]\n\n    def __call__(self,x):\n        input=x\n        y1 = relu(self.lr1(input))  #Using the call function inside Regression Class\"\n        y2= relu(self.lr2(y1))\n        y3= self.lr3(y2)\n        return y3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.045518Z","iopub.execute_input":"2025-10-27T19:26:37.046350Z","iopub.status.idle":"2025-10-27T19:26:37.055675Z","shell.execute_reply.started":"2025-10-27T19:26:37.046328Z","shell.execute_reply":"2025-10-27T19:26:37.054918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def cross_entropy(logits, y):\n    logprob = torch.log_softmax(logits, dim=1)\n\n    loss = - logprob[torch.arange(len(y)),y].mean()\n    \n    return loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.058066Z","iopub.execute_input":"2025-10-27T19:26:37.058305Z","iopub.status.idle":"2025-10-27T19:26:37.068416Z","shell.execute_reply.started":"2025-10-27T19:26:37.058287Z","shell.execute_reply":"2025-10-27T19:26:37.067690Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 64\nepochs = 200\nEpoch_eval = 5\nlr = 1e-3\nhidden = 512","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.069094Z","iopub.execute_input":"2025-10-27T19:26:37.069299Z","iopub.status.idle":"2025-10-27T19:26:37.079336Z","shell.execute_reply.started":"2025-10-27T19:26:37.069282Z","shell.execute_reply":"2025-10-27T19:26:37.078577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def accuracy(model, data) :\n    acc = 0\n    c = 0\n    for x, y in data :\n        ypred = model(x)\n        yhat = torch.argmax(ypred, -1)\n        acc += (y == yhat).to(float).mean()\n        c+=1\n    acc = acc / c\n    return acc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.080010Z","iopub.execute_input":"2025-10-27T19:26:37.080225Z","iopub.status.idle":"2025-10-27T19:26:37.091091Z","shell.execute_reply.started":"2025-10-27T19:26:37.080208Z","shell.execute_reply":"2025-10-27T19:26:37.090404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MNIST = Neural_Network(hidden)\n\nfor i in range(epochs):\n    for x,y in train_dl:\n        y_pred=MNIST(x)\n\n        loss = cross_entropy(y_pred,y)\n\n        loss.backward() # This calculates the gradient for backward propagation\n\n        with torch.no_grad():\n            for j in range(len(MNIST.params)):\n                MNIST.params[j]-= lr*MNIST.params[j].grad # THe gradient Descent Step\n\n        for j in range(len(MNIST.params)):\n            MNIST.params[j].grad.zero_()\n\n    if (i+1)%Epoch_eval==0:\n        print(f\"Epoch {i+1}, Loss = {loss} , Accuracy = {accuracy(MNIST,test_dl)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:26:37.091776Z","iopub.execute_input":"2025-10-27T19:26:37.091998Z","iopub.status.idle":"2025-10-27T19:37:44.529814Z","shell.execute_reply.started":"2025-10-27T19:26:37.091981Z","shell.execute_reply":"2025-10-27T19:37:44.528697Z"}},"outputs":[],"execution_count":null}]}