{"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":"# importing libraries\nimport math\nimport numpy as np\nimport os\nimport torch\nimport torchvision\nimport tarfile\nfrom torch.utils.data import random_split\nfrom torchvision.datasets import ImageFolder\nfrom torchvision.transforms import ToTensor,ToPILImage\nimport matplotlib.pyplot as plt\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:20:41.504353Z","iopub.execute_input":"2022-08-23T17:20:41.505206Z","iopub.status.idle":"2022-08-23T17:20:44.083764Z","shell.execute_reply.started":"2022-08-23T17:20:41.505115Z","shell.execute_reply":"2022-08-23T17:20:44.082133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"darkgrid\")","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:20:44.231793Z","iopub.execute_input":"2022-08-23T17:20:44.232427Z","iopub.status.idle":"2022-08-23T17:20:44.238902Z","shell.execute_reply.started":"2022-08-23T17:20:44.232389Z","shell.execute_reply":"2022-08-23T17:20:44.237197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:20:44.932332Z","iopub.execute_input":"2022-08-23T17:20:44.932697Z","iopub.status.idle":"2022-08-23T17:20:45.005633Z","shell.execute_reply.started":"2022-08-23T17:20:44.932665Z","shell.execute_reply":"2022-08-23T17:20:45.003277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = torchvision.datasets.CIFAR10(root='/gdrive/My Drive/Colab Notebooks/Data_files/cifar10', train=True, download=True, transform=ToTensor())\ndataset_test = torchvision.datasets.CIFAR10(root='/gdrive/My Drive/Colab Notebooks/Data_files/cifar10', train=False, transform=ToTensor())","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:21:45.031669Z","iopub.execute_input":"2022-08-23T17:21:45.032050Z","iopub.status.idle":"2022-08-23T17:21:46.258431Z","shell.execute_reply.started":"2022-08-23T17:21:45.032016Z","shell.execute_reply":"2022-08-23T17:21:46.257444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed = 42\ntorch.manual_seed(random_seed)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:21:47.241251Z","iopub.execute_input":"2022-08-23T17:21:47.242367Z","iopub.status.idle":"2022-08-23T17:21:47.250029Z","shell.execute_reply.started":"2022-08-23T17:21:47.242321Z","shell.execute_reply":"2022-08-23T17:21:47.248944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_size = 5000\ntrain_size = len(dataset) - val_size\n\ntrain_data, val_data = random_split(dataset, [train_size, val_size], generator=torch.manual_seed(random_seed))\nlen(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:21:50.307001Z","iopub.execute_input":"2022-08-23T17:21:50.307378Z","iopub.status.idle":"2022-08-23T17:21:50.320559Z","shell.execute_reply.started":"2022-08-23T17:21:50.307346Z","shell.execute_reply":"2022-08-23T17:21:50.319519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 256\n\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size)\nval_loader = torch.utils.data.DataLoader(val_data, batch_size=batch_size)\ntest_loader = torch.utils.data.DataLoader(dataset_test, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:21:51.405952Z","iopub.execute_input":"2022-08-23T17:21:51.406654Z","iopub.status.idle":"2022-08-23T17:21:51.412895Z","shell.execute_reply.started":"2022-08-23T17:21:51.406616Z","shell.execute_reply":"2022-08-23T17:21:51.411546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Neural_Network(nn.Module):\n    def __init__(self):\n        super(Neural_Network, self).__init__()\n        self.fc1 = nn.Linear(32*32*3, 512)\n        self.fc2 = nn.Linear(512,256)\n        self.fc3 = nn.Linear(256,10)\n        \n    def forward(self, image):\n        image = image.view(-1, 32*32*3)\n        \n        out = F.relu(self.fc1(image))\n        out = F.relu(self.fc2(out))\n        out = self.fc3(out)\n        \n        return out","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:21:52.942215Z","iopub.execute_input":"2022-08-23T17:21:52.942589Z","iopub.status.idle":"2022-08-23T17:21:52.952872Z","shell.execute_reply.started":"2022-08-23T17:21:52.942557Z","shell.execute_reply":"2022-08-23T17:21:52.951737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_first_nn = Neural_Network()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:22:09.560997Z","iopub.execute_input":"2022-08-23T17:22:09.561383Z","iopub.status.idle":"2022-08-23T17:22:09.583424Z","shell.execute_reply.started":"2022-08-23T17:22:09.561351Z","shell.execute_reply":"2022-08-23T17:22:09.582347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(my_first_nn.parameters(), lr=0.005)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:22:10.909377Z","iopub.execute_input":"2022-08-23T17:22:10.909971Z","iopub.status.idle":"2022-08-23T17:22:10.914843Z","shell.execute_reply.started":"2022-08-23T17:22:10.909933Z","shell.execute_reply":"2022-08-23T17:22:10.913887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 100\n\nvalid_loss_min = np.Inf\n\nfor epoch in range(num_epochs):\n    \n    train_loss, val_loss = 0,0\n    \n    # Set the mode \n    my_first_nn.train()\n    \n    for data, label in train_loader:\n\n        # Clear any possible accumulated gradients\n        optimizer.zero_grad()\n\n        output = my_first_nn(data)\n        \n        loss = criterion(output, label)\n        # compute grad\n        loss.backward()\n        optimizer.step()\n        \n        train_loss += loss.item() * data.size(0)\n        \n    # Set the model in eval mode\n    my_first_nn.eval()\n    \n    for data, label in val_loader:\n        \n        output= my_first_nn(data)\n        loss = criterion(output, label)\n        val_loss += loss.item() * data.size(0)\n        \n    train_loss/=train_size\n    val_loss/=val_size\n    \n    print('Epoch: {} \\tTraining Loss: {:.6f} \\tValidation Loss: {:.6f}'.format(\n        epoch+1, \n        train_loss,\n        val_loss\n        ))\n    \n    if val_loss<valid_loss_min:\n        \n        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        valid_loss_min,\n        val_loss))\n        torch.save(my_first_nn.state_dict(), 'model.pt')\n        valid_loss_min = val_loss","metadata":{"execution":{"iopub.status.busy":"2022-08-23T17:22:28.357418Z","iopub.execute_input":"2022-08-23T17:22:28.357886Z","iopub.status.idle":"2022-08-23T17:39:23.675804Z","shell.execute_reply.started":"2022-08-23T17:22:28.357850Z","shell.execute_reply":"2022-08-23T17:39:23.674543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\n\nmy_first_nn.eval()\n    \nfor data, label in test_loader:\n\n    output= my_first_nn(data)\n    \n    for tensor in output:\n        prediction = torch.argmax(tensor).item()\n        preds.append(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:05:50.878941Z","iopub.execute_input":"2022-08-23T18:05:50.880208Z","iopub.status.idle":"2022-08-23T18:05:52.540537Z","shell.execute_reply.started":"2022-08-23T18:05:50.880170Z","shell.execute_reply":"2022-08-23T18:05:52.539582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = list(range(1, len(preds)+1))\nids.sort(key=lambda x: str(x))","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:05:53.977796Z","iopub.execute_input":"2022-08-23T18:05:53.978329Z","iopub.status.idle":"2022-08-23T18:05:53.987155Z","shell.execute_reply.started":"2022-08-23T18:05:53.978286Z","shell.execute_reply":"2022-08-23T18:05:53.985862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:06:09.357920Z","iopub.execute_input":"2022-08-23T18:06:09.358929Z","iopub.status.idle":"2022-08-23T18:06:09.365631Z","shell.execute_reply.started":"2022-08-23T18:06:09.358880Z","shell.execute_reply":"2022-08-23T18:06:09.364572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame({'id': ids, 'label': preds})\ndf['label'] = df['label'].apply(lambda x: dataset.classes[x])\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:06:19.886973Z","iopub.execute_input":"2022-08-23T18:06:19.887921Z","iopub.status.idle":"2022-08-23T18:06:19.921996Z","shell.execute_reply.started":"2022-08-23T18:06:19.887881Z","shell.execute_reply":"2022-08-23T18:06:19.920900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels = []\n\nfor data,label in dataset_test:\n    test_labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:10:30.104983Z","iopub.execute_input":"2022-08-23T18:10:30.106195Z","iopub.status.idle":"2022-08-23T18:10:31.352675Z","shell.execute_reply.started":"2022-08-23T18:10:30.106144Z","shell.execute_reply":"2022-08-23T18:10:31.351741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nacc = accuracy_score(test_labels, preds)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:11:21.938602Z","iopub.execute_input":"2022-08-23T18:11:21.939220Z","iopub.status.idle":"2022-08-23T18:11:22.039351Z","shell.execute_reply.started":"2022-08-23T18:11:21.939171Z","shell.execute_reply":"2022-08-23T18:11:22.038104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:11:25.417656Z","iopub.execute_input":"2022-08-23T18:11:25.418040Z","iopub.status.idle":"2022-08-23T18:11:25.424543Z","shell.execute_reply.started":"2022-08-23T18:11:25.417995Z","shell.execute_reply":"2022-08-23T18:11:25.423492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}