{"cells":[{"metadata":{},"cell_type":"markdown","source":"!pip uninstall kaggle<br>\n!pip install --upgrade pip<br>\n!pip install kaggle==1.5.6<br>\n!mkdir -p ~/.kaggle<br>\n!cp kaggle.json ~/.kaggle<br>\n!ls -lha kaggle.json<br>\n!chmood 600 ~/.kaggle/kaggle.json<br>","execution_count":null},{"metadata":{"id":"ecdqKqdTrbdF","outputId":"9ff3bd7f-182c-460e-b166-0f53d86d996a","trusted":false},"cell_type":"code","source":"#!kaggle competitions download -c 2020-ai-exam-fashionmnist-2","execution_count":null,"outputs":[]},{"metadata":{"id":"VimwY582rg-D","outputId":"9d183557-cab2-4a40-9521-f830306510a5","trusted":false},"cell_type":"code","source":"#!unzip 2020-ai-exam-fashionmnist-2.zip","execution_count":null,"outputs":[]},{"metadata":{"id":"ddu_yXt2rjq0","trusted":false},"cell_type":"code","source":"import torch\nimport torchvision.datasets as data\nimport torchvision.transforms as transforms\nimport random\n\nimport pandas as pd\nimport numpy as np\n","execution_count":null,"outputs":[]},{"metadata":{"id":"JVV2iSb6rrnB","trusted":false},"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\nrandom.seed(777)\ntorch.manual_seed(777)\nif device == 'cuda':\n  torch.cuda.manual_seed_all(777)","execution_count":null,"outputs":[]},{"metadata":{"id":"xd79tdVNr8_E","trusted":false},"cell_type":"code","source":"learning_rate = 0.1\ntraining_epochs = 15\nbatch_size = 100","execution_count":null,"outputs":[]},{"metadata":{"id":"A2QPzVXFsHWx","trusted":false},"cell_type":"code","source":"train_data = pd.read_csv('../input/2020-ai-exam-fashionmnist-2/mnist_train_label.csv', header = None)\ntest_data = pd.read_csv('../input/2020-ai-exam-fashionmnist-2/mnist_test.csv', header = None, usecols=range(1, 785))","execution_count":null,"outputs":[]},{"metadata":{"id":"PHZpyAJCs_fi","outputId":"dd5c7aeb-d9cb-4308-aa30-58089b2ee152","trusted":false},"cell_type":"code","source":"test_data","execution_count":null,"outputs":[]},{"metadata":{"id":"xSeCtLEks0ig","outputId":"02899929-2920-4fdb-c550-af91b3ae942f","trusted":false},"cell_type":"code","source":"train_data","execution_count":null,"outputs":[]},{"metadata":{"id":"9vkKrrlstqE0","outputId":"c17cd84a-887b-41ca-aa58-c58e41fe86be","trusted":false},"cell_type":"code","source":"x_train_data = train_data.loc[:, 1:784]\ny_train_data = train_data.loc[:, 0]\n\n#데이터 정규화\nx_train_data = x_train_data/255\nx_train_data","execution_count":null,"outputs":[]},{"metadata":{"id":"L-ZS-TVP0YoA","trusted":false},"cell_type":"code","source":"  \nx_train_data = np.array(x_train_data)\ny_train_data = np.array(y_train_data)\nx_train_data = torch.FloatTensor(x_train_data)\ny_train_data = torch.LongTensor(y_train_data)","execution_count":null,"outputs":[]},{"metadata":{"id":"caoEpWKauTrY","trusted":false},"cell_type":"code","source":"train_dataset =  torch.utils.data.TensorDataset(x_train_data, y_train_data)","execution_count":null,"outputs":[]},{"metadata":{"id":"7QrSYKAruKch","trusted":false},"cell_type":"code","source":"data_loader = torch.utils.data.DataLoader(dataset=train_dataset, \n                                          batch_size=batch_size, \n                                          shuffle = True, \n                                          drop_last = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"Nie5bETTukEe","trusted":false},"cell_type":"code","source":"linear = torch.nn.Linear(784,10, bias = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"7KudOpwJuqsF","outputId":"d9fbd186-bef0-49c4-81e8-287b96dc415b","trusted":false},"cell_type":"code","source":"torch.nn.init.xavier_normal_(linear.weight)","execution_count":null,"outputs":[]},{"metadata":{"id":"lyozySTiuufb","trusted":false},"cell_type":"code","source":"model = torch.nn.Sequential(linear).to(device)","execution_count":null,"outputs":[]},{"metadata":{"id":"CqCjnzIyuy76","trusted":false},"cell_type":"code","source":"loss = torch.nn.CrossEntropyLoss().to(device)\noptimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)","execution_count":null,"outputs":[]},{"metadata":{"id":"agf5T-n4yEBD","outputId":"6856a6fd-b7e1-4dbc-bcd3-7a8b5dbaf035","trusted":false},"cell_type":"code","source":"print(len(data_loader))","execution_count":null,"outputs":[]},{"metadata":{"id":"88GWTc34u8Jj","outputId":"fadf6ce8-edba-474c-c3bc-301d33c37043","trusted":false},"cell_type":"code","source":"total_batch = len(data_loader)\nfor epoch in range(training_epochs):\n  avg_cost = 0\n\n  for X, Y in data_loader:\n    X = X.to(device)\n    Y = Y.to(device)\n\n    optimizer.zero_grad()\n    hypothesis = model(X)\n    cost = loss(hypothesis, Y)\n    cost.backward()\n    optimizer.step()\n\n    avg_cost += cost/total_batch\n  print(epoch+1)\n  print(avg_cost)\nprint(\"f\")","execution_count":null,"outputs":[]},{"metadata":{"id":"fHyg5MhBvcWQ","outputId":"e50d098b-a0cd-4595-ce62-20583ba31216","trusted":false},"cell_type":"code","source":"with torch.no_grad():\n  test_data.loc[:, 783] = 0\n  x_test = test_data.loc[:,:]\n  x_test = x_test/255\n  x_test = np.array(x_test)\n  x_test = torch.from_numpy(x_test).float().to(device)\n  prediction = model(x_test)\n  correct = torch.argmax(prediction, 1)\ncorrect","execution_count":null,"outputs":[]},{"metadata":{"id":"tanB4Ygavnek","outputId":"e8ed2f5c-9932-4f31-d715-6a12e4e6f0c8","trusted":false},"cell_type":"code","source":"correct = correct.cpu().numpy().reshape(-1, 1)\ncorrect","execution_count":null,"outputs":[]},{"metadata":{"id":"Tpdgm0ImxCAI","outputId":"176811a5-3f8c-4ba0-d0fe-493bdc6698f1","trusted":false},"cell_type":"code","source":"submit = pd.read_csv('../input/2020-ai-exam-fashionmnist-2/submission.csv')\nsubmit","execution_count":null,"outputs":[]},{"metadata":{"id":"qtJXW-wsxbY0","outputId":"c09bfe38-7ebf-4903-d8b6-190e70ead86b","trusted":false},"cell_type":"code","source":"for i in range(len(correct)):\n  submit['Category'][i] = correct[i].item()\nsubmit","execution_count":null,"outputs":[]},{"metadata":{"id":"NU6SSHQDzCXy","trusted":false},"cell_type":"code","source":"submit.to_csv('submit.csv', index = False, header = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"c2ts9YvqxkL7","outputId":"324d2044-5c74-454d-d64f-e7448c5305ec","trusted":false},"cell_type":"code","source":"!kaggle competitions submit -c 2020-ai-exam-fashionmnist-2 -f submit.csv -m \"result\"","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":4}