{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nimport torch\nfrom torch import nn\nimport torch.nn.functional as F","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-14T04:57:41.567088Z","iopub.execute_input":"2023-07-14T04:57:41.567458Z","iopub.status.idle":"2023-07-14T04:57:43.559408Z","shell.execute_reply.started":"2023-07-14T04:57:41.567429Z","shell.execute_reply":"2023-07-14T04:57:43.557927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-07-14T04:57:43.561819Z","iopub.execute_input":"2023-07-14T04:57:43.562591Z","iopub.status.idle":"2023-07-14T04:57:47.635590Z","shell.execute_reply.started":"2023-07-14T04:57:43.562556Z","shell.execute_reply":"2023-07-14T04:57:47.634451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:47.636958Z","iopub.execute_input":"2023-07-14T04:57:47.637246Z","iopub.status.idle":"2023-07-14T04:57:47.675029Z","shell.execute_reply.started":"2023-07-14T04:57:47.637224Z","shell.execute_reply":"2023-07-14T04:57:47.673515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = np.array(train)\nnp.random.shuffle(train)\nrow, col = train.shape\n\ntest = np.array(test)\nX_test = test / 255\nX_test = torch.tensor(X_test.reshape(-1, 1, 28, 28)).to(torch.float32)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:47.677459Z","iopub.execute_input":"2023-07-14T04:57:47.677722Z","iopub.status.idle":"2023-07-14T04:57:48.265028Z","shell.execute_reply.started":"2023-07-14T04:57:47.677700Z","shell.execute_reply":"2023-07-14T04:57:48.264120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data = train[0:1000].T\nY_val = val_data[0]\nX_val = val_data[1:col] / 255","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.266140Z","iopub.execute_input":"2023-07-14T04:57:48.266493Z","iopub.status.idle":"2023-07-14T04:57:48.273119Z","shell.execute_reply.started":"2023-07-14T04:57:48.266462Z","shell.execute_reply":"2023-07-14T04:57:48.271711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = train[1000:row].T\nY_train = training_data[0]\nX_train = training_data[1:col] / 255","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.274088Z","iopub.execute_input":"2023-07-14T04:57:48.274389Z","iopub.status.idle":"2023-07-14T04:57:48.342591Z","shell.execute_reply.started":"2023-07-14T04:57:48.274363Z","shell.execute_reply":"2023-07-14T04:57:48.341142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.T\nX_train = torch.tensor(X_train.reshape(-1, 1, 28, 28)).to(torch.float32)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.344425Z","iopub.execute_input":"2023-07-14T04:57:48.344828Z","iopub.status.idle":"2023-07-14T04:57:48.485764Z","shell.execute_reply.started":"2023-07-14T04:57:48.344796Z","shell.execute_reply":"2023-07-14T04:57:48.483985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val = X_val.T\nX_val = torch.tensor(X_val.reshape(-1, 1, 28, 28)).to(torch.float32)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.487050Z","iopub.execute_input":"2023-07-14T04:57:48.487445Z","iopub.status.idle":"2023-07-14T04:57:48.494147Z","shell.execute_reply.started":"2023-07-14T04:57:48.487414Z","shell.execute_reply":"2023-07-14T04:57:48.492991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val = torch.tensor(Y_val).to(torch.long)\nY_train = torch.tensor(Y_train).to(torch.long)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.495394Z","iopub.execute_input":"2023-07-14T04:57:48.495762Z","iopub.status.idle":"2023-07-14T04:57:48.508996Z","shell.execute_reply.started":"2023-07-14T04:57:48.495727Z","shell.execute_reply":"2023-07-14T04:57:48.508292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 101\nplt.imshow(X_test[index][0,:,:])","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.511400Z","iopub.execute_input":"2023-07-14T04:57:48.512310Z","iopub.status.idle":"2023-07-14T04:57:48.752435Z","shell.execute_reply.started":"2023-07-14T04:57:48.512265Z","shell.execute_reply":"2023-07-14T04:57:48.751230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitNet(nn.Module):\n    \n    def __init__(self):\n        super(DigitNet, self).__init__()\n        self.cn1 = nn.Conv2d(1, 6, 5)\n        self.cn2 = nn.Conv2d(6, 16, 5)\n        self.fc1 = nn.Linear(16 * 4 * 4, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n        \n    def forward(self, x):\n        x = F.relu(self.cn1(x))\n        x = F.max_pool2d(x, (2, 2))\n        x = F.relu(self.cn2(x))\n        x = F.max_pool2d(x, (2, 2))        \n        x = x.view(-1, self.flattened_features(x))\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n    \n    def flattened_features(self, x):\n        size = x.size()[1:]\n        num_feats = 1\n        for s in size:\n            num_feats *= s\n        return num_feats","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.754190Z","iopub.execute_input":"2023-07-14T04:57:48.754484Z","iopub.status.idle":"2023-07-14T04:57:48.763540Z","shell.execute_reply.started":"2023-07-14T04:57:48.754461Z","shell.execute_reply":"2023-07-14T04:57:48.762077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = DigitNet()","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.765303Z","iopub.execute_input":"2023-07-14T04:57:48.765707Z","iopub.status.idle":"2023-07-14T04:57:48.787059Z","shell.execute_reply.started":"2023-07-14T04:57:48.765673Z","shell.execute_reply":"2023-07-14T04:57:48.785444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.788386Z","iopub.execute_input":"2023-07-14T04:57:48.788675Z","iopub.status.idle":"2023-07-14T04:57:48.796710Z","shell.execute_reply.started":"2023-07-14T04:57:48.788651Z","shell.execute_reply":"2023-07-14T04:57:48.795379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitDataset(Dataset):\n    def __init__(self, X_data, Y_data):\n        self.X_data = X_data\n        self.Y_data = Y_data\n    \n    def __len__(self):\n        return len(self.X_data)\n    \n    def __getitem__(self, index):\n        return self.X_data[index], self.Y_data[index]","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.798381Z","iopub.execute_input":"2023-07-14T04:57:48.798691Z","iopub.status.idle":"2023-07-14T04:57:48.808715Z","shell.execute_reply.started":"2023-07-14T04:57:48.798663Z","shell.execute_reply":"2023-07-14T04:57:48.807244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nshuffle = True","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.810601Z","iopub.execute_input":"2023-07-14T04:57:48.810934Z","iopub.status.idle":"2023-07-14T04:57:48.829204Z","shell.execute_reply.started":"2023-07-14T04:57:48.810905Z","shell.execute_reply":"2023-07-14T04:57:48.827799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = DigitDataset(X_train, Y_train)\ntrain_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=shuffle)\n\nval_dataset = DigitDataset(X_val, Y_val)\nval_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)\n\nloss_fn = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.831136Z","iopub.execute_input":"2023-07-14T04:57:48.831543Z","iopub.status.idle":"2023-07-14T04:57:48.844212Z","shell.execute_reply.started":"2023-07-14T04:57:48.831514Z","shell.execute_reply":"2023-07-14T04:57:48.843179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_loop(dataloader, model, loss_fn, optim):\n    size = len(dataloader.dataset)\n    \n    model.train()\n    for batch, (X, y) in enumerate(dataloader):\n        pred = model(X)\n        loss = loss_fn(pred, y)\n        \n        loss.backward()\n        optim.step()\n        optim.zero_grad()\n        \n        if batch % 250 == 0:\n            loss, current = loss.item(), (batch + 1) * len(X)\n            print(f\"loss: {loss:>7f} [{current:>5d}/{size:>5d}]\")","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.846087Z","iopub.execute_input":"2023-07-14T04:57:48.846656Z","iopub.status.idle":"2023-07-14T04:57:48.858412Z","shell.execute_reply.started":"2023-07-14T04:57:48.846608Z","shell.execute_reply":"2023-07-14T04:57:48.857551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(dataloader, model):\n    success = 0\n    counter = 0\n    with torch.no_grad():\n        for data in dataloader:\n            X, y_truth = data\n            op = model(X)\n            _, pred = torch.max(op.data, 1)\n            counter += y_truth.size(0)\n            success += (pred == y_truth).sum().item()\n    \n    print('validation accuracy is %d %%' % (100 * success / counter))\n\ndef test(dataloader, model):\n    success = 0\n    counter = 0\n    with torch.no_grad():\n        for data in dataloader:\n            X, y_truth = data\n            op = model(X)\n            _, pred = torch.max(op.data, 1)\n            counter += y_truth.size(0)\n            success += (pred == y_truth).sum().item()\n    \n    print('test accuracy is %d %%' % (100 * success / counter))","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:48.859707Z","iopub.execute_input":"2023-07-14T04:57:48.859937Z","iopub.status.idle":"2023-07-14T04:57:48.871097Z","shell.execute_reply.started":"2023-07-14T04:57:48.859918Z","shell.execute_reply":"2023-07-14T04:57:48.870404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 100\nlearning_rate = 1e-3\n\noptim = torch.optim.Adam(net.parameters(), lr=learning_rate)\n\nfor t in range(epochs):\n    print(f\"Epoch {t+1}\\n-------------------------------\")\n    train_loop(train_dataloader, net, loss_fn, optim)\n    if t % 4 == 0:\n        validate(val_dataloader, net)\n\nprint(\"Done!\")\nvalidate(val_dataloader, net)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T04:57:58.274702Z","iopub.execute_input":"2023-07-14T04:57:58.275059Z","iopub.status.idle":"2023-07-14T05:04:36.062055Z","shell.execute_reply.started":"2023-07-14T04:57:58.275030Z","shell.execute_reply":"2023-07-14T05:04:36.061195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-14T05:05:28.362628Z","iopub.execute_input":"2023-07-14T05:05:28.363002Z","iopub.status.idle":"2023-07-14T05:05:28.381932Z","shell.execute_reply.started":"2023-07-14T05:05:28.362973Z","shell.execute_reply":"2023-07-14T05:05:28.379519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op = net(X_test)\n_, y_pred = torch.max(op, 1)\n\nsubmit['Label'] = y_pred \nsubmit.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-07-14T05:05:35.525400Z","iopub.execute_input":"2023-07-14T05:05:35.525811Z","iopub.status.idle":"2023-07-14T05:05:36.522409Z","shell.execute_reply.started":"2023-07-14T05:05:35.525776Z","shell.execute_reply":"2023-07-14T05:05:36.521010Z"},"trusted":true},"execution_count":null,"outputs":[]}]}