{"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\nimport torchvision\nfrom torch.utils.data import Dataset,DataLoader,random_split\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision.transforms as transforms\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pytorch_lightning as pl\nfrom pytorch_lightning import Trainer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-14T10:44:04.410968Z","iopub.execute_input":"2021-07-14T10:44:04.411557Z","iopub.status.idle":"2021-07-14T10:44:07.437528Z","shell.execute_reply.started":"2021-07-14T10:44:04.411471Z","shell.execute_reply":"2021-07-14T10:44:07.436263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyper-parameters\ninput_size = 784  # 28x28\nhidden_size = 500\nnum_classes = 10\nnum_epochs = 10\nbatch_size = 100\nlearning_rate = 0.001","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:11.000987Z","iopub.execute_input":"2021-07-14T10:44:11.001382Z","iopub.status.idle":"2021-07-14T10:44:11.006836Z","shell.execute_reply.started":"2021-07-14T10:44:11.001343Z","shell.execute_reply":"2021-07-14T10:44:11.00552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitDataset(Dataset):\n    def __init__(self):\n        xy = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', delimiter=',', dtype=np.float32, skiprows=1)\n        self.n_samples = xy.shape[0]\n        self.x_data = torch.from_numpy(xy[:, 1:]) /255\n        self.y_data = (xy[:, [0]])\n    def __getitem__(self, index):\n        return self.x_data[index], int(self.y_data[index])\n    def __len__(self):\n        return self.n_samples","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:13.150307Z","iopub.execute_input":"2021-07-14T10:44:13.150648Z","iopub.status.idle":"2021-07-14T10:44:13.157598Z","shell.execute_reply.started":"2021-07-14T10:44:13.150619Z","shell.execute_reply":"2021-07-14T10:44:13.156409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DigitDataset()\ntrain_dataset, val_dataset = random_split(dataset,[33600,8400])","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:17.020928Z","iopub.execute_input":"2021-07-14T10:44:17.021284Z","iopub.status.idle":"2021-07-14T10:44:39.541846Z","shell.execute_reply.started":"2021-07-14T10:44:17.021252Z","shell.execute_reply":"2021-07-14T10:44:39.541094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"li=[]","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:48.461495Z","iopub.execute_input":"2021-07-14T10:44:48.463565Z","iopub.status.idle":"2021-07-14T10:44:48.469199Z","shell.execute_reply.started":"2021-07-14T10:44:48.463526Z","shell.execute_reply":"2021-07-14T10:44:48.468247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitModel(pl.LightningModule):\n    def __init__(self,input_size,hidden_size,num_classes,train_dataset,val_dataset):\n        super(DigitModel,self).__init__()\n        self.train_dataset= train_dataset\n        self.val_dataset= val_dataset\n        self.l1= nn.Linear(input_size,hidden_size)\n        self.l2= nn.Linear(hidden_size, num_classes)\n        self.relu = nn.ReLU()\n    \n    def forward(self,x):\n        out= self.l1(x)\n        out= self.relu(out)\n        out= self.l2(out)\n        return out\n    \n    def train_dataloader(self):\n        return DataLoader(\n            dataset=self.train_dataset, batch_size=batch_size, num_workers=4, shuffle=True\n        )\n    def val_dataloader(self):\n        return torch.utils.data.DataLoader(\n            dataset=self.val_dataset, batch_size=batch_size, num_workers=4, shuffle=False\n        )\n    def training_step(self,batch,batch_idx):\n        images, labels = batch\n        outputs = self(images)\n        loss = F.cross_entropy(outputs, labels)\n        return {\"loss\": loss}\n    \n    def validation_step(self, batch, batch_idx):\n        images, labels = batch\n        outputs = self(images)    \n        loss = F.cross_entropy(outputs, labels)\n        return {\"val_loss\": loss}\n      \n    def test_step(self, batch, batch_idx):\n        images = batch\n        outputs = self(images)     \n        _, predicted = torch.max(outputs.data, 1)           \n        li.append(predicted)\n    \n        return {\"test_acc\":  0}\n    \n    def validation_epoch_end(self, outputs):\n        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()\n        return {'val_loss': avg_loss}\n    \n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr=learning_rate)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:53.833327Z","iopub.execute_input":"2021-07-14T10:44:53.833676Z","iopub.status.idle":"2021-07-14T10:44:53.847401Z","shell.execute_reply.started":"2021-07-14T10:44:53.833644Z","shell.execute_reply":"2021-07-14T10:44:53.846297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DigitModel(input_size,hidden_size,num_classes,train_dataset,val_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:45:01.642267Z","iopub.execute_input":"2021-07-14T10:45:01.642759Z","iopub.status.idle":"2021-07-14T10:45:01.655756Z","shell.execute_reply.started":"2021-07-14T10:45:01.642719Z","shell.execute_reply":"2021-07-14T10:45:01.654749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = Trainer(max_epochs=num_epochs)\ntrainer.fit(model)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:45:03.781099Z","iopub.execute_input":"2021-07-14T10:45:03.781587Z","iopub.status.idle":"2021-07-14T10:45:58.815508Z","shell.execute_reply.started":"2021-07-14T10:45:03.781555Z","shell.execute_reply":"2021-07-14T10:45:58.814425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitDataset(Dataset):\n    def __init__(self):\n        xy = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', delimiter=',', dtype=np.float32, skiprows=1)\n        self.n_samples = xy.shape[0]\n        self.x_data = torch.from_numpy(xy[:,:])\n    def __getitem__(self, index):\n        return self.x_data[index]\n    def __len__(self):\n        return self.n_samples","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:06.621469Z","iopub.execute_input":"2021-07-14T10:46:06.62184Z","iopub.status.idle":"2021-07-14T10:46:06.628427Z","shell.execute_reply.started":"2021-07-14T10:46:06.621801Z","shell.execute_reply":"2021-07-14T10:46:06.627325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = DigitDataset()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:08.921018Z","iopub.execute_input":"2021-07-14T10:46:08.921544Z","iopub.status.idle":"2021-07-14T10:46:23.713853Z","shell.execute_reply.started":"2021-07-14T10:46:08.921512Z","shell.execute_reply":"2021-07-14T10:46:23.712858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = DataLoader(\n            dataset=test_dataset,num_workers=4, shuffle=False\n        )","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:27.471862Z","iopub.execute_input":"2021-07-14T10:46:27.472219Z","iopub.status.idle":"2021-07-14T10:46:27.476453Z","shell.execute_reply.started":"2021-07-14T10:46:27.472189Z","shell.execute_reply":"2021-07-14T10:46:27.47552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.test(test_dataloaders = test_data)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:40.70591Z","iopub.execute_input":"2021-07-14T10:46:40.706399Z","iopub.status.idle":"2021-07-14T10:47:34.75095Z","shell.execute_reply.started":"2021-07-14T10:46:40.706369Z","shell.execute_reply":"2021-07-14T10:47:34.749952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"li2=[]\nj=1\nfor i in li:\n    li2.append([j,i.item()])\n    j+=1","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:47:57.731873Z","iopub.execute_input":"2021-07-14T10:47:57.732279Z","iopub.status.idle":"2021-07-14T10:47:57.782394Z","shell.execute_reply.started":"2021-07-14T10:47:57.732237Z","shell.execute_reply":"2021-07-14T10:47:57.781624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame(li2)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:50:54.810769Z","iopub.execute_input":"2021-07-14T10:50:54.81118Z","iopub.status.idle":"2021-07-14T10:50:54.84608Z","shell.execute_reply.started":"2021-07-14T10:50:54.811144Z","shell.execute_reply":"2021-07-14T10:50:54.845305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns = ['ImageId','Label']\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:51:53.369505Z","iopub.execute_input":"2021-07-14T10:51:53.369856Z","iopub.status.idle":"2021-07-14T10:51:53.622742Z","shell.execute_reply.started":"2021-07-14T10:51:53.369825Z","shell.execute_reply":"2021-07-14T10:51:53.621784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:53:22.972667Z","iopub.execute_input":"2021-07-14T10:53:22.973005Z","iopub.status.idle":"2021-07-14T10:53:23.030853Z","shell.execute_reply.started":"2021-07-14T10:53:22.972978Z","shell.execute_reply":"2021-07-14T10:53:23.030051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2= pd.read_csv('submission.csv')\ndf2.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:54:23.392669Z","iopub.execute_input":"2021-07-14T10:54:23.393023Z","iopub.status.idle":"2021-07-14T10:54:23.410215Z","shell.execute_reply.started":"2021-07-14T10:54:23.392991Z","shell.execute_reply":"2021-07-14T10:54:23.40907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:54:41.020868Z","iopub.execute_input":"2021-07-14T10:54:41.021827Z","iopub.status.idle":"2021-07-14T10:54:41.785746Z","shell.execute_reply.started":"2021-07-14T10:54:41.021767Z","shell.execute_reply":"2021-07-14T10:54:41.784515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! kaggle competitions submit -c digit-recognizer -f submission.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2021-07-14T09:02:33.086996Z","iopub.execute_input":"2021-07-14T09:02:33.087371Z","iopub.status.idle":"2021-07-14T09:02:34.214837Z","shell.execute_reply.started":"2021-07-14T09:02:33.087339Z","shell.execute_reply":"2021-07-14T09:02:34.213751Z"},"trusted":true},"execution_count":null,"outputs":[]}]}