{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":242592,"sourceType":"datasetVersion","datasetId":102285}],"dockerImageVersionId":31153,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import Dataset\nimport torch\n\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/digit_recognizer'):\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-27T04:57:31.361254Z","iopub.execute_input":"2025-10-27T04:57:31.361953Z","iopub.status.idle":"2025-10-27T04:57:37.854858Z","shell.execute_reply.started":"2025-10-27T04:57:31.361926Z","shell.execute_reply":"2025-10-27T04:57:37.854137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:37.856228Z","iopub.execute_input":"2025-10-27T04:57:37.856620Z","iopub.status.idle":"2025-10-27T04:57:44.021904Z","shell.execute_reply.started":"2025-10-27T04:57:37.856591Z","shell.execute_reply":"2025-10-27T04:57:44.021281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn .model_selection import train_test_split\n\ntrain_df, test_df = train_test_split(df, train_size=0.8, random_state=42)\ntrain_df = train_df.to_numpy()\ntest_df = test_df.to_numpy()\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)\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:44.022585Z","iopub.execute_input":"2025-10-27T04:57:44.022856Z","iopub.status.idle":"2025-10-27T04:57:45.133110Z","shell.execute_reply.started":"2025-10-27T04:57:44.022834Z","shell.execute_reply":"2025-10-27T04:57:45.132302Z"}},"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))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.134061Z","iopub.execute_input":"2025-10-27T04:57:45.134481Z","iopub.status.idle":"2025-10-27T04:57:45.236139Z","shell.execute_reply.started":"2025-10-27T04:57:45.134456Z","shell.execute_reply":"2025-10-27T04:57:45.235475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train,x_test= x_train/255, x_test/255,\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.238193Z","iopub.execute_input":"2025-10-27T04:57:45.238409Z","iopub.status.idle":"2025-10-27T04:57:45.328971Z","shell.execute_reply.started":"2025-10-27T04:57:45.238393Z","shell.execute_reply":"2025-10-27T04:57:45.328169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"eps = 1e-6\ndef relu(x):\n    return x.clamp(min=0)\n\ndef log_softmax(x):\n    return x - torch.log(torch.exp(x).sum(dim=1, keepdim=True))\n\ndef nll_loss(log_probs, targets):\n    batch_size = log_probs.shape[0]\n    return -log_probs[range(batch_size), targets].mean()\n\ndef batch_norm(x, gamma):\n    batch_mean = x.mean(dim=0)\n    batch_var = (x-batch_mean).var(dim=0)\n    x_norm = (x-batch_mean)/torch.sqrt(batch_var + eps)\n    return gamma + x_norm\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.329761Z","iopub.execute_input":"2025-10-27T04:57:45.330017Z","iopub.status.idle":"2025-10-27T04:57:45.335612Z","shell.execute_reply.started":"2025-10-27T04:57:45.329989Z","shell.execute_reply":"2025-10-27T04:57:45.334884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 64\nEPOCH = 300\nEPOCH_PER_EVAL = 10\nlr = 1e-2\nhidden_features = 512","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.336389Z","iopub.execute_input":"2025-10-27T04:57:45.336599Z","iopub.status.idle":"2025-10-27T04:57:45.358256Z","shell.execute_reply.started":"2025-10-27T04:57:45.336584Z","shell.execute_reply":"2025-10-27T04:57:45.357606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, y_train, x_test, y_test = [t.to(device) for t in [x_train, y_train, x_test, y_test]]\nw1 = torch.distributions.Uniform(-np.sqrt(1/784), np.sqrt(1/784)).sample((hidden_features, 784)).to(device).requires_grad_()\nb1 = torch.distributions.Uniform(-np.sqrt(1/784), np.sqrt(1/784)).sample((hidden_features, )).to(device).requires_grad_()\nw2 = torch.distributions.Uniform(-np.sqrt(1/hidden_features), np.sqrt(1/hidden_features)).sample((hidden_features, hidden_features)).to(device).requires_grad_()\nb2 = torch.distributions.Uniform(-np.sqrt(1/hidden_features), np.sqrt(1/hidden_features)).sample((hidden_features, )).to(device).requires_grad_()\nw3 = torch.distributions.Uniform(-np.sqrt(1/hidden_features), np.sqrt(1/hidden_features)).sample((10, hidden_features)).to(device).requires_grad_()\nb3 = torch.distributions.Uniform(-np.sqrt(1/hidden_features), np.sqrt(1/hidden_features)).sample((10, )).to(device).requires_grad_()\n\nweights = [w1,w2,w3]\nbias = [b1,b2,b3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.358854Z","iopub.execute_input":"2025-10-27T04:57:45.359080Z","iopub.status.idle":"2025-10-27T04:57:45.678773Z","shell.execute_reply.started":"2025-10-27T04:57:45.359057Z","shell.execute_reply":"2025-10-27T04:57:45.678110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MNIST(Dataset):\n    def __init__(self,x,y):\n        self.x = x\n        self.y = y\n        self.n = len(x)\n\n    def __len__(self):\n        return self.n\n    def __getitem__(self,idx):\n        return self.x[idx], self.y[idx]\n\ntrain_ds = MNIST(x_train, y_train)\ntest_ds = MNIST(x_test, y_test)\ntrain_dl = DataLoader(train_ds,batch_size = BATCH_SIZE)\ntest_dl = DataLoader(test_ds,batch_size = BATCH_SIZE)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.679437Z","iopub.execute_input":"2025-10-27T04:57:45.679622Z","iopub.status.idle":"2025-10-27T04:57:45.685071Z","shell.execute_reply.started":"2025-10-27T04:57:45.679607Z","shell.execute_reply":"2025-10-27T04:57:45.684293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def model(input, weights, bias):\n    temp = input\n    for i in range(len(weights)-1):\n        temp = relu(temp @ weights[i].t() + bias[i])\n    temp = temp @ weights[-1].t() + bias[-1]\n    return log_softmax(temp)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.685760Z","iopub.execute_input":"2025-10-27T04:57:45.686010Z","iopub.status.idle":"2025-10-27T04:57:45.701762Z","shell.execute_reply.started":"2025-10-27T04:57:45.685990Z","shell.execute_reply":"2025-10-27T04:57:45.701018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lp = model(train_ds.x[0].unsqueeze(0),weights,bias)\nnll_loss(lp,train_ds.y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:45.702434Z","iopub.execute_input":"2025-10-27T04:57:45.702730Z","iopub.status.idle":"2025-10-27T04:57:46.601666Z","shell.execute_reply.started":"2025-10-27T04:57:45.702708Z","shell.execute_reply":"2025-10-27T04:57:46.600878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for epoch in range(EPOCH):\n    for i, (xb, yb) in enumerate(train_dl):\n        \n        xb, yb = xb.to(device), yb.to(device)\n        log_probs = model(xb, weights, bias)\n        loss = nll_loss(log_probs, yb)\n        loss.backward()\n        \n        with torch.no_grad(): \n            for param in weights:\n                param -= param.grad * lr\n            for param in bias:\n                param -= param.grad * lr\n                \n            for param in weights:\n                param.grad.zero_()\n            for param in bias:\n                param.grad.zero_()\n    \n\n    if (epoch + 1) % EPOCH_PER_EVAL == 0:\n        total_correct = 0\n        total_samples = 0\n        \n        with torch.no_grad():\n            for xb_test, yb_test in test_dl:\n                xb_test, yb_test = xb_test.to(device), yb_test.to(device)\n                \n                log_probs = model(xb_test, weights, bias)\n                preds = torch.argmax(log_probs, dim=1)\n                total_samples += yb_test.size(0)\n                total_correct += (preds == yb_test).sum().item()\n        \n        accuracy = 100 * total_correct / total_samples\n        print(f'Epoch [{epoch+1}/{EPOCH}], Loss: {loss.item():.4f}, Test Accuracy: {accuracy:.2f}%')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T04:57:46.602472Z","iopub.execute_input":"2025-10-27T04:57:46.602683Z","iopub.status.idle":"2025-10-27T05:03:32.778531Z","shell.execute_reply.started":"2025-10-27T04:57:46.602668Z","shell.execute_reply":"2025-10-27T05:03:32.777715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"raw","source":" ","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}