{"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-25T23:09:35.622256Z","iopub.execute_input":"2022-12-25T23:09:35.622661Z","iopub.status.idle":"2022-12-25T23:09:35.629090Z","shell.execute_reply.started":"2022-12-25T23:09:35.622620Z","shell.execute_reply":"2022-12-25T23:09:35.627966Z"},"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-25T23:09:35.631219Z","iopub.execute_input":"2022-12-25T23:09:35.631579Z","iopub.status.idle":"2022-12-25T23:09:38.601951Z","shell.execute_reply.started":"2022-12-25T23:09:35.631541Z","shell.execute_reply":"2022-12-25T23:09:38.600494Z"},"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-25T23:09:38.603566Z","iopub.execute_input":"2022-12-25T23:09:38.604201Z","iopub.status.idle":"2022-12-25T23:09:38.611351Z","shell.execute_reply.started":"2022-12-25T23:09:38.604170Z","shell.execute_reply":"2022-12-25T23:09:38.609911Z"},"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-25T23:09:38.612670Z","iopub.execute_input":"2022-12-25T23:09:38.613006Z","iopub.status.idle":"2022-12-25T23:09:38.622567Z","shell.execute_reply.started":"2022-12-25T23:09:38.612976Z","shell.execute_reply":"2022-12-25T23:09:38.621309Z"},"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, 1, 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-25T23:09:38.624990Z","iopub.execute_input":"2022-12-25T23:09:38.625268Z","iopub.status.idle":"2022-12-25T23:09:38.637547Z","shell.execute_reply.started":"2022-12-25T23:09:38.625242Z","shell.execute_reply":"2022-12-25T23:09:38.636400Z"},"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-25T23:09:38.638895Z","iopub.execute_input":"2022-12-25T23:09:38.639223Z","iopub.status.idle":"2022-12-25T23:09:38.775457Z","shell.execute_reply.started":"2022-12-25T23:09:38.639193Z","shell.execute_reply":"2022-12-25T23:09:38.773705Z"},"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-25T23:09:38.777378Z","iopub.execute_input":"2022-12-25T23:09:38.778056Z","iopub.status.idle":"2022-12-25T23:09:39.165798Z","shell.execute_reply.started":"2022-12-25T23:09:38.778018Z","shell.execute_reply":"2022-12-25T23:09:39.164601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model, train_dl, val_dl)","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:09:39.167279Z","iopub.execute_input":"2022-12-25T23:09:39.167953Z","iopub.status.idle":"2022-12-25T23:10:19.962534Z","shell.execute_reply.started":"2022-12-25T23:09:39.167923Z","shell.execute_reply":"2022-12-25T23:10:19.961160Z"},"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-25T23:10:19.964144Z","iopub.execute_input":"2022-12-25T23:10:19.964451Z","iopub.status.idle":"2022-12-25T23:10:23.077834Z","shell.execute_reply.started":"2022-12-25T23:10:19.964423Z","shell.execute_reply":"2022-12-25T23:10:23.076408Z"},"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-25T23:10:23.079302Z","iopub.execute_input":"2022-12-25T23:10:23.079606Z","iopub.status.idle":"2022-12-25T23:10:23.119869Z","shell.execute_reply.started":"2022-12-25T23:10:23.079577Z","shell.execute_reply":"2022-12-25T23:10:23.118678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Export the model\ntorch.onnx.export(model,                     # model being run\n                  model.example_input_array, # model input (or a tuple for multiple inputs)\n                  \"baseline.onnx\",           # where to save the model (can be a file or file-like object)\n                  export_params=True,        # store the trained parameter weights inside the model file\n                  opset_version=10,          # the ONNX version to export the model to\n                  do_constant_folding=True,  # whether to execute constant folding for optimization\n                  input_names = ['input'],   # the model's input names\n                  output_names = ['output'], # the model's output names\n                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable lenght axes\n                                'output' : {0 : 'batch_size'}})","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:10:23.121656Z","iopub.execute_input":"2022-12-25T23:10:23.122280Z","iopub.status.idle":"2022-12-25T23:10:23.185867Z","shell.execute_reply.started":"2022-12-25T23:10:23.122244Z","shell.execute_reply":"2022-12-25T23:10:23.184592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf lightning_logs","metadata":{"execution":{"iopub.status.busy":"2022-12-25T23:10:23.187657Z","iopub.execute_input":"2022-12-25T23:10:23.188041Z","iopub.status.idle":"2022-12-25T23:10:23.477677Z","shell.execute_reply.started":"2022-12-25T23:10:23.188012Z","shell.execute_reply":"2022-12-25T23:10:23.476399Z"},"trusted":true},"execution_count":null,"outputs":[]}]}