{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"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 torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.autograd import Variable\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:17.685746Z","iopub.execute_input":"2024-03-02T22:26:17.686137Z","iopub.status.idle":"2024-03-02T22:26:23.339632Z","shell.execute_reply.started":"2024-03-02T22:26:17.686104Z","shell.execute_reply":"2024-03-02T22:26:23.338293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:23.341554Z","iopub.execute_input":"2024-03-02T22:26:23.342021Z","iopub.status.idle":"2024-03-02T22:26:27.317485Z","shell.execute_reply.started":"2024-03-02T22:26:23.341989Z","shell.execute_reply":"2024-03-02T22:26:27.316070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n#the most basic data preprocessing\nclass MNIST(Dataset):\n    def __init__(self, X, y=None):\n        self.X = X\n        self.y = y\n        \n    def __len__(self):\n        return len(self.X) #or self.data\n    \n    def __getitem__(self,idx):\n        if self.y is not None: #basically means if self.train is true, this diffs btw training and test dsets\n            return self.X[idx], self.y[idx]\n        self.X[idx]\n\"\"\"\n\nclass MNIST(Dataset):\n    def __init__(self, dataframe, train=True):\n        self.train = train\n        if train:\n            self.labels = torch.tensor(dataframe['label'].values.astype(np.int64))\n            dataframe.drop('label', axis = 1, inplace=True)\n        self.data = torch.reshape(torch.tensor(dataframe.values.astype(np.float32)), (-1, 1, 28, 28))/255\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, index):\n        input_data = self.data[index]\n        if self.train:\n            label = self.labels[index]\n            return input_data, label\n        return input_data","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:27.319263Z","iopub.execute_input":"2024-03-02T22:26:27.319719Z","iopub.status.idle":"2024-03-02T22:26:27.328684Z","shell.execute_reply.started":"2024-03-02T22:26:27.319677Z","shell.execute_reply":"2024-03-02T22:26:27.327219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = MNIST(train)\ntrain_set, val_set = train_test_split(train_dataset, test_size=0.3, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:27.330500Z","iopub.execute_input":"2024-03-02T22:26:27.330944Z","iopub.status.idle":"2024-03-02T22:26:28.106338Z","shell.execute_reply.started":"2024-03-02T22:26:27.330901Z","shell.execute_reply":"2024-03-02T22:26:28.104453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_iter = iter(val_set)\nimage, label = next(val_iter)\nimage.size()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:28.110221Z","iopub.execute_input":"2024-03-02T22:26:28.110682Z","iopub.status.idle":"2024-03-02T22:26:28.122016Z","shell.execute_reply.started":"2024-03-02T22:26:28.110642Z","shell.execute_reply":"2024-03-02T22:26:28.120790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_set, batch_size = 64, shuffle=True)\nval_loader = DataLoader(val_set, batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:28.123666Z","iopub.execute_input":"2024-03-02T22:26:28.124128Z","iopub.status.idle":"2024-03-02T22:26:28.134937Z","shell.execute_reply.started":"2024-03-02T22:26:28.124084Z","shell.execute_reply":"2024-03-02T22:26:28.132456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(1,8, kernel_size=(3,3),stride=2, padding=1) #8*14*14\n        self.act1 = nn.ReLU()\n        self.conv2 = nn.Conv2d(8,16,kernel_size=(3,3),stride=2, padding=1)#16*7*7\n        self.act2 = nn.ReLU()\n        self.conv3 = nn.Conv2d(16, 32,kernel_size=(3,3),stride=2, padding=1)#32*4*4\n        self.act3 = nn.ReLU()\n        self.conv4 = nn.Conv2d(32,64,kernel_size=(3,3),stride=2, padding=1)#64*2*2\n        self.act4 = nn.ReLU()\n        #self.pool2 = nn.MaxPool2d(kernel_size=(2,2)) #max pooling downsamples by factors of 2\n        self.flat = nn.Flatten()\n        self.fc1 = nn.Linear(256, 128)\n        self.act5 = nn.ReLU()\n        self.fc2 = nn.Linear(128, 10)\n        \n        \n        \n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.act1(x)\n        x = self.conv2(x)\n        x = self.act2(x)\n        x = self.conv3(x)\n        x = self.act3(x)\n        x = self.conv4(x)\n        x = self.act4(x)\n        x = self.flat(x)\n        x = self.fc1(x)\n        x = self.act5(x)\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:28.136841Z","iopub.execute_input":"2024-03-02T22:26:28.137773Z","iopub.status.idle":"2024-03-02T22:26:28.150349Z","shell.execute_reply.started":"2024-03-02T22:26:28.137701Z","shell.execute_reply":"2024-03-02T22:26:28.149214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CNN()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:28.152998Z","iopub.execute_input":"2024-03-02T22:26:28.154502Z","iopub.status.idle":"2024-03-02T22:26:28.184994Z","shell.execute_reply.started":"2024-03-02T22:26:28.154438Z","shell.execute_reply":"2024-03-02T22:26:28.183636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define loss fn and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:28.186496Z","iopub.execute_input":"2024-03-02T22:26:28.186984Z","iopub.status.idle":"2024-03-02T22:26:30.925274Z","shell.execute_reply.started":"2024-03-02T22:26:28.186951Z","shell.execute_reply":"2024-03-02T22:26:30.923894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write training loop\nepochs = 20\nsteps = 0\ntrain_losses, val_losses = [], []\nrunning_loss = 0 \nval_loss = 0 \n\nfor epoch in range(epochs):\n    model.train()\n    for batch_idx, (images, labels) in enumerate(train_loader):\n        steps += 1\n        images = Variable(images.view(-1,1,28,28))\n        labels = Variable(labels)\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        running_loss += loss.item()\n        loss.backward()\n        optimizer.step()\n        \n        total_train= 0\n        correct_train = 0\n        \n        _,predicted = torch.max(outputs.data, 1)\n        total_train += labels.size(0)\n        correct_train += (predicted == labels).sum().item()\n        train_acc = (correct_train/total_train) * 100\n        \n\n    with torch.no_grad():\n        model.eval() #can also be model.inference_mode or something\n        for data in val_loader:\n            images, labels = data[0], data[1]\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            val_loss += loss.item()\n            \n        correct_val = 0\n        total_val = 0\n        \n        _,predicted = torch.max(outputs.data, 1)\n        total_val += labels.size(0)\n        correct_val += (predicted == labels).sum().item()\n        val_acc = (correct_val/total_val) * 100\n        \n    train_losses.append(running_loss/total_train)\n    val_losses.append(val_loss/total_val)\n        \n    print(f\"Epoch [{epoch + 1}/{epochs}], Train loss: {running_loss/steps}, Train acc: {train_acc:.4f}, Val_loss: {val_loss/len(val_loader)} , Val_acc: {val_acc :.4f}\")","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:26:30.926578Z","iopub.execute_input":"2024-03-02T22:26:30.927142Z","iopub.status.idle":"2024-03-02T22:27:39.677592Z","shell.execute_reply.started":"2024-03-02T22:26:30.927111Z","shell.execute_reply":"2024-03-02T22:27:39.676240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntest_dataset = MNIST(test, train=False)\ntest_loader = DataLoader(test_dataset, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:32:09.936767Z","iopub.execute_input":"2024-03-02T22:32:09.938185Z","iopub.status.idle":"2024-03-02T22:32:11.054522Z","shell.execute_reply.started":"2024-03-02T22:32:09.938116Z","shell.execute_reply":"2024-03-02T22:32:11.053509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = []\n\nwith torch.no_grad():\n    for batch in test_loader:\n        preds = model(batch).cpu()\n        _,preds = torch.max(preds, 1)\n        for pred in preds.numpy():\n            y_test.append(pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:32:25.951707Z","iopub.execute_input":"2024-03-02T22:32:25.952122Z","iopub.status.idle":"2024-03-02T22:32:26.609597Z","shell.execute_reply.started":"2024-03-02T22:32:25.952092Z","shell.execute_reply":"2024-03-02T22:32:26.607884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsubmission[\"Label\"] = pd.Series(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:32:43.320475Z","iopub.execute_input":"2024-03-02T22:32:43.321898Z","iopub.status.idle":"2024-03-02T22:32:43.385324Z","shell.execute_reply.started":"2024-03-02T22:32:43.321852Z","shell.execute_reply":"2024-03-02T22:32:43.383448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T22:32:46.623228Z","iopub.execute_input":"2024-03-02T22:32:46.623637Z","iopub.status.idle":"2024-03-02T22:32:46.651847Z","shell.execute_reply.started":"2024-03-02T22:32:46.623609Z","shell.execute_reply":"2024-03-02T22:32:46.650174Z"},"trusted":true},"execution_count":null,"outputs":[]}]}