{"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":{"_uuid":"084d684e18f9c3dc97bec4acb316b3f5551dd1bc"},"cell_type":"markdown","source":"**Converting pandas Dataframe to Pytorch Tensor type **"},{"metadata":{"trusted":true,"_uuid":"1044643b43ff1cc2fb65c1cf1f927be53e835333"},"cell_type":"code","source":"from torch import nn, optim\nimport torch.nn.functional as F\nimport torch.utils.data as data\nimport torch\n\nclass MinstDataset(data.Dataset):\n    def __init__(self):\n        train = pd.read_csv(\"../input/train.csv\")\n        train_labels = train['label'].values\n        train = train.drop(\"label\",axis=1).values.reshape(42000,1,28,28)\n        self.datalist = train\n        self.labels = train_labels\n    def __getitem__(self, index):\n        return torch.Tensor(self.datalist[index].astype(float)), self.labels[index]\n    def __len__(self):\n        return self.datalist.shape[0]\n\ntrain_Set = MinstDataset()\ntrainloader = torch.utils.data.DataLoader( dataset = train_Set , batch_size= 64 , shuffle = True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e8d23c6f589dbfefab27759bbb530abbed49702d"},"cell_type":"markdown","source":"**Defining Network Architecture for Neural Networks**"},{"metadata":{"trusted":true,"_uuid":"f31ac8a9afa203e32edbcf67ae99117aa57a6018"},"cell_type":"code","source":"class Classifier(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.fc1 = nn.Linear(784, 512)\n        self.fc2 = nn.Linear(512, 256)\n        self.fc3 = nn.Linear(256, 128)\n        self.fc4 = nn.Linear(128, 10)\n        \n        self.dropout = nn.Dropout(p=0.2)\n        \n    def forward(self, x):\n        x = x.view(x.shape[0], -1)\n        \n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.dropout(F.relu(self.fc3(x)))\n        x = F.log_softmax(self.fc4(x), dim=1)\n        \n        return x","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a0fd05ef31e50e05d63ac07a593e96aced4bfd5a"},"cell_type":"markdown","source":"**Defining Loss Function criterion  and Optimizer **"},{"metadata":{"trusted":true,"_uuid":"3d2e8e896caf2ecc007f9222823a1f6f2132c3ec"},"cell_type":"code","source":"model = Classifier()\ncriterion = nn.NLLLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.003)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cbc248648f605eaf0771b8e92febba61a0571436"},"cell_type":"markdown","source":"**Traning the Neural Networks**"},{"metadata":{"trusted":true,"_uuid":"7a8fed145fa44fae65fcde22dee2a32d79760886"},"cell_type":"code","source":"from torch.autograd import Variable\nfor epoch in range(20):\n    for i, (images, labels) in enumerate(trainloader):\n        images = Variable(images)\n        labels = Variable(labels)\n        \n        optimizer.zero_grad()\n        outputs = model(images)\n        \n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        \n        if (i+1) % 100 == 0:\n            print ('Epoch [%d/%d], Iter [%d] Loss: %.4f' %(epoch+1, 10, i+1, loss.data[0]))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5e7e66d840dbb4b395a9d40c3f6954fe708432c3"},"cell_type":"markdown","source":"**Evaluting test set and exporting csv**"},{"metadata":{"trusted":true,"_uuid":"1db62a08a62c1450e4709a4139d972e0ef14296b"},"cell_type":"code","source":"model.eval()\ntest = pd.read_csv(\"../input/test.csv\")\ntest = test.values.reshape(28000,1,28,28).astype(float)\ntest = Variable(torch.Tensor(test))\n\npred = model(test)\n\n_, predlabel = torch.max(pred.data, 1)\npredlabel = predlabel.tolist()\n\npredlabel = pd.DataFrame(predlabel)\npredlabel.index = np.arange(28000) + 1\nid = np.arange(28000) + 1\nid = pd.DataFrame(id)\nid.index = id.index + 1\n\npredlabel = pd.concat([id,predlabel], axis=1)\npredlabel.columns = [\"ImageId\", \"Label\"]\n\npredlabel.to_csv('predict.csv', index= False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}