{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nplt.style.use('grayscale')\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:14:36.698372Z","iopub.execute_input":"2024-01-07T15:14:36.698676Z","iopub.status.idle":"2024-01-07T15:14:43.080941Z","shell.execute_reply.started":"2024-01-07T15:14:36.698649Z","shell.execute_reply":"2024-01-07T15:14:43.080131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:16:49.117451Z","iopub.execute_input":"2024-01-07T15:16:49.117872Z","iopub.status.idle":"2024-01-07T15:16:49.181528Z","shell.execute_reply.started":"2024-01-07T15:16:49.117831Z","shell.execute_reply":"2024-01-07T15:16:49.180341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '/kaggle/input/digit-recognizer/test.csv'\ntrain_path = '/kaggle/input/digit-recognizer/train.csv'\nsubmission_path = '/kaggle/input/digit-recognizer/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:16:49.960909Z","iopub.execute_input":"2024-01-07T15:16:49.961264Z","iopub.status.idle":"2024-01-07T15:16:49.965437Z","shell.execute_reply.started":"2024-01-07T15:16:49.961233Z","shell.execute_reply":"2024-01-07T15:16:49.964529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_path)\ntest_df = pd.read_csv(test_path)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:16:50.562716Z","iopub.execute_input":"2024-01-07T15:16:50.563022Z","iopub.status.idle":"2024-01-07T15:16:55.970854Z","shell.execute_reply.started":"2024-01-07T15:16:50.562998Z","shell.execute_reply":"2024-01-07T15:16:55.969879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(torch.utils.data.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-01-07T15:16:55.972866Z","iopub.execute_input":"2024-01-07T15:16:55.973209Z","iopub.status.idle":"2024-01-07T15:16:55.981171Z","shell.execute_reply.started":"2024-01-07T15:16:55.973180Z","shell.execute_reply":"2024-01-07T15:16:55.980024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = Dataset(train_df)\n\ntrain_set, val_set = torch.utils.data.random_split(train_dataset, [int(len(train_dataset)*0.8), int(len(train_dataset)*0.2)])\n\ntrain_loader = DataLoader(train_set, batch_size=64, shuffle=True)\nval_loader = DataLoader(val_set, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:16:55.982660Z","iopub.execute_input":"2024-01-07T15:16:55.983350Z","iopub.status.idle":"2024-01-07T15:16:56.350240Z","shell.execute_reply.started":"2024-01-07T15:16:55.983301Z","shell.execute_reply":"2024-01-07T15:16:56.349353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_set), len(val_set))","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:16:56.352604Z","iopub.execute_input":"2024-01-07T15:16:56.353173Z","iopub.status.idle":"2024-01-07T15:16:56.358795Z","shell.execute_reply.started":"2024-01-07T15:16:56.353130Z","shell.execute_reply":"2024-01-07T15:16:56.357796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ConvModel(nn.Module):\n    def __init__(self):\n        super(ConvModel, self).__init__()\n        self.conv1 = nn.Conv2d(1, 16, 3)\n        self.relu = nn.ReLU()\n        self.maxpool = nn.MaxPool2d(2)\n        self.batch_norm1 = nn.BatchNorm2d(16)\n        self.conv2 = nn.Conv2d(16, 32, 3)\n        self.batch_norm2 = nn.BatchNorm2d(32)\n        self.conv3 = nn.Conv2d(32, 64, 3)\n        self.dropout = nn.Dropout(p=0.3)\n        self.flatten = nn.Flatten()\n        self.fc1 = nn.Linear(64*3*3, 100)\n        self.fc2 = nn.Linear(100, 10)\n\n    def forward(self, x):\n        x = self.relu(self.conv1(x))\n        x = self.batch_norm1(self.maxpool(x))\n        x = self.dropout(x)\n        x = self.relu(self.conv2(x))\n        x = self.batch_norm2(self.maxpool(x))\n        x = self.dropout(x)\n        x = self.relu(self.conv3(x))\n        x = self.flatten(x)\n        x = self.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:37:06.132052Z","iopub.execute_input":"2024-01-07T15:37:06.132489Z","iopub.status.idle":"2024-01-07T15:37:06.143131Z","shell.execute_reply.started":"2024-01-07T15:37:06.132429Z","shell.execute_reply":"2024-01-07T15:37:06.142114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ConvModel().to(device)\ncriterion = nn.CrossEntropyLoss()\n#optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)\noptimizer = torch.optim.SGD(model.parameters(), lr=8e-3, momentum=0.9)\nscheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=6, gamma=0.4)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:37:06.144534Z","iopub.execute_input":"2024-01-07T15:37:06.144933Z","iopub.status.idle":"2024-01-07T15:37:06.158167Z","shell.execute_reply.started":"2024-01-07T15:37:06.144895Z","shell.execute_reply":"2024-01-07T15:37:06.157353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 30\nfor epoch in range(epochs):\n    model.train()  # Set the model to training mode\n    for data in train_loader:\n        inputs, labels = data[0].to(device), data[1].to(device)\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n    # Validation\n    model.eval()  # Set the model to evaluation mode\n    val_loss = 0.0\n    correct = 0\n    total = 0\n\n    with torch.no_grad():\n        for data in val_loader:\n            val_inputs, val_labels = data[0].to(device), data[1].to(device)\n            val_outputs = model(val_inputs)\n            val_loss += criterion(val_outputs, val_labels).item()\n\n            _, predicted = torch.max(val_outputs.data, 1)\n            total += val_labels.size(0)\n            correct += (predicted == val_labels).sum().item()\n\n    average_val_loss = val_loss / len(val_loader)\n    val_accuracy = correct / total\n\n    print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}, Val Loss: {average_val_loss:.4f}, Val Acc: {val_accuracy * 100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:37:13.411144Z","iopub.execute_input":"2024-01-07T15:37:13.411947Z","iopub.status.idle":"2024-01-07T15:38:24.313348Z","shell.execute_reply.started":"2024-01-07T15:37:13.411907Z","shell.execute_reply":"2024-01-07T15:38:24.312358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Dataset(test_df, train=False)\n\ntest_loader = DataLoader(test_dataset, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:38:28.151669Z","iopub.execute_input":"2024-01-07T15:38:28.152036Z","iopub.status.idle":"2024-01-07T15:38:28.263000Z","shell.execute_reply.started":"2024-01-07T15:38:28.152005Z","shell.execute_reply":"2024-01-07T15:38:28.262166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(test_loader)\nimages = next(dataiter)\n\nplt.imshow(images[10,0]);","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:38:29.658429Z","iopub.execute_input":"2024-01-07T15:38:29.659083Z","iopub.status.idle":"2024-01-07T15:38:29.990439Z","shell.execute_reply.started":"2024-01-07T15:38:29.659046Z","shell.execute_reply":"2024-01-07T15:38:29.989461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = []\nwith torch.no_grad():\n    for batch in test_loader:\n        predictions = model(batch.to(device)).cpu()\n        _, predictions = torch.max(predictions, 1)\n        for prediction in predictions.numpy():\n            y_test.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:38:34.003529Z","iopub.execute_input":"2024-01-07T15:38:34.003913Z","iopub.status.idle":"2024-01-07T15:38:34.635161Z","shell.execute_reply.started":"2024-01-07T15:38:34.003879Z","shell.execute_reply":"2024-01-07T15:38:34.634336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label'] = pd.Series(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:38:35.739889Z","iopub.execute_input":"2024-01-07T15:38:35.740720Z","iopub.status.idle":"2024-01-07T15:38:35.888627Z","shell.execute_reply.started":"2024-01-07T15:38:35.740685Z","shell.execute_reply":"2024-01-07T15:38:35.887877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-07T15:38:48.903145Z","iopub.execute_input":"2024-01-07T15:38:48.903987Z","iopub.status.idle":"2024-01-07T15:38:48.970766Z","shell.execute_reply.started":"2024-01-07T15:38:48.903948Z","shell.execute_reply":"2024-01-07T15:38:48.969842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}