{"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import pandas as pd\n\ndata = pd.read_csv('../input/train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\n\nx = torch.ones(2, 2, requires_grad=True)\ny = x + 2\nz = y * y * 3\nout = z.mean()\nout.backward()\nx.grad","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.fc1 = nn.Linear(28 * 28, 800)\n        self.fc2 = nn.Linear(800, 200)\n        self.fc3 = nn.Linear(200, 10)\n\n    \n    def forward(self, x):\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return F.log_softmax(x, dim=1)\n\n\nnet = Net()\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nnet.cuda(device=device)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pycuda.driver as cuda\ncuda.init()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch.optim as optim\n\noptimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9)\n# create a loss function\ncriterion = nn.NLLLoss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_train = data.head(40000)\ndata_test = data.tail(2000)\n\nX_train = data_train.drop('label', axis=1)\nX_test = data_test.drop('label', axis=1)\n\ny_train = data_train['label']\ny_test = data_test['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":false},"cell_type":"code","source":"import torch\nepochs = 1500\n\ndata, target = torch.tensor(X_train.values, dtype=torch.float).cuda()/255, torch.tensor(y_train.values).cuda()\nfor epoch in range(epochs):\n    optimizer.zero_grad()\n    net_out = net(data)\n    loss = criterion(net_out, target)\n    loss.backward()\n    optimizer.step()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"net_out = net(data)\nloss = criterion(net_out, target)\nloss.data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data, target = torch.tensor(X_test.values, dtype=torch.float).cuda()/255, torch.tensor(y_test.values).cuda()\nnet_out = net(data)\ntest_loss = criterion(net_out, target).data\npred = net_out.data.max(1)[1]  # get the index of the max log-probability\ncorrect = pred.eq(target.data).sum()\n\nprint('\\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\\n'.format(\n    test_loss, correct, len(data),\n    100. * correct / len(data)))","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}