{"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-01-12T15:05:21.526999Z","iopub.execute_input":"2023-01-12T15:05:21.527835Z","iopub.status.idle":"2023-01-12T15:05:21.555979Z","shell.execute_reply.started":"2023-01-12T15:05:21.527736Z","shell.execute_reply":"2023-01-12T15:05:21.554651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport csv\nimport torch \nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.utils.data import DataLoader, TensorDataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:21.557834Z","iopub.execute_input":"2023-01-12T15:05:21.558164Z","iopub.status.idle":"2023-01-12T15:05:23.510867Z","shell.execute_reply.started":"2023-01-12T15:05:21.558135Z","shell.execute_reply":"2023-01-12T15:05:23.509627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsample = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\ndig = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:23.512138Z","iopub.execute_input":"2023-01-12T15:05:23.512664Z","iopub.status.idle":"2023-01-12T15:05:30.558388Z","shell.execute_reply.started":"2023-01-12T15:05:23.512631Z","shell.execute_reply":"2023-01-12T15:05:30.557246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.560468Z","iopub.execute_input":"2023-01-12T15:05:30.560961Z","iopub.status.idle":"2023-01-12T15:05:30.591576Z","shell.execute_reply.started":"2023-01-12T15:05:30.560927Z","shell.execute_reply":"2023-01-12T15:05:30.590510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.592898Z","iopub.execute_input":"2023-01-12T15:05:30.593256Z","iopub.status.idle":"2023-01-12T15:05:30.599373Z","shell.execute_reply.started":"2023-01-12T15:05:30.593223Z","shell.execute_reply":"2023-01-12T15:05:30.598244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num = 4\nplot_num = train.iloc[num, 1:]\nplot_num = np.array(plot_num).reshape(28, -1)\nplt.imshow(plot_num, cmap='gray')\nplt.title(f'Label: {train.iloc[num, 0]}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.601107Z","iopub.execute_input":"2023-01-12T15:05:30.601504Z","iopub.status.idle":"2023-01-12T15:05:30.845302Z","shell.execute_reply.started":"2023-01-12T15:05:30.601453Z","shell.execute_reply":"2023-01-12T15:05:30.844141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_indices(n, val_pct):\n    n_val = int(n*val_pct)\n    idx = np.random.permutation(n)\n    return idx[:n_val], idx[n_val:]","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.846570Z","iopub.execute_input":"2023-01-12T15:05:30.846874Z","iopub.status.idle":"2023-01-12T15:05:30.855327Z","shell.execute_reply.started":"2023-01-12T15:05:30.846847Z","shell.execute_reply":"2023-01-12T15:05:30.853521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_idx, train_idx = split_indices(len(train), 0.1)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.857557Z","iopub.execute_input":"2023-01-12T15:05:30.858079Z","iopub.status.idle":"2023-01-12T15:05:30.870646Z","shell.execute_reply.started":"2023-01-12T15:05:30.857993Z","shell.execute_reply":"2023-01-12T15:05:30.869246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train.pop('label')\nlabels_train = labels[train_idx]\nlabels_val = labels[val_idx]","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.874790Z","iopub.execute_input":"2023-01-12T15:05:30.875205Z","iopub.status.idle":"2023-01-12T15:05:30.895549Z","shell.execute_reply.started":"2023-01-12T15:05:30.875170Z","shell.execute_reply":"2023-01-12T15:05:30.894402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size_train = 64\nbatch_size_val = 1024\n\nlearning_rate = 0.01\nmomentum=0.5\nlog_interval = 10\n\nrandom_seed = 42\ntorch.backends.cudnn.enabled = False\ntorch.manual_seed(random_seed)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.896820Z","iopub.execute_input":"2023-01-12T15:05:30.897791Z","iopub.status.idle":"2023-01-12T15:05:30.907601Z","shell.execute_reply.started":"2023-01-12T15:05:30.897757Z","shell.execute_reply":"2023-01-12T15:05:30.906453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sampler = SubsetRandomSampler(train_idx)\ntrain_loader = DataLoader(train, batch_size_train, sampler = train_sampler)\n\nval_sampler = SubsetRandomSampler(val_idx)\nval_loader = DataLoader(train, batch_size_val, sampler = val_sampler)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.909058Z","iopub.execute_input":"2023-01-12T15:05:30.909990Z","iopub.status.idle":"2023-01-12T15:05:30.915283Z","shell.execute_reply.started":"2023-01-12T15:05:30.909941Z","shell.execute_reply":"2023-01-12T15:05:30.914238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train.iloc[train_idx, :].values\nval_X = train.iloc[val_idx, :].values\n\ntrain_X = torch.Tensor(train_X.reshape(train_X.shape[0],1,28,-1))\ntrain_y = torch.Tensor(labels_train)\n\nval_X = torch.Tensor(val_X.reshape(val_X.shape[0],1,28,-1))\nval_y = torch.Tensor(labels_val.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:30.916794Z","iopub.execute_input":"2023-01-12T15:05:30.917343Z","iopub.status.idle":"2023-01-12T15:05:31.787986Z","shell.execute_reply.started":"2023-01-12T15:05:30.917312Z","shell.execute_reply":"2023-01-12T15:05:31.786725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = TensorDataset(train_X, train_y)\nval_dataset = TensorDataset(val_X, val_y)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:31.790074Z","iopub.execute_input":"2023-01-12T15:05:31.791197Z","iopub.status.idle":"2023-01-12T15:05:31.796868Z","shell.execute_reply.started":"2023-01-12T15:05:31.791158Z","shell.execute_reply":"2023-01-12T15:05:31.795510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset, batch_size = batch_size_train, shuffle = True)\nval_loader = DataLoader(val_dataset, batch_size = batch_size_val, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:31.798505Z","iopub.execute_input":"2023-01-12T15:05:31.798861Z","iopub.status.idle":"2023-01-12T15:05:31.810396Z","shell.execute_reply.started":"2023-01-12T15:05:31.798830Z","shell.execute_reply":"2023-01-12T15:05:31.809221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = enumerate(val_loader)\nidx, (sample_data, sample_labels) = next(sample)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:31.812306Z","iopub.execute_input":"2023-01-12T15:05:31.812723Z","iopub.status.idle":"2023-01-12T15:05:31.850646Z","shell.execute_reply.started":"2023-01-12T15:05:31.812689Z","shell.execute_reply":"2023-01-12T15:05:31.849187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nfor i in range(6):\n    plt.subplot(2,3,i+1)\n    plt.tight_layout()\n    plt.imshow(sample_data[i+11].view(28, -1), cmap = 'gray', interpolation='none')\n    plt.title(f'Ground truth: {sample_labels[i+11]}')\n    plt.xticks([])\n    plt.yticks([])  ","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:31.852465Z","iopub.execute_input":"2023-01-12T15:05:31.852863Z","iopub.status.idle":"2023-01-12T15:05:32.395980Z","shell.execute_reply.started":"2023-01-12T15:05:31.852828Z","shell.execute_reply":"2023-01-12T15:05:32.394537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = 28 * 28\nnum_classes = 10\n\nmodel = nn.Linear(input_size, num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.398371Z","iopub.execute_input":"2023-01-12T15:05:32.400286Z","iopub.status.idle":"2023-01-12T15:05:32.406210Z","shell.execute_reply.started":"2023-01-12T15:05:32.400214Z","shell.execute_reply":"2023-01-12T15:05:32.404946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(1, 20, kernel_size = 5)\n        self.conv2 = nn.Conv2d(20, 40, kernel_size = 5)\n        self.conv2_drop = nn.Dropout2d()\n        self.fc1 = nn.Linear(640, 120)\n        self.fc2 = nn.Linear(120, 10)\n        \n    def forward(self, x):\n        x = F.relu(F.max_pool2d(self.conv1(x), 2))\n        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))\n        x = x.view(-1, 640)\n        x = F.relu(self.fc1(x))\n        x = F.dropout(x, training = self.training)\n        x = self.fc2(x)\n        return F.log_softmax(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.408026Z","iopub.execute_input":"2023-01-12T15:05:32.408378Z","iopub.status.idle":"2023-01-12T15:05:32.419673Z","shell.execute_reply.started":"2023-01-12T15:05:32.408346Z","shell.execute_reply":"2023-01-12T15:05:32.418426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network = net() #instantiate network\noptimizer = optim.SGD(network.parameters(), lr = learning_rate, momentum = momentum)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.422110Z","iopub.execute_input":"2023-01-12T15:05:32.423012Z","iopub.status.idle":"2023-01-12T15:05:32.439711Z","shell.execute_reply.started":"2023-01-12T15:05:32.422940Z","shell.execute_reply":"2023-01-12T15:05:32.438185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = [] \ntrain_counter = []\nval_losses = []\nval_counter = []","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.441551Z","iopub.execute_input":"2023-01-12T15:05:32.442490Z","iopub.status.idle":"2023-01-12T15:05:32.451183Z","shell.execute_reply.started":"2023-01-12T15:05:32.442424Z","shell.execute_reply":"2023-01-12T15:05:32.450017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(epoch):\n    network.train()\n    for batch_idx, (data, target) in enumerate(train_loader):\n        optimizer.zero_grad()\n        output = network(data)\n        loss = F.nll_loss(output, target.type(torch.LongTensor))\n        loss.backward()\n        optimizer.step()\n        \n        if batch_idx % log_interval == 0:\n            print(f'Train Epoch: {epoch} [{batch_idx*len(data)}/{len(train_loader.dataset)}] \\\n            {100*batch_idx/len(train_loader):.0f},\\tLoss: {loss.item():.6f}')\n            train_losses.append(loss.item())\n            train_counter.append(batch_idx*64 + (epoch-1)*len(train_loader.dataset))","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.453658Z","iopub.execute_input":"2023-01-12T15:05:32.454627Z","iopub.status.idle":"2023-01-12T15:05:32.466814Z","shell.execute_reply.started":"2023-01-12T15:05:32.454578Z","shell.execute_reply":"2023-01-12T15:05:32.465544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def val():\n    network.eval()\n    val_loss = 0\n    correct = 0\n    \n    with torch.no_grad():\n        for data, target in val_loader:\n            output = network(data)\n            val_loss += F.nll_loss(output, target.type(torch.LongTensor), size_average=False).item()\n            pred = output.data.max(1, keepdim=True)[1]\n            correct += pred.eq(target.data.view_as(pred)).sum()\n            \n    val_loss /= len(val_loader.dataset)\n    val_losses.append(val_loss)\n    print(f'Val set: Avg loss {val_loss:.4f}, Accuracy: {correct}/{len(val_loader.dataset)}, \\\n    ({100*correct/len(val_loader.dataset)})')  ","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.468314Z","iopub.execute_input":"2023-01-12T15:05:32.474908Z","iopub.status.idle":"2023-01-12T15:05:32.485776Z","shell.execute_reply.started":"2023-01-12T15:05:32.474836Z","shell.execute_reply":"2023-01-12T15:05:32.484424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 3\nval()\nfor epoch in range(1, n_epochs + 1):\n    train(epoch)\n    val()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:05:32.488612Z","iopub.execute_input":"2023-01-12T15:05:32.489328Z","iopub.status.idle":"2023-01-12T15:06:31.949481Z","shell.execute_reply.started":"2023-01-12T15:05:32.489253Z","shell.execute_reply":"2023-01-12T15:06:31.948291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = test.pop('id')\n\ntest_pred = test.values.reshape(test.shape[0], 1, 28, -1)\ntest_pred = torch.Tensor(test_pred)\n\noutput = network(test_pred)\n\npreds = output.data.max(1)[1]","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:06:31.954287Z","iopub.execute_input":"2023-01-12T15:06:31.954750Z","iopub.status.idle":"2023-01-12T15:06:32.815147Z","shell.execute_reply.started":"2023-01-12T15:06:31.954716Z","shell.execute_reply":"2023-01-12T15:06:32.814146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame()\nsub['id'] = test_ids\nsub['label'] = preds\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T15:06:52.924140Z","iopub.execute_input":"2023-01-12T15:06:52.924748Z","iopub.status.idle":"2023-01-12T15:06:52.944400Z","shell.execute_reply.started":"2023-01-12T15:06:52.924708Z","shell.execute_reply":"2023-01-12T15:06:52.943114Z"},"trusted":true},"execution_count":null,"outputs":[]}]}