Files
Kaggle/digit-recognizer/main.ipynb
T
2026-07-05 00:31:14 +08:00

217 lines
8.4 KiB
Plaintext

{
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.13",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat_minor": 4,
"nbformat": 4,
"cells": [
{
"cell_type": "code",
"source": "import matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nimport pandas as pd",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:12.326817130Z",
"start_time": "2026-07-03T10:17:12.293250353Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:09.940312Z",
"iopub.execute_input": "2026-07-04T16:16:09.940612Z",
"iopub.status.idle": "2026-07-04T16:16:12.856395Z",
"shell.execute_reply.started": "2026-07-04T16:16:09.940586Z",
"shell.execute_reply": "2026-07-04T16:16:12.855776Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "%cd /kaggle/working\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ntrain_df=pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\ntest_df=pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:15.014258006Z",
"start_time": "2026-07-03T10:17:12.346500913Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:12.857730Z",
"iopub.execute_input": "2026-07-04T16:16:12.858065Z",
"iopub.status.idle": "2026-07-04T16:16:16.048768Z",
"shell.execute_reply.started": "2026-07-04T16:16:12.858040Z",
"shell.execute_reply": "2026-07-04T16:16:16.048163Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "fig, ax = plt.subplots(nrows=2, ncols=2, sharex='all', sharey='all')\nax = ax.flatten()\nfor i in range(4):\n img = train_df.iloc[i][1:].to_numpy().reshape(28,28)\n # ax[i].imshow(img,cmap='Greys')\n ax[i].imshow(img)\n ax[i].set_title(f'{train_df.iloc[i][0]}')",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:15.565961732Z",
"start_time": "2026-07-03T10:17:15.073738808Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:16.049873Z",
"iopub.execute_input": "2026-07-04T16:16:16.050089Z",
"iopub.status.idle": "2026-07-04T16:16:16.369380Z",
"shell.execute_reply.started": "2026-07-04T16:16:16.050066Z",
"shell.execute_reply": "2026-07-04T16:16:16.368725Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "class DatasetMnist(Dataset):\n def __init__(self,df):\n self.df=df\n def __len__(self):\n return len(self.df)\n def __getitem__(self, idx):\n item= {\"Data\": torch.tensor(self.df.iloc[idx][1:].to_numpy().reshape(28, 28),dtype=torch.float), \"label\": torch.tensor(self.df.iloc[idx][0],dtype=torch.long)}\n return item\nbatch_size =64\ntrain_dataset = DatasetMnist(train_df)\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:15.669173315Z",
"start_time": "2026-07-03T10:17:15.616933550Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:16.370253Z",
"iopub.execute_input": "2026-07-04T16:16:16.370426Z",
"iopub.status.idle": "2026-07-04T16:16:16.375630Z",
"shell.execute_reply.started": "2026-07-04T16:16:16.370409Z",
"shell.execute_reply": "2026-07-04T16:16:16.375171Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "class MnistModule(nn.Module):\n def __init__(self):\n super(MnistModule, self).__init__()\n self.fc1 = nn.Linear(28*28, 512)\n self.relu = nn.ReLU()\n self.fc2 = nn.Linear(512, 256)\n self.fc3 = nn.Linear(256, 128)\n self.fc4 = nn.Linear(128,10)\n def forward(self,X):\n return self.fc4(self.relu(self.fc3(self.relu(self.fc2(self.relu(self.fc1(X.view(-1,28*28))))))))",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:15.729366448Z",
"start_time": "2026-07-03T10:17:15.670668930Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:16.376258Z",
"iopub.execute_input": "2026-07-04T16:16:16.376401Z",
"iopub.status.idle": "2026-07-04T16:16:16.389616Z",
"shell.execute_reply.started": "2026-07-04T16:16:16.376386Z",
"shell.execute_reply": "2026-07-04T16:16:16.388703Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "model = MnistModule()\nmodel=model.to(device)\nloss_func = nn.CrossEntropyLoss()\noptimizer = torch.optim.AdamW(model.parameters(), lr=2e-4)\nscheduler=torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=10)\n",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:17:15.792463697Z",
"start_time": "2026-07-03T10:17:15.730536380Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:16.390304Z",
"iopub.execute_input": "2026-07-04T16:16:16.390504Z",
"iopub.status.idle": "2026-07-04T16:16:18.580638Z",
"shell.execute_reply.started": "2026-07-04T16:16:16.390482Z",
"shell.execute_reply": "2026-07-04T16:16:18.580038Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "epochs = 30\nfor epoch in range(epochs):\n model.train()\n training_loss=0\n for batch in train_loader:\n optimizer.zero_grad()\n X = batch['Data']\n labels=batch['label']\n X=X.to(device)\n labels=labels.to(device)\n #print(model(X),'\\n',labels)\n loss = loss_func(model(X),labels)\n loss.backward()\n optimizer.step()\n training_loss+=loss.item()\n scheduler.step()\n print(f\"train_loss: {training_loss/len(train_loader)}\")",
"metadata": {
"ExecuteTime": {
"end_time": "2026-07-03T10:18:55.758148762Z",
"start_time": "2026-07-03T10:17:31.736342843Z"
},
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:16:18.582512Z",
"iopub.execute_input": "2026-07-04T16:16:18.582874Z",
"iopub.status.idle": "2026-07-04T16:18:53.082304Z",
"shell.execute_reply.started": "2026-07-04T16:16:18.582856Z",
"shell.execute_reply": "2026-07-04T16:18:53.081057Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"class DatasetMnistTest(Dataset):\n",
" def __init__(self,df):\n",
" self.df=df\n",
" def __len__(self):\n",
" return len(self.df)\n",
" def __getitem__(self, idx):\n",
" item= {\"Data\": torch.tensor(self.df.iloc[idx].to_numpy().reshape(28, 28),dtype=torch.float)}\n",
" return item\n",
"test_dataset = DatasetMnistTest(test_df)\n",
"test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\n",
"all_preds = []\n",
"with torch.no_grad():\n",
" for batch in test_loader:\n",
" input_ids = batch['Data'].to(device)\n",
" outputs = model(input_ids)\n",
" preds = torch.argmax(outputs, dim=1)\n",
" all_preds.extend(preds.cpu().numpy())\n",
"idd = range(1,len(all_preds)+1)\n",
"submission = pd.DataFrame({\n",
" 'ImageId':idd,\n",
" 'Label': all_preds\n",
"})\n",
"print(submission)\n",
"submission.to_csv('submission.csv', index=False)\n",
"print(\"Submission saved!\")"
],
"metadata": {
"trusted": true,
"execution": {
"iopub.status.busy": "2026-07-04T16:19:40.600448Z",
"iopub.execute_input": "2026-07-04T16:19:40.601006Z",
"iopub.status.idle": "2026-07-04T16:19:41.910494Z",
"shell.execute_reply.started": "2026-07-04T16:19:40.600982Z",
"shell.execute_reply": "2026-07-04T16:19:41.909862Z"
}
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": "",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": null
}
]
}