{"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)\n\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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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-21T15:31:36.303924Z","iopub.execute_input":"2023-02-21T15:31:36.304444Z","iopub.status.idle":"2023-02-21T15:31:36.313533Z","shell.execute_reply.started":"2023-02-21T15:31:36.304401Z","shell.execute_reply":"2023-02-21T15:31:36.312340Z"},"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-21T15:31:36.316277Z","iopub.execute_input":"2023-02-21T15:31:36.317069Z","iopub.status.idle":"2023-02-21T15:31:36.331084Z","shell.execute_reply.started":"2023-02-21T15:31:36.317007Z","shell.execute_reply":"2023-02-21T15:31:36.329838Z"},"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-21T15:31:36.332531Z","iopub.execute_input":"2023-02-21T15:31:36.333111Z","iopub.status.idle":"2023-02-21T15:31:36.342529Z","shell.execute_reply.started":"2023-02-21T15:31:36.333077Z","shell.execute_reply":"2023-02-21T15:31:36.341585Z"},"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-21T15:31:36.343831Z","iopub.execute_input":"2023-02-21T15:31:36.344170Z","iopub.status.idle":"2023-02-21T15:31:40.842338Z","shell.execute_reply.started":"2023-02-21T15:31:36.344141Z","shell.execute_reply":"2023-02-21T15:31:40.840982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()\ntrain_df.max()\nprint(test_df.iloc)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:40.845999Z","iopub.execute_input":"2023-02-21T15:31:40.846513Z","iopub.status.idle":"2023-02-21T15:31:40.927450Z","shell.execute_reply.started":"2023-02-21T15:31:40.846452Z","shell.execute_reply":"2023-02-21T15:31:40.926274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:40.928805Z","iopub.execute_input":"2023-02-21T15:31:40.929278Z","iopub.status.idle":"2023-02-21T15:31:40.963282Z","shell.execute_reply.started":"2023-02-21T15:31:40.929232Z","shell.execute_reply":"2023-02-21T15:31:40.962091Z"},"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-21T15:31:40.964656Z","iopub.execute_input":"2023-02-21T15:31:40.964984Z","iopub.status.idle":"2023-02-21T15:31:40.972549Z","shell.execute_reply.started":"2023-02-21T15:31:40.964953Z","shell.execute_reply":"2023-02-21T15:31:40.971392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_test_img(1)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:40.974080Z","iopub.execute_input":"2023-02-21T15:31:40.974418Z","iopub.status.idle":"2023-02-21T15:31:41.234759Z","shell.execute_reply.started":"2023-02-21T15:31:40.974387Z","shell.execute_reply":"2023-02-21T15:31:41.233922Z"},"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-21T15:31:41.236016Z","iopub.execute_input":"2023-02-21T15:31:41.236529Z","iopub.status.idle":"2023-02-21T15:31:41.243185Z","shell.execute_reply.started":"2023-02-21T15:31:41.236498Z","shell.execute_reply":"2023-02-21T15:31:41.242245Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:41.244798Z","iopub.execute_input":"2023-02-21T15:31:41.245532Z","iopub.status.idle":"2023-02-21T15:31:41.254947Z","shell.execute_reply.started":"2023-02-21T15:31:41.245488Z","shell.execute_reply":"2023-02-21T15:31:41.254186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## test\nmnistDataset1[268][1]","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:41.256260Z","iopub.execute_input":"2023-02-21T15:31:41.256685Z","iopub.status.idle":"2023-02-21T15:31:41.332989Z","shell.execute_reply.started":"2023-02-21T15:31:41.256645Z","shell.execute_reply":"2023-02-21T15:31:41.331811Z"},"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-21T15:31:41.334578Z","iopub.execute_input":"2023-02-21T15:31:41.335101Z","iopub.status.idle":"2023-02-21T15:31:41.341759Z","shell.execute_reply.started":"2023-02-21T15:31:41.335069Z","shell.execute_reply":"2023-02-21T15:31:41.340737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = MLP()\nprint(model1)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:41.345443Z","iopub.execute_input":"2023-02-21T15:31:41.346519Z","iopub.status.idle":"2023-02-21T15:31:41.363489Z","shell.execute_reply.started":"2023-02-21T15:31:41.346461Z","shell.execute_reply":"2023-02-21T15:31:41.362154Z"},"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-21T15:31:41.365374Z","iopub.execute_input":"2023-02-21T15:31:41.366231Z","iopub.status.idle":"2023-02-21T15:31:41.374803Z","shell.execute_reply.started":"2023-02-21T15:31:41.366196Z","shell.execute_reply":"2023-02-21T15:31:41.373735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit(model1, dataloader1)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:31:41.376528Z","iopub.execute_input":"2023-02-21T15:31:41.377065Z","iopub.status.idle":"2023-02-21T15:37:14.948194Z","shell.execute_reply.started":"2023-02-21T15:31:41.377000Z","shell.execute_reply":"2023-02-21T15:37:14.946994Z"},"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-21T15:37:14.949684Z","iopub.execute_input":"2023-02-21T15:37:14.950177Z","iopub.status.idle":"2023-02-21T15:37:21.211153Z","shell.execute_reply.started":"2023-02-21T15:37:14.950141Z","shell.execute_reply":"2023-02-21T15:37:21.210109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:37:21.212408Z","iopub.execute_input":"2023-02-21T15:37:21.212759Z","iopub.status.idle":"2023-02-21T15:37:21.241687Z","shell.execute_reply.started":"2023-02-21T15:37:21.212728Z","shell.execute_reply":"2023-02-21T15:37:21.240828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd['Label'] = res","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:37:21.242796Z","iopub.execute_input":"2023-02-21T15:37:21.243652Z","iopub.status.idle":"2023-02-21T15:37:21.252399Z","shell.execute_reply.started":"2023-02-21T15:37:21.243619Z","shell.execute_reply":"2023-02-21T15:37:21.251205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pd.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-21T15:37:21.253998Z","iopub.execute_input":"2023-02-21T15:37:21.254375Z","iopub.status.idle":"2023-02-21T15:37:21.268398Z","shell.execute_reply.started":"2023-02-21T15:37:21.254331Z","shell.execute_reply":"2023-02-21T15:37:21.267068Z"},"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-21T15:37:21.270141Z","iopub.execute_input":"2023-02-21T15:37:21.270473Z","iopub.status.idle":"2023-02-21T15:37:21.305805Z","shell.execute_reply.started":"2023-02-21T15:37:21.270445Z","shell.execute_reply":"2023-02-21T15:37:21.304404Z"},"trusted":true},"execution_count":null,"outputs":[]}]}