{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=pd.read_csv('../input/train.csv',dtype=np.float32)# reading the csv file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()#checking out the data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"targets_numpy = data.label.values\nfeatures_numpy = data.loc[:,data.columns != \"label\"].values/255 # normalization","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import torch\nfrom torchvision import datasets,transforms\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Dividing data into training and testing"},{"metadata":{"trusted":true},"cell_type":"code","source":"features_train, features_test, targets_train, targets_test = train_test_split(features_numpy,\n                                                                             targets_numpy,\n                                                                             test_size = 0.2,\n                                                                             random_state = 42)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"featuresTrain = torch.FloatTensor(features_train)\ntargetsTrain = torch.LongTensor(targets_train) # data type is long\n\n# create feature and targets tensor for test set.\nfeaturesTest = torch.FloatTensor(features_test)\ntargetsTest = torch.LongTensor(targets_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(type(featuresTest))\nprint(type(targetsTest))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainset = torch.utils.data.TensorDataset(featuresTrain,targetsTrain)\ntestset = torch.utils.data.TensorDataset(featuresTest,targetsTest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainloader = torch.utils.data.DataLoader(trainset, batch_size = 64, shuffle = False)\ntestloader = torch.utils.data.DataLoader(testset, batch_size = 64, shuffle = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# visualize one of the images in data set\nplt.imshow(features_numpy[10].reshape(28,28))\nplt.axis(\"off\")\nplt.title(str(targets_numpy[10]))\nplt.savefig('graph.png')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from torch import nn, optim \nimport torch.nn.functional as F","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#creating our classifier\nclass classifier(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.fc1=nn.Linear(784, 256)\n        self.fc2=nn.Linear(256,128)\n        self.fc3=nn.Linear(128,10)\n        self.dropout = nn.Dropout(p=0.2)\n    def forward (self,x):\n        x=self.dropout(F.relu(self.fc1(x)))\n        x=self.dropout(F.relu(self.fc2(x)))\n        x=F.log_softmax(self.fc3(x),dim=1)\n        return x\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=classifier()\ncriterion=nn.NLLLoss()\noptimizer=optim.Adam(model.parameters(),lr=0.004)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_on_gpu=torch.cuda.is_available()\nif train_on_gpu:\n    model.cuda()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs=15\ntrain_losses, test_losses = [], []\nfor e in range(epochs):\n    running_loss = 0\n    for images, labels in trainloader:\n        if train_on_gpu:\n            images,labels=images.cuda(),labels.cuda()\n        \n        optimizer.zero_grad()\n        \n        log_ps = model(images)\n        loss = criterion(log_ps, labels)\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        \n    else:\n        test_loss = 0\n        accuracy = 0\n        \n        # Turn off gradients for validation, saves memory and computations\n        with torch.no_grad():\n            model.eval()\n            for images, labels in testloader:\n                if train_on_gpu:\n                    images,labels=images.cuda(),labels.cuda()\n                log_ps = model(images)\n                test_loss += criterion(log_ps, labels)\n                \n                ps = torch.exp(log_ps)\n                top_p, top_class = ps.topk(1, dim=1)\n                equals = top_class == labels.view(*top_class.shape)\n                accuracy += torch.mean(equals.type(torch.FloatTensor))\n        \n        model.train()\n        \n        train_losses.append(running_loss/len(trainloader))\n        test_losses.append(test_loss/len(testloader))\n\n        print(\"Epoch: {}/{}.. \".format(e+1, epochs),\n              \"Training Loss: {:.3f}.. \".format(train_losses[-1]),\n              \"Test Loss: {:.3f}.. \".format(test_losses[-1]),\n              \"Test Accuracy: {:.3f}\".format(accuracy/len(testloader)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%matplotlib inline\n%config InlineBackend.figure_format = 'retina'\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(train_losses, label='Training loss')\nplt.plot(test_losses, label='Validation loss')\nplt.legend(frameon=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Now that our model is trained we will use it on test.csv and store the output we get from it in submission.csv which can be submitted on kaggle"},{"metadata":{"trusted":true},"cell_type":"code","source":"testdata=pd.read_csv('../input/test.csv',dtype=np.float32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testdata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ssubmission=pd.read_csv('../input/sample_submission.csv',)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ssubmission.head() # Checking out how the submission has to be made","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testfeatues = testdata.loc[:].values/255 # normalization","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testfeatures = torch.from_numpy(testfeatues)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(testfeatures.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testlabels=torch.ones_like(testfeatures)# Creating a dummy label list so that we can make a dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(testlabels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testset = torch.utils.data.TensorDataset(testfeatures,testlabels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testloader = torch.utils.data.DataLoader(testset, batch_size = 64, shuffle = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ImageId=[]\nLabel=[]\nmodel.eval()\ncount=-1\nfor images,labels in (testloader):\n    count+=1\n    if train_on_gpu:\n        images=images.cuda()\n    log_ps = model(images)\n    ps = torch.exp(log_ps)\n    top_p, top_class = ps.topk(1, dim=1)\n    i=0\n    for i in range(64):\n        try:#our dataset is not exactly divisible by 64 so it will go out of bound at some point\n            Label.append(top_class[i].item())#Storing label\n            ImageId.append(count*64+i+1)#atoring respective ImageId\n        except Exception:\n            print(i)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### We will be now checking our first 50 labels and ImageId. Labels should contain number from 0 to 9 and ImageId ahould be in increasing order staring from 1 all the way to 50"},{"metadata":{"trusted":true},"cell_type":"code","source":"print((Label[:50]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ImageId[:50])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission={\n    'ImageId':ImageId,\n    'Label':Label\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataa=pd.read_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataa.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"### To download your submission csv check this link out https://www.kaggle.com/getting-started/60617"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}