{"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-06-08T14:26:18.966561Z","iopub.execute_input":"2022-06-08T14:26:18.967265Z","iopub.status.idle":"2022-06-08T14:26:18.99498Z","shell.execute_reply.started":"2022-06-08T14:26:18.967178Z","shell.execute_reply":"2022-06-08T14:26:18.994186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os \nimport numpy as np\nimport matplotlib.pyplot as plt\nimport torch\nimport torch.optim as optim\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:16:37.057066Z","iopub.execute_input":"2022-06-08T15:16:37.058127Z","iopub.status.idle":"2022-06-08T15:16:37.34578Z","shell.execute_reply.started":"2022-06-08T15:16:37.058081Z","shell.execute_reply":"2022-06-08T15:16:37.344816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('/kaggle/input/digit-recognizer'))\nroot_path = '/kaggle/input/digit-recognizer'","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:16:40.860122Z","iopub.execute_input":"2022-06-08T15:16:40.860545Z","iopub.status.idle":"2022-06-08T15:16:40.86791Z","shell.execute_reply.started":"2022-06-08T15:16:40.860503Z","shell.execute_reply":"2022-06-08T15:16:40.866947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(root_path + '/train.csv')\ntest_df = pd.read_csv(root_path + '/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:16:42.770032Z","iopub.execute_input":"2022-06-08T15:16:42.770401Z","iopub.status.idle":"2022-06-08T15:16:47.737327Z","shell.execute_reply.started":"2022-06-08T15:16:42.770354Z","shell.execute_reply":"2022-06-08T15:16:47.736435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = A.Compose(\n [\n    ToTensorV2(),\n ]\n)\n    \nval_transform = A.Compose(\n [\n    ToTensorV2(),\n ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:49:19.374185Z","iopub.execute_input":"2022-06-08T15:49:19.374736Z","iopub.status.idle":"2022-06-08T15:49:19.379988Z","shell.execute_reply.started":"2022-06-08T15:49:19.374687Z","shell.execute_reply":"2022-06-08T15:49:19.378974Z"},"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(1, 8, kernel_size = 3)\n        self.conv2 = nn.Conv2d(8, 16, kernel_size = 3)\n        self.fc1 = nn.Linear(16 * 5 * 5, 256)\n        self.fc2 = nn.Linear(256, 128)\n        self.fc3 = nn.Linear(128, 10)\n    \n    def forward(self, x):\n        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))\n        print(x.shape)\n        x = F.max_pool2d(F.relu(self.conv2(x)), 2)\n        print(x.shape)\n        x = torch.flatten(x, 1)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:49:21.101776Z","iopub.execute_input":"2022-06-08T15:49:21.102148Z","iopub.status.idle":"2022-06-08T15:49:21.11419Z","shell.execute_reply.started":"2022-06-08T15:49:21.102116Z","shell.execute_reply":"2022-06-08T15:49:21.113334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MnistDataset(Dataset):\n    def __init__(self, df, transforms=None):\n        self.y = df['label'] \n        self.transforms = transforms\n\n        x = df.drop('label', axis=1)\n        self.x = x.values.reshape(x.shape[0], 28, 28, 1)\n\n    def __len__(self):\n        return len(self.x)\n\n    def __getitem__(self, idx):\n        image = self.x[idx]\n        label = self.y[idx]\n        if self.transforms:\n            image = self.transforms(image = image)['image']\n        return image, label\n","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:49:23.542575Z","iopub.execute_input":"2022-06-08T15:49:23.543004Z","iopub.status.idle":"2022-06-08T15:49:23.550601Z","shell.execute_reply.started":"2022-06-08T15:49:23.542969Z","shell.execute_reply":"2022-06-08T15:49:23.54937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_dataset = MnistDataset(train_df, train_transform)\ntrain_loader = DataLoader(mnist_dataset, batch_size=16, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:49:25.648425Z","iopub.execute_input":"2022-06-08T15:49:25.648822Z","iopub.status.idle":"2022-06-08T15:49:25.734787Z","shell.execute_reply.started":"2022-06-08T15:49:25.648788Z","shell.execute_reply":"2022-06-08T15:49:25.733707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = Net()\nprint(net)\ninput = torch.randn(1, 1, 28, 28)\nout = net(input)\nprint(out)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T15:20:32.724065Z","iopub.execute_input":"2022-06-08T15:20:32.724658Z","iopub.status.idle":"2022-06-08T15:20:32.764685Z","shell.execute_reply.started":"2022-06-08T15:20:32.724608Z","shell.execute_reply":"2022-06-08T15:20:32.763589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)","metadata":{},"execution_count":null,"outputs":[]}]}