{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.nn import functional as F\nfrom torch.utils.data import TensorDataset, DataLoader\nimport pytorch_lightning as pl\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-25T14:39:38.878913Z","iopub.execute_input":"2022-12-25T14:39:38.879432Z","iopub.status.idle":"2022-12-25T14:39:45.951710Z","shell.execute_reply.started":"2022-12-25T14:39:38.879327Z","shell.execute_reply":"2022-12-25T14:39:45.950525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_df, val_df = train_test_split(train_df)\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\nprint(len(train_df), len(val_df), len(test_df))","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:39:45.953823Z","iopub.execute_input":"2022-12-25T14:39:45.955071Z","iopub.status.idle":"2022-12-25T14:39:52.410671Z","shell.execute_reply.started":"2022-12-25T14:39:45.955028Z","shell.execute_reply":"2022-12-25T14:39:52.409751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = torch.as_tensor(train_df.values)[:,1:]\ntrain_y = torch.as_tensor(train_df.values)[:,0]\nval_X = torch.as_tensor(val_df.values)[:,1:]\nval_y = torch.as_tensor(val_df.values)[:,0]\ntest_X = torch.as_tensor(test_df.values)\nprint(train_X.shape, train_y.shape, val_X.shape, val_y.shape, test_X.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:44:19.187799Z","iopub.execute_input":"2022-12-25T14:44:19.188223Z","iopub.status.idle":"2022-12-25T14:44:19.197562Z","shell.execute_reply.started":"2022-12-25T14:44:19.188190Z","shell.execute_reply":"2022-12-25T14:44:19.195632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DataLoader(TensorDataset(train_X, train_y), batch_size=128, shuffle=True, num_workers=4)\nval_dl = DataLoader(TensorDataset(val_X, val_y), batch_size=128, shuffle=False, num_workers=4)\ntest_dl = DataLoader(TensorDataset(test_X), batch_size=128, shuffle=False, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:46:56.600255Z","iopub.execute_input":"2022-12-25T14:46:56.600745Z","iopub.status.idle":"2022-12-25T14:46:56.607948Z","shell.execute_reply.started":"2022-12-25T14:46:56.600694Z","shell.execute_reply":"2022-12-25T14:46:56.606649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MNISTModel(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Sequential(         \n            nn.Conv2d(1, 16, 5,  1,  2),                              \n            nn.ReLU(),                      \n            nn.MaxPool2d(kernel_size=2),    \n        )\n        self.conv2 = nn.Sequential(         \n            nn.Conv2d(16, 32, 5, 1, 2),     \n            nn.ReLU(),                      \n            nn.MaxPool2d(2),                \n        )\n        # fully connected layer, output 10 classes\n        self.out = nn.LazyLinear(10)\n        \n        self.example_input_array = torch.randn(2, 2, 28*28)\n        \n    def forward(self, x):\n        # reshape inputs to 2d\n        x = x.view(-1, 1, 28, 28)\n        # cast inputs to float between 0 and 1\n        x = x / 255\n        x = self.conv1(x)\n        x = self.conv2(x)\n        # flatten the output of conv2 to (batch_size, 32 * 7 * 7)\n        x = x.flatten(1)    \n        output = self.out(x)\n        return output\n    \n    def predict_step(self, batch, batch_idx):\n        return self(batch[0])\n    \n    def shared_step(self, batch):\n        x, y = batch\n        loss = F.cross_entropy(self(x), y)\n        return loss\n\n    def training_step(self, batch, batch_idx): return self.shared_step(batch)\n    def validation_step(self, batch, batch_idx): return self.shared_step(batch)\n        \n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr=0.02)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:54:08.270373Z","iopub.execute_input":"2022-12-25T14:54:08.270831Z","iopub.status.idle":"2022-12-25T14:54:08.283853Z","shell.execute_reply.started":"2022-12-25T14:54:08.270793Z","shell.execute_reply":"2022-12-25T14:54:08.282461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xb, yb = next(iter(train_dl))\nmodel = MNISTModel()\nyp = model(xb)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:54:09.612917Z","iopub.execute_input":"2022-12-25T14:54:09.613304Z","iopub.status.idle":"2022-12-25T14:54:09.837423Z","shell.execute_reply.started":"2022-12-25T14:54:09.613274Z","shell.execute_reply":"2022-12-25T14:54:09.833565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = pl.Trainer(\n    accelerator=\"auto\",\n    max_epochs=4,\n    callbacks=[pl.callbacks.TQDMProgressBar(refresh_rate=20)],\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:54:11.192210Z","iopub.execute_input":"2022-12-25T14:54:11.193550Z","iopub.status.idle":"2022-12-25T14:54:11.607397Z","shell.execute_reply.started":"2022-12-25T14:54:11.193484Z","shell.execute_reply":"2022-12-25T14:54:11.605535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model, train_dl, val_dl)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:54:13.047463Z","iopub.execute_input":"2022-12-25T14:54:13.050328Z","iopub.status.idle":"2022-12-25T14:55:10.330773Z","shell.execute_reply.started":"2022-12-25T14:54:13.050275Z","shell.execute_reply":"2022-12-25T14:55:10.328012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = trainer.predict(model, test_dl)\ntest_pred = torch.cat(test_pred)\ntest_pred = test_pred.argmax(-1)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T14:56:54.912361Z","iopub.execute_input":"2022-12-25T14:56:54.912797Z","iopub.status.idle":"2022-12-25T14:57:40.471395Z","shell.execute_reply.started":"2022-12-25T14:56:54.912762Z","shell.execute_reply":"2022-12-25T14:57:40.470160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": range(1, len(test_pred)+1),\n    \"Label\": test_pred\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-12-25T15:04:31.763625Z","iopub.execute_input":"2022-12-25T15:04:31.764089Z","iopub.status.idle":"2022-12-25T15:04:31.810812Z","shell.execute_reply.started":"2022-12-25T15:04:31.764051Z","shell.execute_reply":"2022-12-25T15:04:31.809613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}