{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport torch.optim as optim\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset\nfrom sklearn.model_selection import train_test_split\nfrom torchvision import transforms\nfrom sklearn.preprocessing import StandardScaler\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-14T11:24:54.363357Z","iopub.execute_input":"2024-11-14T11:24:54.363841Z","iopub.status.idle":"2024-11-14T11:25:00.640375Z","shell.execute_reply.started":"2024-11-14T11:24:54.363776Z","shell.execute_reply":"2024-11-14T11:25:00.638858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = '/kaggle/input/digit-recognizer/train.csv'\ntest_data = '/kaggle/input/digit-recognizer/test.csv'\n\ntrain_df = pd.read_csv(train_data)\ntest_df = pd.read_csv(test_data)\nlabel = train_df['label'].copy()\ntrain_df.drop(columns=['label'], inplace=True)\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:02.632712Z","iopub.execute_input":"2024-11-14T11:25:02.633352Z","iopub.status.idle":"2024-11-14T11:25:09.828471Z","shell.execute_reply.started":"2024-11-14T11:25:02.633309Z","shell.execute_reply":"2024-11-14T11:25:09.827189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_data(data, n):\n    fig, axes = plt.subplots(ncols=n, figsize=(10,2))\n    for i in range(n):\n        pixels = data.loc[i, 'pixel0':'pixel783'].values.reshape((28,28))\n        axes[i].imshow(pixels, cmap='gray')\n        axes[i].set_title(label[i])\n        axes[i].axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:09.830446Z","iopub.execute_input":"2024-11-14T11:25:09.830826Z","iopub.status.idle":"2024-11-14T11:25:09.838737Z","shell.execute_reply.started":"2024-11-14T11:25:09.830781Z","shell.execute_reply":"2024-11-14T11:25:09.837349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_data(train_df, 5)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:09.840509Z","iopub.execute_input":"2024-11-14T11:25:09.840967Z","iopub.status.idle":"2024-11-14T11:25:10.290538Z","shell.execute_reply.started":"2024-11-14T11:25:09.840925Z","shell.execute_reply":"2024-11-14T11:25:10.289231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, path, transform):\n        super().__init__() # Calls the initializer of the parent Dataset class\n        self.data = pd.read_csv(path,header='infer').values # Loads the data from a CSV file, and convert them to numpy array from dataframe\n        self.transform = transform # ToTensor in our case\n        self.length = self.data.shape[0] # Number of rows (images) in our dataset\n        \n    def __len__(self):\n        return self.length # returns the length of the dataset when called. \n                           #This method is necessary for any PyTorch Dataset class so that PyTorch knows the number of items in the dataset.\n    \n    def __getitem__(self, idx):\n        flatimg = self.data[idx,1:].astype(np.uint8) # Access all the rows and columns from 2nd to end\n        label = self.data[idx,0] # Since the first column is of our labels, we take all rows and 1st column only\n        image = self.transform(np.reshape(flatimg, (28,28,1))) # Convert the whole row taken as flatimg (np array) to a 3d array and then to a tensor\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:10.293998Z","iopub.execute_input":"2024-11-14T11:25:10.294530Z","iopub.status.idle":"2024-11-14T11:25:10.306793Z","shell.execute_reply.started":"2024-11-14T11:25:10.294470Z","shell.execute_reply":"2024-11-14T11:25:10.305327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import ToTensor\ndataset = CustomDataset(train_data, ToTensor())\ntrain_dataloader = DataLoader(dataset,batch_size=64, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:10.308433Z","iopub.execute_input":"2024-11-14T11:25:10.308900Z","iopub.status.idle":"2024-11-14T11:25:13.694521Z","shell.execute_reply.started":"2024-11-14T11:25:10.308847Z","shell.execute_reply":"2024-11-14T11:25:13.693264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, data):\n        self.data = data\n        print(type(self.data))  \n\n        \n    def __len__(self):\n        return len(self.data)\n    def __getitem__(self, idx):\n        image = torch.tensor(self.data.iloc[idx].values, dtype=torch.float32)\n        image = image / 255.0\n        image = image.view(1, 28, 28)\n        return image\n\n\ntest_dataset = TestDataset(test_df)\ntest_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:13.696125Z","iopub.execute_input":"2024-11-14T11:25:13.696631Z","iopub.status.idle":"2024-11-14T11:25:13.707199Z","shell.execute_reply.started":"2024-11-14T11:25:13.696575Z","shell.execute_reply":"2024-11-14T11:25:13.705828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)\n        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)\n        self.fc1 = nn.Linear(64 * 7 * 7, 128)\n        self.fc2 = nn.Linear(128, 10)\n\n    def forward(self, x):\n        x = x.view(-1, 1, 28, 28)\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = x.view(-1, 64 * 7 * 7)\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:25:13.708851Z","iopub.execute_input":"2024-11-14T11:25:13.709258Z","iopub.status.idle":"2024-11-14T11:25:13.725078Z","shell.execute_reply.started":"2024-11-14T11:25:13.709213Z","shell.execute_reply":"2024-11-14T11:25:13.723846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EnhancedCNN(nn.Module):\n    def __init__(self):\n        super(EnhancedCNN, self).__init__()\n        \n        self.relu = nn.ReLU()\n        \n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(3, 3), stride=1, padding=1)\n        self.bn1 = nn.BatchNorm2d(16)\n        self.pool1 = nn.MaxPool2d(kernel_size=(2, 2), stride=2)\n        \n        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(3, 3), stride=1, padding=1)\n        self.bn2 = nn.BatchNorm2d(32)\n        self.pool2 = nn.MaxPool2d(kernel_size=(2, 2), stride=2)\n        \n        self.conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), stride=1, padding=1)\n        self.bn3 = nn.BatchNorm2d(64)\n        \n        self.conv4 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), stride=1, padding=1)\n        self.bn4 = nn.BatchNorm2d(128)\n        self.pool3 = nn.MaxPool2d(kernel_size=(2, 2), stride=2)\n        self.dropout2 = nn.Dropout(0.5)\n        \n        self.conv5 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(3, 3), stride=1, padding=1)\n        self.bn5 = nn.BatchNorm2d(256)\n        \n        self.flatten = nn.Flatten()\n        \n        self.fc1 = nn.Linear(256 * 3 * 3, 128)\n        self.fc2 = nn.Linear(128, 10)\n        \n        self.dropout3 = nn.Dropout(0.7)\n\n    def forward(self, x):\n        x = self.relu(self.bn1(self.conv1(x)))\n        x = self.pool1(x)\n        \n        x = self.relu(self.bn2(self.conv2(x)))\n        x = self.pool2(x)\n        \n        x = self.relu(self.bn3(self.conv3(x)))\n        \n        x = self.relu(self.bn4(self.conv4(x)))\n        x = self.pool3(x)\n        x = self.dropout2(x)\n        \n        x = self.relu(self.bn5(self.conv5(x)))\n        \n        x = self.flatten(x)\n        x = self.dropout3(F.relu(self.fc1(x)))\n        output = self.fc2(x)\n        \n        return output\n","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:27:06.920050Z","iopub.execute_input":"2024-11-14T11:27:06.920496Z","iopub.status.idle":"2024-11-14T11:27:06.941771Z","shell.execute_reply.started":"2024-11-14T11:27:06.920458Z","shell.execute_reply":"2024-11-14T11:27:06.939862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = CNN()\nmodel = EnhancedCNN()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:27:12.507199Z","iopub.execute_input":"2024-11-14T11:27:12.507679Z","iopub.status.idle":"2024-11-14T11:27:12.535187Z","shell.execute_reply.started":"2024-11-14T11:27:12.507637Z","shell.execute_reply":"2024-11-14T11:27:12.533180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Train(model, train_loader, criterion, optimizer, epochs):\n    for epoch in range(epochs):\n        model.train()\n        running_loss = 0\n        correct_predictions = 0\n        total_samples = 0\n        for i, (images, labels) in enumerate(train_loader):\n            imgages = images\n            labels = labels\n            \n            optimizer.zero_grad()\n            \n            output =  model(images)\n            \n            loss = criterion(output, labels)\n            \n            loss.backward()\n            \n            optimizer.step()\n            \n            running_loss += loss.item()\n            \n            \n            _, predictions = torch.max(output,1)\n            correct_predictions += (predictions==labels).sum().item()\n            total_samples += labels.size(0)\n            \n        epoch_loss = running_loss / len(train_loader)\n        epoch_accuracy = correct_predictions / total_samples * 100\n        print(f'Epoch {epoch+1}/{epochs} Loss: {running_loss:.4f} Accuracy: {epoch_accuracy:.2f}%')\n    print('Обучения закончено 👍')\n            ","metadata":{"execution":{"iopub.status.busy":"2024-11-14T11:27:12.900122Z","iopub.execute_input":"2024-11-14T11:27:12.900575Z","iopub.status.idle":"2024-11-14T11:27:12.911911Z","shell.execute_reply.started":"2024-11-14T11:27:12.900533Z","shell.execute_reply":"2024-11-14T11:27:12.910597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train(model, train_dataloader, criterion, optimizer, 10)","metadata":{"execution":{"iopub.status.busy":"2024-11-14T13:06:56.222049Z","iopub.execute_input":"2024-11-14T13:06:56.222530Z","iopub.status.idle":"2024-11-14T13:16:12.601480Z","shell.execute_reply.started":"2024-11-14T13:06:56.222487Z","shell.execute_reply":"2024-11-14T13:16:12.600064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\npredictions = []\n\nwith torch.no_grad():\n    for images in test_dataloader:\n        outputs = model(images)\n        _, predicted = torch.max(outputs, 1)\n        predictions.extend(predicted.numpy())\n        \nsample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample_submission['Label'] = predictions\nsample_submission.to_csv('predicted.csv', index = False)\nprint(f\"Saved predicted.csv ✔👌👍\")","metadata":{"execution":{"iopub.status.busy":"2024-11-14T13:16:12.604110Z","iopub.execute_input":"2024-11-14T13:16:12.604533Z","iopub.status.idle":"2024-11-14T13:16:26.539966Z","shell.execute_reply.started":"2024-11-14T13:16:12.604491Z","shell.execute_reply":"2024-11-14T13:16:26.538436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}