{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch as torch\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n    \n    \n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-27T11:52:42.239895Z","iopub.execute_input":"2023-02-27T11:52:42.240452Z","iopub.status.idle":"2023-02-27T11:52:42.259342Z","shell.execute_reply.started":"2023-02-27T11:52:42.240376Z","shell.execute_reply":"2023-02-27T11:52:42.257673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport torch\nimport torch.nn.functional as F\nfrom torch import nn, optim\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torchvision import transforms, models\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.autograd import Variable\n\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:01:12.850461Z","iopub.execute_input":"2023-02-27T12:01:12.850929Z","iopub.status.idle":"2023-02-27T12:01:13.223951Z","shell.execute_reply.started":"2023-02-27T12:01:12.850881Z","shell.execute_reply":"2023-02-27T12:01:13.222498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 100\nEPOCHS = 10\nDEVICE = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:01:45.440043Z","iopub.execute_input":"2023-02-27T12:01:45.440856Z","iopub.status.idle":"2023-02-27T12:01:45.445904Z","shell.execute_reply.started":"2023-02-27T12:01:45.440809Z","shell.execute_reply":"2023-02-27T12:01:45.444862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv', dtype=np.float32)  \ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv', dtype=np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T11:55:54.289640Z","iopub.execute_input":"2023-02-27T11:55:54.290132Z","iopub.status.idle":"2023-02-27T11:55:59.524965Z","shell.execute_reply.started":"2023-02-27T11:55:54.290088Z","shell.execute_reply":"2023-02-27T11:55:59.523458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-27T11:55:59.527213Z","iopub.execute_input":"2023-02-27T11:55:59.528179Z","iopub.status.idle":"2023-02-27T11:55:59.564360Z","shell.execute_reply.started":"2023-02-27T11:55:59.528131Z","shell.execute_reply":"2023-02-27T11:55:59.562645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.max().max()","metadata":{"execution":{"iopub.status.busy":"2023-02-27T11:59:34.988231Z","iopub.execute_input":"2023-02-27T11:59:34.988786Z","iopub.status.idle":"2023-02-27T11:59:35.081806Z","shell.execute_reply.started":"2023-02-27T11:59:34.988739Z","shell.execute_reply":"2023-02-27T11:59:35.080345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(idx):\n    train_np = train_df.to_numpy()\n    sample_np = train_np[idx][1:].reshape((28,28))\n    \n    # print label and img\n    print(\"Label : {}\".format(train_np[idx][0])) # label\n    plt.imshow(sample_np, cmap='gray')\n\n    \ndef show_test_img(idx):\n    test_np = test_df.to_numpy()\n    sample_np = test_np[idx][:].reshape((28,28))\n    \n    # print label and img\n    print(\"Label : {}\".format(test_np[idx][0])) # label\n    plt.imshow(sample_np, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:00:31.774000Z","iopub.execute_input":"2023-02-27T12:00:31.774527Z","iopub.status.idle":"2023-02-27T12:00:31.785078Z","shell.execute_reply.started":"2023-02-27T12:00:31.774474Z","shell.execute_reply":"2023-02-27T12:00:31.782928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_test_img(1)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:01:16.325379Z","iopub.execute_input":"2023-02-27T12:01:16.325879Z","iopub.status.idle":"2023-02-27T12:01:16.599088Z","shell.execute_reply.started":"2023-02-27T12:01:16.325836Z","shell.execute_reply":"2023-02-27T12:01:16.596504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MNISTDataset(Dataset): \n  def __init__(self, raw_data):\n    self.raw_data = raw_data \n    self.x = raw_data.iloc[:,1:]\n    self.y = raw_data.iloc[:,0]\n    \n    \n  def __len__(self):\n    return len(self.raw_data)\n\n  def __getitem__(self, idx):\n    x_data = torch.LongTensor(self.raw_data.iloc[idx,1:])\n    x_data = x_data/255.\n    y_data = torch.LongTensor(self.raw_data.iloc[idx,:1])\n    return x_data, y_data","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:02:38.418552Z","iopub.execute_input":"2023-02-27T12:02:38.419021Z","iopub.status.idle":"2023-02-27T12:02:38.427511Z","shell.execute_reply.started":"2023-02-27T12:02:38.418982Z","shell.execute_reply":"2023-02-27T12:02:38.426359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnistDataset1 = MNISTDataset(train_df)\n\ndataloader1 = DataLoader(mnistDataset1, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)\n## test","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:02:44.990510Z","iopub.execute_input":"2023-02-27T12:02:44.991229Z","iopub.status.idle":"2023-02-27T12:02:44.998640Z","shell.execute_reply.started":"2023-02-27T12:02:44.991181Z","shell.execute_reply":"2023-02-27T12:02:44.997120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## test\nmnistDataset1[268][1]","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:02:55.484916Z","iopub.execute_input":"2023-02-27T12:02:55.485372Z","iopub.status.idle":"2023-02-27T12:02:55.574105Z","shell.execute_reply.started":"2023-02-27T12:02:55.485333Z","shell.execute_reply":"2023-02-27T12:02:55.572587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MLP(nn.Module):\n    def __init__(self):\n        super(MLP, self).__init__()\n        self.linear1 = nn.Linear(784,300)\n        self.linear2 = nn.Linear(300,150)\n        self.linear3 = nn.Linear(150,50)\n        self.linear4 = nn.Linear(50,10)\n    \n    def forward(self,X):\n        X = F.relu(self.linear1(X))\n        X = F.relu(self.linear2(X))\n        X = F.relu(self.linear3(X))\n        X = self.linear4(X)\n        return F.log_softmax(X, dim=1)   ","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:02:59.727240Z","iopub.execute_input":"2023-02-27T12:02:59.727760Z","iopub.status.idle":"2023-02-27T12:02:59.737123Z","shell.execute_reply.started":"2023-02-27T12:02:59.727705Z","shell.execute_reply":"2023-02-27T12:02:59.735795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = MLP()\nprint(model1)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:03:04.205595Z","iopub.execute_input":"2023-02-27T12:03:04.206655Z","iopub.status.idle":"2023-02-27T12:03:04.226845Z","shell.execute_reply.started":"2023-02-27T12:03:04.206578Z","shell.execute_reply":"2023-02-27T12:03:04.225198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(model, dataloader):\n    optimizer = torch.optim.Adam(model.parameters())#,lr=0.001, betas=(0.9,0.999))\n    error_func = nn.CrossEntropyLoss()\n    model.train()\n    for epoch in range(EPOCHS):\n        correct = 0\n        for batch_idx, (x_batch, y_batch) in enumerate(dataloader):\n            x_batch = Variable(x_batch).float()\n            y_batch = Variable(y_batch).squeeze(dim=-1)\n            optimizer.zero_grad()\n            output = model(x_batch).float()\n            \n            loss = error_func(output, y_batch)\n            loss.backward()\n            optimizer.step()\n\n            # Total correct predictions\n            predicted = torch.max(output.data, 1)[1]\n            correct += (predicted == y_batch).sum()\n\n            if batch_idx % 100 == 0:\n                print('Epoch : {} [{}/{}]\\t Accuracy:{:.3f}%'.format(\n                    epoch, batch_idx*BATCH_SIZE, len(dataloader.dataset), float(correct*100) / float(BATCH_SIZE*(batch_idx+1))\n                        )\n                     )","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:03:17.237982Z","iopub.execute_input":"2023-02-27T12:03:17.238898Z","iopub.status.idle":"2023-02-27T12:03:17.249451Z","shell.execute_reply.started":"2023-02-27T12:03:17.238836Z","shell.execute_reply":"2023-02-27T12:03:17.247663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit(model1, dataloader1)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:03:24.037471Z","iopub.execute_input":"2023-02-27T12:03:24.037972Z","iopub.status.idle":"2023-02-27T12:09:53.802763Z","shell.execute_reply.started":"2023-02-27T12:03:24.037927Z","shell.execute_reply":"2023-02-27T12:09:53.801467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(model, test_data):\n    model.eval()\n    preds = torch.LongTensor()\n    test_preds = torch.LongTensor()\n    \n    for i, data in enumerate(test_data):\n\n        data = data.unsqueeze(0)\n        #print(data)\n        output = model(data)\n        \n        preds = output.data.max(1, keepdim=True)[1]\n        test_preds = torch.cat((test_preds, preds), dim=0)\n        \n    return test_preds\n\n\ntest_data = torch.LongTensor(test_df.to_numpy())\ntest_data = test_data/255.\nres = evaluate(model1, test_data)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:09:53.805442Z","iopub.execute_input":"2023-02-27T12:09:53.806281Z","iopub.status.idle":"2023-02-27T12:10:00.699515Z","shell.execute_reply.started":"2023-02-27T12:09:53.806222Z","shell.execute_reply":"2023-02-27T12:10:00.697998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:10:26.206192Z","iopub.execute_input":"2023-02-27T12:10:26.206671Z","iopub.status.idle":"2023-02-27T12:10:26.230372Z","shell.execute_reply.started":"2023-02-27T12:10:26.206629Z","shell.execute_reply":"2023-02-27T12:10:26.228678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd['Label'] = res\n","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:10:28.301480Z","iopub.execute_input":"2023-02-27T12:10:28.301961Z","iopub.status.idle":"2023-02-27T12:10:28.309945Z","shell.execute_reply.started":"2023-02-27T12:10:28.301915Z","shell.execute_reply":"2023-02-27T12:10:28.308440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:10:31.190000Z","iopub.execute_input":"2023-02-27T12:10:31.190780Z","iopub.status.idle":"2023-02-27T12:10:31.203238Z","shell.execute_reply.started":"2023-02-27T12:10:31.190718Z","shell.execute_reply":"2023-02-27T12:10:31.201702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:11:54.384767Z","iopub.execute_input":"2023-02-27T12:11:54.385265Z","iopub.status.idle":"2023-02-27T12:11:54.430613Z","shell.execute_reply.started":"2023-02-27T12:11:54.385225Z","shell.execute_reply":"2023-02-27T12:11:54.429212Z"},"trusted":true},"execution_count":null,"outputs":[]}]}