{"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":"markdown","source":"# USING CONVOLUTIONAL NUERAL NETWORKS\n## Epochs: 35\n## Accuracy: 95.79%\n## Time Taken: 2min 9s (Using GPU)","metadata":{}},{"cell_type":"code","source":"import torch \nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:11.619604Z","iopub.execute_input":"2021-07-29T07:15:11.620009Z","iopub.status.idle":"2021-07-29T07:15:12.911672Z","shell.execute_reply.started":"2021-07-29T07:15:11.619924Z","shell.execute_reply":"2021-07-29T07:15:12.910835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain_data=pd.read_csv('../input/digit-recognizer/train.csv')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:12.913265Z","iopub.execute_input":"2021-07-29T07:15:12.913649Z","iopub.status.idle":"2021-07-29T07:15:16.290702Z","shell.execute_reply.started":"2021-07-29T07:15:12.913612Z","shell.execute_reply":"2021-07-29T07:15:16.288922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=train_data.values\nX_train, y_train = train_data[:, 1:] , train_data[:, 0]\nX_train = X_train.reshape(X_train.shape[0], 1, 28, 28)\nX_train, y_train = torch.from_numpy(X_train).float(), torch.from_numpy(y_train).long()\ntrain_dataset = torch.utils.data.TensorDataset(X_train, y_train)\ntrain_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.293818Z","iopub.execute_input":"2021-07-29T07:15:16.294410Z","iopub.status.idle":"2021-07-29T07:15:16.471200Z","shell.execute_reply.started":"2021-07-29T07:15:16.294365Z","shell.execute_reply":"2021-07-29T07:15:16.470073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Image_classification(nn.Module):\n  def __init__(self):\n    super().__init__()\n    self.network=nn.Sequential(\n        nn.Conv2d(1,28,kernel_size=3,stride=1,padding=1),    #32x28x28\n        nn.ReLU(),\n        nn.Conv2d(28,64,kernel_size=3,stride=1,padding=1),   #64x28x28\n        nn.ReLU(),\n        nn.MaxPool2d(2,2) ,                                  #64x14x14\n        nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),  \n        nn.Flatten(),\n        nn.Linear(128*14*14,1024),\n        nn.ReLU(),\n        nn.Linear(1024,10)  )\n  def forward(self,x):\n    return self.network(x)  ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.472966Z","iopub.execute_input":"2021-07-29T07:15:16.473443Z","iopub.status.idle":"2021-07-29T07:15:16.483983Z","shell.execute_reply.started":"2021-07-29T07:15:16.473405Z","shell.execute_reply":"2021-07-29T07:15:16.482904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn=Image_classification()\nmodel_cnn","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.485518Z","iopub.execute_input":"2021-07-29T07:15:16.486113Z","iopub.status.idle":"2021-07-29T07:15:16.865295Z","shell.execute_reply.started":"2021-07-29T07:15:16.486072Z","shell.execute_reply":"2021-07-29T07:15:16.864412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer=torch.optim.SGD(model_cnn.parameters(),lr=1e-5)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.866460Z","iopub.execute_input":"2021-07-29T07:15:16.866924Z","iopub.status.idle":"2021-07-29T07:15:16.871517Z","shell.execute_reply.started":"2021-07-29T07:15:16.866884Z","shell.execute_reply":"2021-07-29T07:15:16.870628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn=nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.872817Z","iopub.execute_input":"2021-07-29T07:15:16.873314Z","iopub.status.idle":"2021-07-29T07:15:16.881862Z","shell.execute_reply.started":"2021-07-29T07:15:16.873277Z","shell.execute_reply":"2021-07-29T07:15:16.880853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.cuda.is_available():\n    model_cnn=model_cnn.cuda()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:16.883340Z","iopub.execute_input":"2021-07-29T07:15:16.883818Z","iopub.status.idle":"2021-07-29T07:15:21.175700Z","shell.execute_reply.started":"2021-07-29T07:15:16.883767Z","shell.execute_reply":"2021-07-29T07:15:21.174738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nepochs=35\nfor epoch in range(epochs):\n    score=0\n    for batch in train_loader:\n        x,y=batch\n        if torch.cuda.is_available():\n            x=x.cuda()\n            y=y.cuda()\n        model_cnn.train()\n        #STEP-1: Forward\n        out=model_cnn(x)\n        \n        #STEP-2:Loss\n        loss=loss_fn(out,y)\n        \n        #Step-3:Cleaning the prev calculated gradients\n        model_cnn.zero_grad()\n        \n        #Step-4:Accumalate partial derivatives of Loss wrt to params\n        loss.backward()\n        \n        #Step-5: Takes 5 steps to update\n        optimizer.step()\n        _,pred=torch.max(out,axis=1)\n        score+=(pred==y).sum()\n    acc=score/len(train_data)\n    print(\"{}/{} Epochs | Train Loss={:.4f} |Accuracy={:.4f}\".format(epoch+1,epochs,loss,acc)  )","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:15:21.179805Z","iopub.execute_input":"2021-07-29T07:15:21.180065Z","iopub.status.idle":"2021-07-29T07:17:31.048742Z","shell.execute_reply.started":"2021-07-29T07:15:21.180040Z","shell.execute_reply":"2021-07-29T07:17:31.047868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntest_data=pd.read_csv('../input/digit-recognizer/test.csv')\ntest_data","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:24:40.543776Z","iopub.execute_input":"2021-07-29T07:24:40.544084Z","iopub.status.idle":"2021-07-29T07:24:42.628607Z","shell.execute_reply.started":"2021-07-29T07:24:40.544056Z","shell.execute_reply":"2021-07-29T07:24:42.627714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    test_data=test_data.values\nexcept:\n    print(type(test_data),test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:24:47.413954Z","iopub.execute_input":"2021-07-29T07:24:47.414911Z","iopub.status.idle":"2021-07-29T07:24:47.419568Z","shell.execute_reply.started":"2021-07-29T07:24:47.414809Z","shell.execute_reply":"2021-07-29T07:24:47.418481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=[]\nfor i in range(len(test_data)):\n    img=test_data[i,:]\n    img=img.reshape(1,1,28,28)\n    img=torch.from_numpy(img).float()\n    model_cnn.eval()\n    if torch.cuda.is_available():\n        img=img.cuda()\n    output=model_cnn(img)\n    _,predicted = torch.max(output , 1)\n    pred.append(predicted.item())","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:24:47.421618Z","iopub.execute_input":"2021-07-29T07:24:47.422076Z","iopub.status.idle":"2021-07-29T07:25:26.311928Z","shell.execute_reply.started":"2021-07-29T07:24:47.422042Z","shell.execute_reply":"2021-07-29T07:25:26.311022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:25:26.313842Z","iopub.execute_input":"2021-07-29T07:25:26.314194Z","iopub.status.idle":"2021-07-29T07:25:26.319972Z","shell.execute_reply.started":"2021-07-29T07:25:26.314159Z","shell.execute_reply":"2021-07-29T07:25:26.319033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/digit-recognizer/sample_submission.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:30:37.015873Z","iopub.execute_input":"2021-07-29T07:30:37.016195Z","iopub.status.idle":"2021-07-29T07:30:37.041368Z","shell.execute_reply.started":"2021-07-29T07:30:37.016167Z","shell.execute_reply":"2021-07-29T07:30:37.040410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Label']=pred\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:30:51.091162Z","iopub.execute_input":"2021-07-29T07:30:51.091503Z","iopub.status.idle":"2021-07-29T07:30:51.114808Z","shell.execute_reply.started":"2021-07-29T07:30:51.091451Z","shell.execute_reply":"2021-07-29T07:30:51.113911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('Submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:33:14.483972Z","iopub.execute_input":"2021-07-29T07:33:14.484413Z","iopub.status.idle":"2021-07-29T07:33:14.582050Z","shell.execute_reply.started":"2021-07-29T07:33:14.484374Z","shell.execute_reply":"2021-07-29T07:33:14.581334Z"},"trusted":true},"execution_count":null,"outputs":[]}]}