{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nfrom torchvision import datasets\nimport torchvision.transforms as transforms\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_on_gpu = torch.cuda.is_available()\n\nif not train_on_gpu:\n    print('cuda not available. train on cpu.')\nelse:\n    print('cuda all dayyy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nidle = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nX_idle = idle.drop(columns='label').values\ny_idle = idle['label'].values\n\nX_train, X_val, y_train, y_val = train_test_split(X_idle, y_idle, random_state=0)\n\nX_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv').values\n\nX_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"torch_X_train = torch.from_numpy(X_train).type(torch.LongTensor).view(-1, 1, 28, 28).float()\ntorch_y_train = torch.from_numpy(y_train).type(torch.LongTensor)\n\ntorch_X_val = torch.from_numpy(X_val).type(torch.LongTensor).view(-1, 1, 28, 28).float()\ntorch_y_val = torch.from_numpy(y_val).type(torch.LongTensor)\n\ntorch_X_test = torch.from_numpy(X_test).type(torch.LongTensor).view(-1, 1, 28, 28).float()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = torch.utils.data.TensorDataset(torch_X_train, torch_y_train)\nval = torch.utils.data.TensorDataset(torch_X_val, torch_y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_workers = 0\nbatch_size = 20","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size, num_workers=num_workers)\nval_loader = torch.utils.data.DataLoader(val, batch_size=batch_size, num_workers=num_workers)\ntest_loader = torch.utils.data.DataLoader(torch_X_test, batch_size=batch_size, num_workers=num_workers)                       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataiter = iter(train_loader)\nimages, labels = dataiter.next()\nimages = images.numpy()\n\nfig = plt.figure(figsize=(25,4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, 20/2, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(str(labels[idx].item()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class net(nn.Module):\n    def __init__(self):\n        super(net, self).__init__()\n        # input is 28x28x1 image\n        self.conv1 = nn.Conv2d(1,14,3, padding=1)\n        # 14x14x14\n        self.conv2 = nn.Conv2d(14,28,3, padding=1)\n        # 7x7x28\n        self.conv3 = nn.Conv2d(28,56,3, padding=1)\n        \n        self.pool = nn.MaxPool2d(2,2)\n        \n        # 3x3x56\n        self.fc1 = nn.Linear(56*3*3, 100)\n        \n        self.fc2 = nn.Linear(100, 10)\n        \n        self.dropout = nn.Dropout(0.2)\n        \n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = self.pool(F.relu(self.conv3(x)))\n        \n        x = x.view(-1, 56*3*3)\n        \n        x = self.dropout(F.relu(self.fc1(x)))\n        x = self.fc2(x)\n        \n        return x\n\nmodel = net()\nprint(model)\n\nif train_on_gpu:\n    model.cuda()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch.optim as optim\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.01)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_epochs = 30\nstop_count = 0\nval_loss_min = np.Inf\n\nfor epoch in range(1, n_epochs+1):\n    train_loss = 0.0\n    val_loss = 0.0\n    val_accuracy = 0.0\n    batches = 0\n\n    if stop_count >= 10:\n        print('stopping. no improvement in 10 epochs.')\n        break\n        \n    model.train()\n    for data, target in train_loader:\n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\n        optimizer.zero_grad()\n        output = model(data)\n        loss = criterion(output, target)\n        loss.backward()\n        optimizer.step()\n        train_loss += loss.item()*data.size(0)\n        \n    model.eval()\n    with torch.no_grad():\n        for data, target in val_loader:\n            if train_on_gpu:\n                data, target = data.cuda(), target.cuda()\n            output = model(data)\n            loss = criterion(output, target)\n            val_loss += loss.item()*data.size(0)\n\n            top_p, top_class = output.topk(1,dim=1)\n            equals = top_class == target.view(*top_class.shape)\n            batch_accuracy = torch.mean(equals.type(torch.FloatTensor))\n            val_accuracy += batch_accuracy.item()\n            batches += 1\n        \n    train_loss = train_loss / len(train_loader.sampler)\n    val_loss = val_loss / len(val_loader.sampler)\n    val_accuracy = val_accuracy / batches\n    \n    print(f'- - - - -\\nepoch: {epoch}\\ntrain loss: {train_loss}\\nval loss: {val_loss}\\nval accuracy: {val_accuracy}')\n   \n    stop_count += 1\n    \n    if val_loss <= val_loss_min:\n        print(f'val loss decrease: {val_loss_min} --> {val_loss}. saving model...')\n        torch.save(model.state_dict(), 'mnist_cnn.pt')\n        val_loss_min = val_loss\n        stop_count = 0\n    \n\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_state_dict(torch.load('mnist_cnn.pt'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.eval()\ntest_preds = torch.LongTensor()\n\nfor data in test_loader:\n    if train_on_gpu:\n        data = data.cuda()\n    \n    output = model(data)\n    _, top_class = output.cpu().topk(1, dim=1)\n    \n    test_preds = torch.cat((test_preds, top_class), dim=0)\n\ntest_preds    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub['Label'] = test_preds.numpy().squeeze()\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('mnist_pytorch_sub.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}