{"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":"code","source":"import torch\nimport numpy as np\nimport pandas as pd\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torchvision import datasets\nimport torchvision.transforms as transforms\n\nimport matplotlib.pyplot as plt\nimport torch.utils.data as Data","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-27T12:54:24.259954Z","iopub.execute_input":"2023-02-27T12:54:24.260377Z","iopub.status.idle":"2023-02-27T12:54:27.433073Z","shell.execute_reply.started":"2023-02-27T12:54:24.260347Z","shell.execute_reply":"2023-02-27T12:54:27.431342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest_data = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')\n\ntrain_labels = torch.tensor(train_data.label.values.reshape(-1))\ntest_labels = torch.tensor(test_data.label.values.reshape(-1))\ntrain_data = torch.tensor(train_data.drop('label', axis=1).values, dtype = torch.float32)\ntest_data = torch.tensor(test_data.drop('label', axis=1).values, dtype = torch.float32)\n\nprint(train_data.shape, test_data.shape)\nall_dataset = Data.TensorDataset(train_labels, train_data)\nbatch_size = 64 \n# 把 dataset 放入 DataLoader\nloader = Data.DataLoader(\n    dataset=all_dataset,      # torch TensorDataset format\n    batch_size=batch_size,      # mini batch size\n    shuffle=True,               # 要不要打乱数据 (打乱比较好)\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:54:27.435972Z","iopub.execute_input":"2023-02-27T12:54:27.436579Z","iopub.status.idle":"2023-02-27T12:54:32.740557Z","shell.execute_reply.started":"2023-02-27T12:54:27.436537Z","shell.execute_reply":"2023-02-27T12:54:32.739467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MLP(nn.Module):\n    def __init__(self):\n        super(MLP, self).__init__()\n        # implement your codes here\n        self.fc1 = nn.Sequential(nn.Linear(784, 400), nn.ReLU())\n        self.fc2 = nn.Sequential(nn.Linear(400, 200), nn.ReLU())\n        self.fc3 = nn.Sequential(nn.Linear(200, 100), nn.ReLU())\n        self.fc4 = nn.Linear(100, 10)\n    def forward(self, x):\n        # flatten image input\n        x = x.view(-1, 28 * 28)\n\n        # implement your codes here\n        x = self.fc1(x)\n        x = self.fc2(x)\n        x = self.fc3(x)\n        x = self.fc4(x)\n        \n        return x\nnet = MLP()\nprint(net)\n# specify loss function\nloss = nn.CrossEntropyLoss()\n# specify your optimizer\noptimizer1 = optim.SGD(net.parameters(),lr=0.005)\noptimizer1.zero_grad()\noptimizer1","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:54:32.742084Z","iopub.execute_input":"2023-02-27T12:54:32.742480Z","iopub.status.idle":"2023-02-27T12:54:32.788405Z","shell.execute_reply.started":"2023-02-27T12:54:32.742443Z","shell.execute_reply":"2023-02-27T12:54:32.786300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 10  # suggest training between 100-150 epochs\n\n# prep model for training\nloss_list = []\nfor epoch in range(n_epochs):\n    # monitor training loss\n    train_loss = 0.0\n    total_correct = 0\n    \n    for target, data in loader:\n        net.train()\n        y_predict = net(data)\n        l1 = loss(y_predict, target)\n        optimizer1.zero_grad()\n        l1.backward()\n        optimizer1.step()  # 执行优化\n        # implement your code here\n        train_loss += l1.item() \n        # the total loss of this batch\n        total_correct += (y_predict.argmax(dim=1) == target).sum().item()\n        # the accumulated number of correctly classified samples of this batch\n        \n    # print training statistics \n    # calculate average loss and accuracy over an epoch\n    loss_list.append(train_loss)\n    train_loss = train_loss / len(train_labels)\n    train_acc = 100. * total_correct / len(train_labels)\n    \n    print('Epoch: {} \\tTraining Loss: {:.6f} \\tTraining Acc: {:.2f}%%'.format(\n        epoch+1, \n        train_loss,\n        train_acc\n        ))","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:54:32.790030Z","iopub.execute_input":"2023-02-27T12:54:32.790666Z","iopub.status.idle":"2023-02-27T12:55:17.413388Z","shell.execute_reply.started":"2023-02-27T12:54:32.790625Z","shell.execute_reply":"2023-02-27T12:55:17.412446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test2_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ndata = torch.tensor(test2_data.iloc[:,1:].values, dtype = torch.float32)\npre = net(data)\npre_label = pre.argmax(dim=1)\nsub = pd.DataFrame(pre_label,columns=['label'] )\nsub = pd.concat([test2_data['id'], sub], axis = 1)\nsub","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:55:17.415831Z","iopub.execute_input":"2023-02-27T12:55:17.416687Z","iopub.status.idle":"2023-02-27T12:55:17.929007Z","shell.execute_reply.started":"2023-02-27T12:55:17.416625Z","shell.execute_reply":"2023-02-27T12:55:17.927245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('/kaggle/working/submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T12:55:17.930824Z","iopub.execute_input":"2023-02-27T12:55:17.931242Z","iopub.status.idle":"2023-02-27T12:55:17.946803Z","shell.execute_reply.started":"2023-02-27T12:55:17.931200Z","shell.execute_reply":"2023-02-27T12:55:17.945760Z"},"trusted":true},"execution_count":null,"outputs":[]}]}