{"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":"import pandas as pd \nimport numpy as np \nimport torch \nfrom torch import nn \nfrom torch.utils.data import Dataset,DataLoader\nimport matplotlib.pyplot as plt \n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:10.690530Z","iopub.execute_input":"2022-07-22T15:56:10.691145Z","iopub.status.idle":"2022-07-22T15:56:10.703413Z","shell.execute_reply.started":"2022-07-22T15:56:10.691094Z","shell.execute_reply":"2022-07-22T15:56:10.701717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset = pd.read_csv('../input/digit-recognizer/train.csv')\ntestset = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:10.706171Z","iopub.execute_input":"2022-07-22T15:56:10.707567Z","iopub.status.idle":"2022-07-22T15:56:16.126449Z","shell.execute_reply.started":"2022-07-22T15:56:10.707476Z","shell.execute_reply":"2022-07-22T15:56:16.125027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainLabels = trainset.loc[:,'label']\ntrainSamples = trainset.loc[:,'pixel0':]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.128705Z","iopub.execute_input":"2022-07-22T15:56:16.129279Z","iopub.status.idle":"2022-07-22T15:56:16.138015Z","shell.execute_reply.started":"2022-07-22T15:56:16.129230Z","shell.execute_reply":"2022-07-22T15:56:16.136491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainSamples /= 255","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.139817Z","iopub.execute_input":"2022-07-22T15:56:16.140971Z","iopub.status.idle":"2022-07-22T15:56:16.281861Z","shell.execute_reply.started":"2022-07-22T15:56:16.140924Z","shell.execute_reply":"2022-07-22T15:56:16.280523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset,DataLoader\n\nclass datasets(Dataset):\n    def __init__(self,images=trainSamples,labels = trainLabels):\n        self.images = images\n        self.labels = trainLabels\n        \n        self.len = len(images)\n        \n    def __len__(self):\n        return self.len \n    \n    def __getitem__(self,i):\n        \n        x = torch.tensor(np.array(trainSamples.loc[i,:]).reshape((28,28)),dtype=torch.float32).unsqueeze(0)\n        y = torch.tensor(np.array(self.labels[i]),dtype=torch.float32)\n        return x,y","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.285413Z","iopub.execute_input":"2022-07-22T15:56:16.285734Z","iopub.status.idle":"2022-07-22T15:56:16.295515Z","shell.execute_reply.started":"2022-07-22T15:56:16.285705Z","shell.execute_reply":"2022-07-22T15:56:16.294057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = datasets()\ntrainLoader = DataLoader(dataset,batch_size=16,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.297051Z","iopub.execute_input":"2022-07-22T15:56:16.298094Z","iopub.status.idle":"2022-07-22T15:56:16.308187Z","shell.execute_reply.started":"2022-07-22T15:56:16.298034Z","shell.execute_reply":"2022-07-22T15:56:16.306672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice,torch.cuda.get_device_name()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.309929Z","iopub.execute_input":"2022-07-22T15:56:16.311187Z","iopub.status.idle":"2022-07-22T15:56:16.323519Z","shell.execute_reply.started":"2022-07-22T15:56:16.311139Z","shell.execute_reply":"2022-07-22T15:56:16.322055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Cnn(nn.Module):\n    def __init__(self) -> None:\n        super(Cnn,self).__init__()\n\n        self.conv = nn.Sequential(\n            nn.Conv2d(1,64,5,1), \n            nn.ReLU(), # 24x 24\n            nn.MaxPool2d(2,2), # 16x 12 x 12\n\n            nn.Conv2d(64,128,3), \n            nn.ReLU(),\n            nn.MaxPool2d(2,2), # 32x5x5\n\n            nn.Conv2d(128,128,5), # 64x 1x14\n            nn.ReLU(),\n\n            nn.Conv2d(128,10,1,1) # 10x1x1\n            \n        )\n    def forward(self,x):\n        x = self.conv(x)\n        return x\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.325977Z","iopub.execute_input":"2022-07-22T15:56:16.326554Z","iopub.status.idle":"2022-07-22T15:56:16.338733Z","shell.execute_reply.started":"2022-07-22T15:56:16.326510Z","shell.execute_reply":"2022-07-22T15:56:16.337386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Cnn()\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.342460Z","iopub.execute_input":"2022-07-22T15:56:16.343074Z","iopub.status.idle":"2022-07-22T15:56:16.358738Z","shell.execute_reply.started":"2022-07-22T15:56:16.343038Z","shell.execute_reply":"2022-07-22T15:56:16.357370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lossfn = nn.CrossEntropyLoss()\noptimizer = torch .optim.Adam(model.parameters(),lr = 3e-4)\nepochs = 30\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.360532Z","iopub.execute_input":"2022-07-22T15:56:16.361391Z","iopub.status.idle":"2022-07-22T15:56:16.368642Z","shell.execute_reply.started":"2022-07-22T15:56:16.361347Z","shell.execute_reply":"2022-07-22T15:56:16.367163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tqdm import tqdm\nfrom tqdm.notebook import tqdm\n\ndef train(model,optimizer,lossfn,device,trainloader):\n    model.train()\n    correct =0\n    totalLoss = 0\n    \n    for x,y in tqdm(trainloader):\n        \n        x,y = x.type(torch.float),y.type(torch.LongTensor)\n        x,y = x.to(device),y.to(device)\n        output = model(x)\n        output = torch.flatten(output,start_dim=1)\n        loss = lossfn(output,y)\n\n        totalLoss +=loss.item()\n#         preds = torch.argmax(output,dim=1).cpu().detach()\n#         print(preds)\n#         correct +=(output.cpu().detach()==preds).sum().item()\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    \n    print(f'Loss{totalLoss/len(trainloader)}')\n    return totalLoss/len(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.371001Z","iopub.execute_input":"2022-07-22T15:56:16.372205Z","iopub.status.idle":"2022-07-22T15:56:16.384560Z","shell.execute_reply.started":"2022-07-22T15:56:16.372156Z","shell.execute_reply":"2022-07-22T15:56:16.383093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainloss=[]\n\nfor e in range(epochs):\n    print(f'Epoch: {e+1}/{epochs}')\n    tl= train(model,optimizer,lossfn,device,trainLoader)\n    trainloss.append(tl)\n# trainacc.append(ta)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T15:56:16.386719Z","iopub.execute_input":"2022-07-22T15:56:16.387698Z","iopub.status.idle":"2022-07-22T16:07:06.418889Z","shell.execute_reply.started":"2022-07-22T15:56:16.387650Z","shell.execute_reply":"2022-07-22T16:07:06.417441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(epochs), trainloss)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:07:06.421011Z","iopub.execute_input":"2022-07-22T16:07:06.422334Z","iopub.status.idle":"2022-07-22T16:07:06.634477Z","shell.execute_reply.started":"2022-07-22T16:07:06.422289Z","shell.execute_reply":"2022-07-22T16:07:06.633059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset/=255","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:07:06.640383Z","iopub.execute_input":"2022-07-22T16:07:06.641114Z","iopub.status.idle":"2022-07-22T16:07:06.734966Z","shell.execute_reply.started":"2022-07-22T16:07:06.641074Z","shell.execute_reply":"2022-07-22T16:07:06.733645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndic = {'ImageId':[],'Label':[]}\n\nwith torch.no_grad():\n    \n    for i in tqdm(range(len(testset))):\n\n        x = torch.tensor(np.array(testset.loc[i,:]).reshape((28,28)),dtype=torch.float32).unsqueeze(0).unsqueeze(0)\n\n        # x = torch.tensor(np.array(testset.loc[i,:])).unsqueeze(0)\n        # x= x.type(torch.FloatTensor)\n        x = x.to(device)\n        output = model(x)\n        pred = output.argmax(dim=1).cpu().item()\n        dic['ImageId'].append(i+1)\n        dic['Label'].append(pred)\n        # print(pred)\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:07:06.737102Z","iopub.execute_input":"2022-07-22T16:07:06.738049Z","iopub.status.idle":"2022-07-22T16:07:38.295534Z","shell.execute_reply.started":"2022-07-22T16:07:06.737963Z","shell.execute_reply":"2022-07-22T16:07:38.294050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(dic)\ndf.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:07:38.297606Z","iopub.execute_input":"2022-07-22T16:07:38.298221Z","iopub.status.idle":"2022-07-22T16:07:38.373114Z","shell.execute_reply.started":"2022-07-22T16:07:38.298173Z","shell.execute_reply":"2022-07-22T16:07:38.372013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}