{"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"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"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)\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'):\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-08-21T08:41:43.167781Z","iopub.execute_input":"2025-08-21T08:41:43.167978Z","iopub.status.idle":"2025-08-21T08:41:44.883196Z","shell.execute_reply.started":"2025-08-21T08:41:43.167961Z","shell.execute_reply":"2025-08-21T08:41:44.882389Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Using PYTORCH","metadata":{}},{"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\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, TensorDataset, random_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:42:54.984979Z","iopub.execute_input":"2025-08-21T08:42:54.985699Z","iopub.status.idle":"2025-08-21T08:42:58.420122Z","shell.execute_reply.started":"2025-08-21T08:42:54.985665Z","shell.execute_reply":"2025-08-21T08:42:58.419407Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:42:13.514472Z","iopub.execute_input":"2025-08-21T08:42:13.515123Z","iopub.status.idle":"2025-08-21T08:42:17.855816Z","shell.execute_reply.started":"2025-08-21T08:42:13.515093Z","shell.execute_reply":"2025-08-21T08:42:17.855251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(\"label\", axis=1).values / 255.0\ny = train[\"label\"].values\nX_test = test.values / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:42:21.096376Z","iopub.execute_input":"2025-08-21T08:42:21.097152Z","iopub.status.idle":"2025-08-21T08:42:21.360165Z","shell.execute_reply.started":"2025-08-21T08:42:21.097119Z","shell.execute_reply":"2025-08-21T08:42:21.359453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshape for CNN (N, 1, 28, 28)\nX = X.reshape(-1, 1, 28, 28).astype(np.float32)\nX_test = X_test.reshape(-1, 1, 28, 28).astype(np.float32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:42:27.518419Z","iopub.execute_input":"2025-08-21T08:42:27.518704Z","iopub.status.idle":"2025-08-21T08:42:27.611640Z","shell.execute_reply.started":"2025-08-21T08:42:27.518683Z","shell.execute_reply":"2025-08-21T08:42:27.611033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert to PyTorch tensors\nX_tensor = torch.tensor(X)\ny_tensor = torch.tensor(y, dtype=torch.long)\nX_test_tensor = torch.tensor(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:43:03.945697Z","iopub.execute_input":"2025-08-21T08:43:03.946604Z","iopub.status.idle":"2025-08-21T08:43:04.073036Z","shell.execute_reply.started":"2025-08-21T08:43:03.946573Z","shell.execute_reply":"2025-08-21T08:43:04.072116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create dataset\ndataset = TensorDataset(X_tensor, y_tensor)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:43:24.549673Z","iopub.execute_input":"2025-08-21T08:43:24.549967Z","iopub.status.idle":"2025-08-21T08:43:24.556035Z","shell.execute_reply.started":"2025-08-21T08:43:24.549944Z","shell.execute_reply":"2025-08-21T08:43:24.555254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train-validation split\nval_size = int(0.1 * len(dataset))\ntrain_size = len(dataset) - val_size\ntrain_dataset, val_dataset = random_split(dataset, [train_size, val_size])\n\ntrain_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=128, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:43:40.502707Z","iopub.execute_input":"2025-08-21T08:43:40.502989Z","iopub.status.idle":"2025-08-21T08:43:40.521298Z","shell.execute_reply.started":"2025-08-21T08:43:40.502969Z","shell.execute_reply":"2025-08-21T08:43:40.520680Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Define CNN Model","metadata":{}},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(1, 32, 3, padding=1)\n        self.conv2 = nn.Conv2d(32, 32, 3, padding=1)\n        self.pool1 = nn.MaxPool2d(2,2)\n        self.drop1 = nn.Dropout(0.25)\n\n        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)\n        self.conv4 = nn.Conv2d(64, 64, 3, padding=1)\n        self.pool2 = nn.MaxPool2d(2,2)\n        self.drop2 = nn.Dropout(0.25)\n\n        self.fc1 = nn.Linear(64*7*7, 256)\n        self.drop3 = nn.Dropout(0.5)\n        self.fc2 = nn.Linear(256, 10)\n\n    def forward(self, x):\n        x = F.relu(self.conv1(x))\n        x = F.relu(self.conv2(x))\n        x = self.pool1(x)\n        x = self.drop1(x)\n\n        x = F.relu(self.conv3(x))\n        x = F.relu(self.conv4(x))\n        x = self.pool2(x)\n        x = self.drop2(x)\n\n        x = x.view(x.size(0), -1)  # flatten\n        x = F.relu(self.fc1(x))\n        x = self.drop3(x)\n        x = self.fc2(x)\n        return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:44:10.169577Z","iopub.execute_input":"2025-08-21T08:44:10.169865Z","iopub.status.idle":"2025-08-21T08:44:10.177439Z","shell.execute_reply.started":"2025-08-21T08:44:10.169842Z","shell.execute_reply":"2025-08-21T08:44:10.176722Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = CNN().to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:44:34.744636Z","iopub.execute_input":"2025-08-21T08:44:34.745167Z","iopub.status.idle":"2025-08-21T08:44:35.013486Z","shell.execute_reply.started":"2025-08-21T08:44:34.745146Z","shell.execute_reply":"2025-08-21T08:44:35.012669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:44:44.032717Z","iopub.execute_input":"2025-08-21T08:44:44.033459Z","iopub.status.idle":"2025-08-21T08:44:46.547068Z","shell.execute_reply.started":"2025-08-21T08:44:44.033436Z","shell.execute_reply":"2025-08-21T08:44:46.546360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs = 15\nfor epoch in range(epochs):\n    model.train()\n    total_loss = 0\n    for images, labels in train_loader:\n        images, labels = images.to(device), labels.to(device)\n        \n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        total_loss += loss.item()\n    \n    # Validation\n    model.eval()\n    correct, total = 0, 0\n    with torch.no_grad():\n        for images, labels in val_loader:\n            images, labels = images.to(device), labels.to(device)\n            outputs = model(images)\n            _, preds = torch.max(outputs, 1)\n            correct += (preds == labels).sum().item()\n            total += labels.size(0)\n    val_acc = correct / total\n    print(f\"Epoch {epoch+1}/{epochs}, Loss: {total_loss:.4f}, Val Acc: {val_acc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:45:07.303830Z","iopub.execute_input":"2025-08-21T08:45:07.304611Z","iopub.status.idle":"2025-08-21T08:45:55.114906Z","shell.execute_reply.started":"2025-08-21T08:45:07.304585Z","shell.execute_reply":"2025-08-21T08:45:55.114264Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict on Test Set","metadata":{}},{"cell_type":"code","source":"model.eval()\ntest_loader = DataLoader(X_test_tensor, batch_size=128, shuffle=False)\n\npreds = []\nwith torch.no_grad():\n    for images in test_loader:\n        images = images.to(device)\n        outputs = model(images)\n        _, predicted = torch.max(outputs, 1)\n        preds.extend(predicted.cpu().numpy())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:46:45.066710Z","iopub.execute_input":"2025-08-21T08:46:45.067013Z","iopub.status.idle":"2025-08-21T08:46:45.803494Z","shell.execute_reply.started":"2025-08-21T08:46:45.066993Z","shell.execute_reply":"2025-08-21T08:46:45.802861Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create Submission File","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(preds)+1),\n    \"Label\": preds\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"✅ Submission file created!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:47:09.109453Z","iopub.execute_input":"2025-08-21T08:47:09.110147Z","iopub.status.idle":"2025-08-21T08:47:09.184987Z","shell.execute_reply.started":"2025-08-21T08:47:09.110124Z","shell.execute_reply":"2025-08-21T08:47:09.184254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:47:53.146114Z","iopub.execute_input":"2025-08-21T08:47:53.146428Z","iopub.status.idle":"2025-08-21T08:47:53.170911Z","shell.execute_reply.started":"2025-08-21T08:47:53.146408Z","shell.execute_reply":"2025-08-21T08:47:53.170165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import FileLink\n\n# Save file to working directory\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)\n\n# Create a download link\nFileLink(r'submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T08:53:56.490623Z","iopub.execute_input":"2025-08-21T08:53:56.490909Z","iopub.status.idle":"2025-08-21T08:53:56.515402Z","shell.execute_reply.started":"2025-08-21T08:53:56.490889Z","shell.execute_reply":"2025-08-21T08:53:56.514853Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Using Tensorflow","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:04:51.546435Z","iopub.execute_input":"2025-08-21T09:04:51.546684Z","iopub.status.idle":"2025-08-21T09:05:05.215260Z","shell.execute_reply.started":"2025-08-21T09:04:51.546668Z","shell.execute_reply":"2025-08-21T09:05:05.214562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load data\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:09.085948Z","iopub.execute_input":"2025-08-21T09:05:09.086935Z","iopub.status.idle":"2025-08-21T09:05:12.114342Z","shell.execute_reply.started":"2025-08-21T09:05:09.086908Z","shell.execute_reply":"2025-08-21T09:05:12.113743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Separate features and labels\nX = train.drop(\"label\", axis=1).values\ny = train[\"label\"].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:18.321684Z","iopub.execute_input":"2025-08-21T09:05:18.322381Z","iopub.status.idle":"2025-08-21T09:05:18.405005Z","shell.execute_reply.started":"2025-08-21T09:05:18.322351Z","shell.execute_reply":"2025-08-21T09:05:18.404458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize (0–1)\nX = X / 255.0\ntest = test / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:24.717891Z","iopub.execute_input":"2025-08-21T09:05:24.718616Z","iopub.status.idle":"2025-08-21T09:05:24.864171Z","shell.execute_reply.started":"2025-08-21T09:05:24.718592Z","shell.execute_reply":"2025-08-21T09:05:24.863566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshape to (28,28,1) for CNN\nX = X.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:33.958943Z","iopub.execute_input":"2025-08-21T09:05:33.959662Z","iopub.status.idle":"2025-08-21T09:05:33.963185Z","shell.execute_reply.started":"2025-08-21T09:05:33.959638Z","shell.execute_reply":"2025-08-21T09:05:33.962368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# One-hot encode labels\ny = to_categorical(y, num_classes=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:41.782922Z","iopub.execute_input":"2025-08-21T09:05:41.783195Z","iopub.status.idle":"2025-08-21T09:05:41.787961Z","shell.execute_reply.started":"2025-08-21T09:05:41.783174Z","shell.execute_reply":"2025-08-21T09:05:41.787288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split train-validation\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:05:50.231040Z","iopub.execute_input":"2025-08-21T09:05:50.231338Z","iopub.status.idle":"2025-08-21T09:05:50.603837Z","shell.execute_reply.started":"2025-08-21T09:05:50.231314Z","shell.execute_reply":"2025-08-21T09:05:50.603034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build CNN model\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),\n    BatchNormalization(),\n    Conv2D(32, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    Dropout(0.25),\n    \n    Conv2D(64, (3,3), activation='relu'),\n    BatchNormalization(),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    Dropout(0.25),\n    \n    Flatten(),\n    Dense(256, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:06:05.781285Z","iopub.execute_input":"2025-08-21T09:06:05.781550Z","iopub.status.idle":"2025-08-21T09:06:07.180547Z","shell.execute_reply.started":"2025-08-21T09:06:05.781532Z","shell.execute_reply":"2025-08-21T09:06:07.179956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile\nmodel.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:06:16.849736Z","iopub.execute_input":"2025-08-21T09:06:16.850053Z","iopub.status.idle":"2025-08-21T09:06:16.864395Z","shell.execute_reply.started":"2025-08-21T09:06:16.850031Z","shell.execute_reply":"2025-08-21T09:06:16.863724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train\nhistory = model.fit(X_train, y_train, \n                    validation_data=(X_val, y_val),\n                    epochs=15, batch_size=128, verbose=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:06:26.092799Z","iopub.execute_input":"2025-08-21T09:06:26.093395Z","iopub.status.idle":"2025-08-21T09:07:09.537094Z","shell.execute_reply.started":"2025-08-21T09:06:26.093372Z","shell.execute_reply":"2025-08-21T09:07:09.536477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict test set\npred = model.predict(test)\npred_labels = np.argmax(pred, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:07:22.015697Z","iopub.execute_input":"2025-08-21T09:07:22.016282Z","iopub.status.idle":"2025-08-21T09:07:24.858199Z","shell.execute_reply.started":"2025-08-21T09:07:22.016259Z","shell.execute_reply":"2025-08-21T09:07:24.857526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create submission file\nsubmission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(pred_labels)+1),\n    \"Label\": pred_labels\n})\nsubmission.to_csv(\"submission.csv\", index=False)\n\nprint(\"Submission file created!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T09:07:34.511705Z","iopub.execute_input":"2025-08-21T09:07:34.512294Z","iopub.status.idle":"2025-08-21T09:07:34.537845Z","shell.execute_reply.started":"2025-08-21T09:07:34.512267Z","shell.execute_reply":"2025-08-21T09:07:34.537033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}