{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import torch\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\", dtype=np.float32)\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\", dtype=np.float32)\nsample_sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train_df.label.values\nX = train_df.loc[:, train_df.columns != 'label'].values/255\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.shape, X_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Model(torch.nn.Module):\n    def __init__(self, n_neurons):\n        super(Model, self).__init__()\n        self.fc1 = torch.nn.Linear(28 * 28, n_neurons)\n        self.ac1 = torch.nn.Sigmoid()\n        self.fc2 = torch.nn.Linear(n_neurons, 10) \n    \n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.ac1(x)\n        x = self.fc2(x)\n        return x\n    \nmodel = Model(100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"torch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=1.0e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"XTrain = torch.from_numpy(X_train)\nyTrain = torch.from_numpy(y_train).type(torch.LongTensor)\n\nXTest = torch.from_numpy(X_test)\nyTest = torch.from_numpy(y_test).type(torch.LongTensor)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs = 100\n\n# X_test = X_test.to(device) # for GPU\n# y_test = y_test.to(device) # for GPU\n\nfor epoch in range(10):\n    order = np.random.permutation(len(XTrain))\n    \n    for i in range(0, len(XTrain), bs):\n        optimizer.zero_grad()\n        \n        bi = order[i:i+bs]\n        \n        X_batch = XTrain[bi] #.to(device) # for GPU\n        y_batch = yTrain[bi] #.to(device) # for GPU\n        \n        preds = model.forward(X_batch) \n        \n        loss_value = loss(preds, y_batch)\n        loss_value.backward()\n        \n        optimizer.step()\n\n    test_preds = model.forward(XTest)        \n    accuracy = (test_preds.argmax(dim=1) == yTest).float().mean()    \n    \n    print(accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = torch.from_numpy(test_df.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub['Label'] = model(pred).argmax(dim=1)\nsample_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}