{"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":"2022-04-29T14:49:03.817265Z","iopub.execute_input":"2022-04-29T14:49:03.817648Z","iopub.status.idle":"2022-04-29T14:49:03.847471Z","shell.execute_reply.started":"2022-04-29T14:49:03.817535Z","shell.execute_reply":"2022-04-29T14:49:03.846522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport torch\nimport torch.nn as nn\nfrom torch import optim\nfrom torch.autograd import Variable\nimport torch.nn.functional as F\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:35:50.665949Z","iopub.execute_input":"2022-05-20T12:35:50.666484Z","iopub.status.idle":"2022-05-20T12:35:50.672010Z","shell.execute_reply.started":"2022-05-20T12:35:50.666439Z","shell.execute_reply":"2022-05-20T12:35:50.671249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:17:55.161991Z","iopub.execute_input":"2022-05-20T12:17:55.162241Z","iopub.status.idle":"2022-05-20T12:17:58.159519Z","shell.execute_reply.started":"2022-05-20T12:17:55.162215Z","shell.execute_reply":"2022-05-20T12:17:58.158438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['label'].values\nX = df.drop(['label'], axis=1).values\n\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:01.118577Z","iopub.execute_input":"2022-05-20T12:18:01.118896Z","iopub.status.idle":"2022-05-20T12:18:01.656671Z","shell.execute_reply.started":"2022-05-20T12:18:01.118861Z","shell.execute_reply":"2022-05-20T12:18:01.655623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 20\n\ntorch_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_test = torch.from_numpy(X_test).type(torch.LongTensor).view(-1, 1,28,28).float()\ntorch_y_test = torch.from_numpy(y_test).type(torch.LongTensor)\n\ntrain = torch.utils.data.TensorDataset(torch_X_train,torch_y_train)\ntest = torch.utils.data.TensorDataset(torch_X_test,torch_y_test)\n\n\ntrain_loader = torch.utils.data.DataLoader(train, batch_size = BATCH_SIZE, shuffle = False)\ntest_loader = torch.utils.data.DataLoader(test, batch_size = BATCH_SIZE, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:01.658154Z","iopub.execute_input":"2022-05-20T12:18:01.658374Z","iopub.status.idle":"2022-05-20T12:18:01.747828Z","shell.execute_reply.started":"2022-05-20T12:18:01.658348Z","shell.execute_reply":"2022-05-20T12:18:01.747215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, kernel_size=3, padding=1, stride=1)\n        self.conv2 = nn.Conv2d(in_channels=10, out_channels=20, kernel_size=3, padding=1, stride=1)\n        self.conv3 = nn.Conv2d(in_channels=20, out_channels=40, kernel_size=3, padding=1, stride=1)\n        self.pool = nn.MaxPool2d(kernel_size=2)\n        self.fc1 = nn.Linear(in_features=40 * 3 * 3, out_features=128)\n        self.fc2 = nn.Linear(in_features=128, out_features=64)\n        self.fc3 = nn.Linear(in_features=64, out_features=10)\n        self.dropout = nn.Dropout(0.25)\n        self.softmax = nn.LogSoftmax(dim=1)\n\n    def forward(self, x):\n#         print(x.shape)\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        x = x.view(x.size(0), -1)\n\n\n        x = F.relu(self.fc1(x))\n        x = self.dropout(x)\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return self.softmax(x)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:01.803522Z","iopub.execute_input":"2022-05-20T12:18:01.803809Z","iopub.status.idle":"2022-05-20T12:18:01.813194Z","shell.execute_reply.started":"2022-05-20T12:18:01.803777Z","shell.execute_reply":"2022-05-20T12:18:01.812198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(epochs, model, criterion, optimizer):\n    \n    criterion = nn.CrossEntropyLoss()\n    optimizer = optim.SGD(model.parameters(), lr=0.003)\n\n    \n    for e in range(epochs):\n        running_loss = 0\n        for images, labels in train_loader:\n\n            optimizer.zero_grad()\n            output = model(images)\n            loss = criterion(output, labels)\n            loss.backward()\n            optimizer.step()\n         \n            running_loss += loss.data\n        else:\n            print(f\" Epoch {e}, Training loss: {running_loss/len(train_loader)}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:02.272309Z","iopub.execute_input":"2022-05-20T12:18:02.272628Z","iopub.status.idle":"2022-05-20T12:18:02.278654Z","shell.execute_reply.started":"2022-05-20T12:18:02.272574Z","shell.execute_reply":"2022-05-20T12:18:02.278018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_net = Net()\n# criterion = nn.CrossEntropyLoss()\n# optimizer = optim.SGD(model_net.parameters(), lr=0.003)\n# epochs = 10","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:02.692986Z","iopub.execute_input":"2022-05-20T12:18:02.693445Z","iopub.status.idle":"2022-05-20T12:18:02.697882Z","shell.execute_reply.started":"2022-05-20T12:18:02.693398Z","shell.execute_reply":"2022-05-20T12:18:02.697231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(epochs, model, criterion, optimizer):\n    \n    for e in range(epochs):\n        running_loss = 0\n        for images, labels in train_loader:\n\n            optimizer.zero_grad()\n            output = model(images)\n            loss = criterion(output, labels)\n            loss.backward()\n            optimizer.step()\n         \n            running_loss += loss.data\n        else:\n            print(f\" Epoch {e}, Training loss: {running_loss/len(train_loader)}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:18:04.163690Z","iopub.execute_input":"2022-05-20T12:18:04.164461Z","iopub.status.idle":"2022-05-20T12:18:04.170787Z","shell.execute_reply.started":"2022-05-20T12:18:04.164410Z","shell.execute_reply":"2022-05-20T12:18:04.170072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nmodel_net = Net()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model_net.parameters(), lr=0.003)\n\nfit(epochs, model_net, criterion, optimizer)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:21:24.831079Z","iopub.execute_input":"2022-05-20T12:21:24.831361Z","iopub.status.idle":"2022-05-20T12:23:13.961118Z","shell.execute_reply.started":"2022-05-20T12:21:24.831333Z","shell.execute_reply":"2022-05-20T12:23:13.960441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef predict(model, criterion):\n    test_loss = 0.0\n    class_correct = list(0. for i in range(10))\n    class_total = list(0. for i in range(10))\n    classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n\n    for data, target in test_loader:\n        \n        output = model_net(data)\n        loss = criterion(output, target)\n        \n        test_loss += loss.item()*data.size(0)\n        _, pred = torch.max(output, 1)    \n\n        correct_tensor = pred.eq(target.data.view_as(pred))\n        correct = np.squeeze(correct_tensor.numpy())\n\n        for i in range(BATCH_SIZE):\n            label = target.data[i]\n            class_correct[label] += correct[i].item()\n            class_total[label] += 1\n\n    test_loss = test_loss/len(test_loader.dataset)\n    print(f'Test Loss: {round(test_loss, 3)}\\n')\n\n    for i in range(10):\n        if class_total[i] > 0:\n            print(f\"Test Accuracy of {classes[i]}: {round(100 * class_correct[i] / class_total[i], 1)} ({np.sum(class_correct[i])}/{np.sum(class_total[i])})\")\n        else:\n            print('Test Accuracy of %5s: N/A (no training examples)' % (classes[i]))\n\n    print(f'\\nTest Accuracy {round(100 * np.sum(class_correct) / np.sum(class_total), 1)}: ({np.sum(class_correct)}/{np.sum(class_total)})')\n\npredict(model_net, criterion)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:30:17.317629Z","iopub.execute_input":"2022-05-20T12:30:17.318064Z","iopub.status.idle":"2022-05-20T12:30:18.194293Z","shell.execute_reply.started":"2022-05-20T12:30:17.318032Z","shell.execute_reply":"2022-05-20T12:30:18.193631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest = torch.from_numpy(df_test.values).type(torch.LongTensor).view(-1, 1,28,28).float()\ntestloader = torch.utils.data.DataLoader(test, batch_size=10, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:34:02.854823Z","iopub.execute_input":"2022-05-20T12:34:02.855195Z","iopub.status.idle":"2022-05-20T12:34:04.632899Z","shell.execute_reply.started":"2022-05-20T12:34:02.855150Z","shell.execute_reply":"2022-05-20T12:34:04.632177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_values(model):\n    px = pd.DataFrame()\n    for test_imgs in testloader:\n        test_imgs = Variable(test_imgs)\n        output = model(test_imgs)\n        predicted = torch.max(output,1)[1]\n        px = px.append(pd.DataFrame({'ImageId': 0, 'Label': predicted.numpy()}))\n\n    px['ImageId'] = range(1, len(px) + 1)\n    return px","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:35:13.612510Z","iopub.execute_input":"2022-05-20T12:35:13.612773Z","iopub.status.idle":"2022-05-20T12:35:13.618891Z","shell.execute_reply.started":"2022-05-20T12:35:13.612746Z","shell.execute_reply":"2022-05-20T12:35:13.617911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predict = predict_values(model_net)\ndf_predict.to_csv('submission.csv', sep=',', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T12:36:02.665066Z","iopub.execute_input":"2022-05-20T12:36:02.665336Z","iopub.status.idle":"2022-05-20T12:36:09.962852Z","shell.execute_reply.started":"2022-05-20T12:36:02.665308Z","shell.execute_reply":"2022-05-20T12:36:09.962260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}