{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":30043,"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\nimport os\n\nimport torch\nimport torch.nn as nn\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:18.479778Z","iopub.execute_input":"2026-05-21T14:39:18.480136Z","iopub.status.idle":"2026-05-21T14:39:18.904022Z","shell.execute_reply.started":"2026-05-21T14:39:18.480106Z","shell.execute_reply":"2026-05-21T14:39:18.903122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH = 32\nEPOCHS = 8\nLR = 0.001\n\nDEVICE = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:18.908894Z","iopub.execute_input":"2026-05-21T14:39:18.909235Z","iopub.status.idle":"2026-05-21T14:39:18.938588Z","shell.execute_reply.started":"2026-05-21T14:39:18.909200Z","shell.execute_reply":"2026-05-21T14:39:18.937708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\", dtype=np.float32)\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\", dtype=np.float32)\nsample_sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\n\ntrain_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:18.939687Z","iopub.status.idle":"2026-05-21T14:39:22.782206Z","shell.execute_reply.started":"2026-05-21T14:39:18.940056Z","shell.execute_reply":"2026-05-21T14:39:22.781226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:22.783586Z","iopub.execute_input":"2026-05-21T14:39:22.783954Z","iopub.status.idle":"2026-05-21T14:39:22.810818Z","shell.execute_reply.started":"2026-05-21T14:39:22.783897Z","shell.execute_reply":"2026-05-21T14:39:22.810033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_Train = train_df.label.values\nX0 = train_df.loc[:, train_df.columns != 'label'].values/255\n\nX0.shape, Y_Train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:22.812308Z","iopub.execute_input":"2026-05-21T14:39:22.812652Z","iopub.status.idle":"2026-05-21T14:39:22.899081Z","shell.execute_reply.started":"2026-05-21T14:39:22.812617Z","shell.execute_reply":"2026-05-21T14:39:22.898187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_Train = torch.from_numpy(Y_Train).type(torch.LongTensor)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:22.902850Z","iopub.execute_input":"2026-05-21T14:39:22.903156Z","iopub.status.idle":"2026-05-21T14:39:22.907352Z","shell.execute_reply.started":"2026-05-21T14:39:22.903130Z","shell.execute_reply":"2026-05-21T14:39:22.906579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_Train_1 = X0.reshape((-1, 1, 28, 28))\nX_Train_1.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:22.908787Z","iopub.execute_input":"2026-05-21T14:39:22.909096Z","iopub.status.idle":"2026-05-21T14:39:22.920347Z","shell.execute_reply.started":"2026-05-21T14:39:22.909068Z","shell.execute_reply":"2026-05-21T14:39:22.919406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_Train = np.stack((X_Train_1,)*3, axis=1)\nX_Train = np.squeeze(X_Train, axis=2)\nX_Train.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:22.921389Z","iopub.execute_input":"2026-05-21T14:39:22.921653Z","iopub.status.idle":"2026-05-21T14:39:23.125810Z","shell.execute_reply.started":"2026-05-21T14:39:22.921628Z","shell.execute_reply":"2026-05-21T14:39:23.125019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# split data into train and validation\nX_train, X_valid, y_train, y_valid = train_test_split(\n    X_Train,\n    Y_Train,\n    test_size=0.2,\n    random_state=0,\n    stratify=Y_Train\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:23.126963Z","iopub.execute_input":"2026-05-21T14:39:23.127180Z","iopub.status.idle":"2026-05-21T14:39:24.615398Z","shell.execute_reply.started":"2026-05-21T14:39:23.127158Z","shell.execute_reply":"2026-05-21T14:39:24.614523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Transform = transforms.Compose(\n    [transforms.ToTensor()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:24.616723Z","iopub.execute_input":"2026-05-21T14:39:24.617028Z","iopub.status.idle":"2026-05-21T14:39:24.621325Z","shell.execute_reply.started":"2026-05-21T14:39:24.616998Z","shell.execute_reply":"2026-05-21T14:39:24.620313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class GetData(Dataset):\n    def __init__(self, X_Train, Y_Train, Transform):\n        self.X = X_Train\n        self.transform = Transform\n        self.Y = Y_Train\n        \n    def __len__(self):\n        return len(self.X)\n\n    def __getitem__(self, index):\n        return self.transform(self.X[index]).permute((1, 2, 0)).contiguous(), self.Y[index]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:24.622309Z","iopub.execute_input":"2026-05-21T14:39:24.622604Z","iopub.status.idle":"2026-05-21T14:39:24.637147Z","shell.execute_reply.started":"2026-05-21T14:39:24.622577Z","shell.execute_reply":"2026-05-21T14:39:24.636222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainset = GetData(X_train, y_train, Transform)\nvalidset = GetData(X_valid, y_valid, Transform)\n\ntrainloader = DataLoader(\n    trainset,\n    batch_size=BATCH,\n    shuffle=True,\n    num_workers=4\n)\n\nvalidloader = DataLoader(\n    validset,\n    batch_size=BATCH,\n    shuffle=False,\n    num_workers=4\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:24.638395Z","iopub.execute_input":"2026-05-21T14:39:24.638894Z","iopub.status.idle":"2026-05-21T14:39:24.651314Z","shell.execute_reply.started":"2026-05-21T14:39:24.638853Z","shell.execute_reply":"2026-05-21T14:39:24.650411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"next(iter(trainloader))[0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:24.652671Z","iopub.execute_input":"2026-05-21T14:39:24.652967Z","iopub.status.idle":"2026-05-21T14:39:24.775305Z","shell.execute_reply.started":"2026-05-21T14:39:24.652936Z","shell.execute_reply":"2026-05-21T14:39:24.774450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = torchvision.models.resnet152()\n# adding a layer\nmodel.fc = nn.Linear(2048, 10, bias=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:24.776785Z","iopub.execute_input":"2026-05-21T14:39:24.777092Z","iopub.status.idle":"2026-05-21T14:39:25.949682Z","shell.execute_reply.started":"2026-05-21T14:39:24.777060Z","shell.execute_reply":"2026-05-21T14:39:25.949007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = model.to(DEVICE)\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=LR)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:25.951076Z","iopub.execute_input":"2026-05-21T14:39:25.951445Z","iopub.status.idle":"2026-05-21T14:39:27.999112Z","shell.execute_reply.started":"2026-05-21T14:39:25.951405Z","shell.execute_reply":"2026-05-21T14:39:27.998406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_score\nimport torch\n\nfor epoch in range(EPOCHS):\n\n    # ======================\n    # TRAINING\n    # ======================\n    model.train()\n\n    train_loss = 0.0\n    train_correct = 0\n    train_total = 0\n\n    train_preds = []\n    train_labels = []\n\n    for images, labels in trainloader:\n\n        images = images.to(DEVICE)\n        labels = labels.to(DEVICE)\n\n        logits = model(images)\n\n        loss = criterion(logits, labels)\n\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n\n        preds = torch.argmax(logits, dim=1)\n\n        train_correct += (preds == labels).sum().item()\n        train_total += labels.size(0)\n\n        train_preds.extend(preds.cpu().numpy())\n        train_labels.extend(labels.cpu().numpy())\n\n    train_acc = train_correct / train_total\n\n    train_precision = precision_score(\n        train_labels,\n        train_preds,\n        average='macro'\n    )\n\n    # ======================\n    # VALIDATION FOR CHECKING OVERFITTING / UNDERFITTING\n    # ======================\n    model.eval()\n\n    valid_loss = 0.0\n    valid_correct = 0\n    valid_total = 0\n\n    valid_preds = []\n    valid_labels = []\n\n    with torch.no_grad():\n\n        for images, labels in validloader:\n\n            images = images.to(DEVICE)\n            labels = labels.to(DEVICE)\n\n            logits = model(images)\n\n            loss = criterion(logits, labels)\n\n            valid_loss += loss.item()\n\n            preds = torch.argmax(logits, dim=1)\n\n            valid_correct += (preds == labels).sum().item()\n            valid_total += labels.size(0)\n\n            valid_preds.extend(preds.cpu().numpy())\n            valid_labels.extend(labels.cpu().numpy())\n\n    valid_acc = valid_correct / valid_total\n\n    valid_precision = precision_score(\n        valid_labels,\n        valid_preds,\n        average='macro'\n    )\n\n    # ======================\n    # PRINT RESULT\n    # ======================\n    print(\n        f'Epoch [{epoch+1}/{EPOCHS}] '\n        f'| Train Loss: {train_loss/len(trainloader):.4f} '\n        f'| Train Acc: {train_acc:.4f} '\n        f'| Train Precision: {train_precision:.4f} '\n        f'| Val Loss: {valid_loss/len(validloader):.4f} '\n        f'| Val Acc: {valid_acc:.4f} '\n        f'| Val Precision: {valid_precision:.4f}'\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:39:28.000253Z","iopub.execute_input":"2026-05-21T14:39:28.000500Z","iopub.status.idle":"2026-05-21T14:52:23.307685Z","shell.execute_reply.started":"2026-05-21T14:39:28.000454Z","shell.execute_reply":"2026-05-21T14:52:23.306651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Xt_1 = (test_df.values/255).reshape((-1, 1, 28, 28))\nXt_1.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:52:23.309353Z","iopub.execute_input":"2026-05-21T14:52:23.309647Z","iopub.status.idle":"2026-05-21T14:52:23.344340Z","shell.execute_reply.started":"2026-05-21T14:52:23.309616Z","shell.execute_reply":"2026-05-21T14:52:23.343518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Xt = np.stack((Xt_1,)*3, axis=1)\nXt = np.squeeze(Xt, axis=2)\nXt.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:52:23.345646Z","iopub.execute_input":"2026-05-21T14:52:23.345873Z","iopub.status.idle":"2026-05-21T14:52:23.480209Z","shell.execute_reply.started":"2026-05-21T14:52:23.345851Z","shell.execute_reply":"2026-05-21T14:52:23.479396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with torch.no_grad():\n    model.eval()\n    sample_sub['Label'] = model(torch.from_numpy(Xt).to(DEVICE)).cpu().argmax(dim=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:52:23.481583Z","iopub.execute_input":"2026-05-21T14:52:23.481848Z","iopub.status.idle":"2026-05-21T14:52:31.501274Z","shell.execute_reply.started":"2026-05-21T14:52:23.481823Z","shell.execute_reply":"2026-05-21T14:52:31.500247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_sub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:52:31.503094Z","iopub.execute_input":"2026-05-21T14:52:31.503488Z","iopub.status.idle":"2026-05-21T14:52:31.512661Z","shell.execute_reply.started":"2026-05-21T14:52:31.503445Z","shell.execute_reply":"2026-05-21T14:52:31.511896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_sub.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-21T14:52:31.513774Z","iopub.execute_input":"2026-05-21T14:52:31.514052Z","iopub.status.idle":"2026-05-21T14:52:31.944136Z","shell.execute_reply.started":"2026-05-21T14:52:31.514026Z","shell.execute_reply":"2026-05-21T14:52:31.943441Z"}},"outputs":[],"execution_count":null}]}