{"cells":[{"metadata":{"id":"JN-NLW-4kz5u","outputId":"6733faf8-087b-4813-a163-e1b4caea92cd","trusted":false},"cell_type":"code","source":"!pip uninstall kaggle\n!pip install --upgrade pip\n!pip install kaggle==1.5.6","execution_count":null,"outputs":[]},{"metadata":{"id":"Yq940P8-lKTm","outputId":"20d83614-9164-4028-8fb5-4d3acd395a1f","trusted":false},"cell_type":"code","source":"!mkdir -p ~/.kaggle\n!cp kaggle.json ~/.kaggle\n!ls -lha kaggle.json\n!chmod 600 ~/.kaggle/kaggle.json","execution_count":null,"outputs":[]},{"metadata":{"id":"ohn1PyjQlLUk","outputId":"1104ce29-7cb1-44ac-9d09-f3fca34851b9","trusted":false},"cell_type":"code","source":"!kaggle competitions download -c 2020-ai-exam-fashionmnist-2","execution_count":null,"outputs":[]},{"metadata":{"id":"cw7shKqKlW60","outputId":"8f6ee8ac-c4d8-4400-a323-698d324b9598","trusted":false},"cell_type":"code","source":"!unzip 2020-ai-exam-fashionmnist-2","execution_count":null,"outputs":[]},{"metadata":{"id":"tb7Jxlyilvuo","trusted":false},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport torch","execution_count":null,"outputs":[]},{"metadata":{"id":"hH4BA5O1lzlN","trusted":false},"cell_type":"code","source":"train_csv = pd.read_csv('mnist_train_label.csv')\ntest_csv = pd.read_csv('mnist_test.csv') ","execution_count":null,"outputs":[]},{"metadata":{"id":"7PWbeENdl2AM","outputId":"22d454eb-b3ba-4f26-8b67-5e955b12e1ff","trusted":false},"cell_type":"code","source":"train_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"dOf-MhJVuWWP","outputId":"fc79658c-a22f-43bb-afed-2d648dddb187","trusted":false},"cell_type":"code","source":"len(test_csv)","execution_count":null,"outputs":[]},{"metadata":{"id":"QGyJpo8yqVHs","outputId":"abf69317-5e80-4df9-bc3f-1fd4c74711cd","trusted":false},"cell_type":"code","source":"train_csv.describe()","execution_count":null,"outputs":[]},{"metadata":{"id":"3AzrpIjGmF-7","trusted":false},"cell_type":"code","source":"train_L = train_csv.iloc[:,0]","execution_count":null,"outputs":[]},{"metadata":{"id":"9osDU7XUmMPL","trusted":false},"cell_type":"code","source":"train_D = train_csv.drop('9',axis =1)","execution_count":null,"outputs":[]},{"metadata":{"id":"Z9l9-2__uK7e","trusted":false},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\ntrain_D = scaler.fit_transform(train_D)# standardscaler 사용해서 정규화. 나머지는 1번과 동일. ","execution_count":null,"outputs":[]},{"metadata":{"id":"lvNEv4XwnRyj","outputId":"2e1d9927-b106-41ef-b75d-8816b096b2ef","trusted":false},"cell_type":"code","source":"len(train_D)","execution_count":null,"outputs":[]},{"metadata":{"id":"kOt_QTzsqCUi","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"gYUdLK3SmYnY","trusted":false},"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\ntorch.manual_seed(777)\nif device == 'cuda':\n    torch.cuda.manual_seed_all(777)","execution_count":null,"outputs":[]},{"metadata":{"id":"PMLatIdFpuqH","trusted":false},"cell_type":"code","source":"device= 'cpu'","execution_count":null,"outputs":[]},{"metadata":{"id":"MKZ7exYItbNy","trusted":false},"cell_type":"code","source":"l1 = torch.nn.Linear(train_D.shape[1], 10, bias = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"Xe1n6a2YmonV","outputId":"7fdb91a2-dcba-49aa-a71f-ccc3a549557f","trusted":false},"cell_type":"code","source":"torch.nn.init.xavier_uniform_(l1.weight)\n#1번과 동일. 가중치 최적화 ","execution_count":null,"outputs":[]},{"metadata":{"id":"gNSnNKhDnGuy","trusted":false},"cell_type":"code","source":"model = torch.nn.Sequential(l1).to(device)","execution_count":null,"outputs":[]},{"metadata":{"id":"uWrLvijknbnD","trusted":false},"cell_type":"code","source":"lr = 0.1\nepoch = 15\nbatch_size= 1000","execution_count":null,"outputs":[]},{"metadata":{"id":"csWo83lunFm2","trusted":false},"cell_type":"code","source":"loss = torch.nn.CrossEntropyLoss().to(device) # to gpu\noptimizer = torch.optim.SGD(model.parameters(), lr=lr) # 0.49","execution_count":null,"outputs":[]},{"metadata":{"id":"fNXwxNRpna6j","trusted":false},"cell_type":"code","source":"train_D = torch.FloatTensor(np.array(train_D))\ntrain_L = torch.LongTensor(np.array(train_L))","execution_count":null,"outputs":[]},{"metadata":{"id":"u7Wxuy6Qo5zF","trusted":false},"cell_type":"code","source":"#!nvidia-smi","execution_count":null,"outputs":[]},{"metadata":{"id":"6EMmD_v5pz6K","outputId":"77690576-f87c-47cb-ed5d-36f07f106ea5","trusted":false},"cell_type":"code","source":"train_L","execution_count":null,"outputs":[]},{"metadata":{"id":"t_8frahXpDiT","outputId":"9e6590b6-1366-4a11-abf3-cd8772f98f78","trusted":false},"cell_type":"code","source":"for stop in range(epoch):\n    \n    # 그래디언트 초기화\n    optimizer.zero_grad()\n    # Forward 계산\n    hypothesis = model(train_D)\n    # Error 계산\n    cost = loss(hypothesis, train_L)\n    # Backward 계산 \n    cost.backward()\n    # 가중치 갱신\n    optimizer.step()\n    print(stop, cost.item())\n","execution_count":null,"outputs":[]},{"metadata":{"id":"p5iyBhKlrOVd","trusted":false},"cell_type":"code","source":"test_D= torch.FloatTensor(np.array(test_csv))","execution_count":null,"outputs":[]},{"metadata":{"id":"GaF2ItmfrSXg","trusted":false},"cell_type":"code","source":"with torch.no_grad():\n  model.eval()\n  pred= model(test_D.to(device))\n","execution_count":null,"outputs":[]},{"metadata":{"id":"2bC6-8RJrYw5","trusted":false},"cell_type":"code","source":"pred_list= torch.argmax(pred, 1).cpu()","execution_count":null,"outputs":[]},{"metadata":{"id":"aJF6pOYArddr","outputId":"67c4a795-4504-4c74-f23f-d8f913c7f122","trusted":false},"cell_type":"code","source":"pred_list[:20]","execution_count":null,"outputs":[]},{"metadata":{"id":"zRA99t4irhtC","trusted":false},"cell_type":"code","source":"sample= pd.read_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"3fCNhx3VrsUr","outputId":"32cea04e-e74d-4f09-c0a0-0119fe56da26","trusted":false},"cell_type":"code","source":"len(test_D)","execution_count":null,"outputs":[]},{"metadata":{"id":"uzvroGlfrkAD","outputId":"7fb8b724-4f8a-488e-d400-b62a07c77721","trusted":false},"cell_type":"code","source":"sample","execution_count":null,"outputs":[]},{"metadata":{"id":"oiSPic_orlHF","trusted":false},"cell_type":"code","source":"result = {}\nresult['Id'] = list(i for i in range(1,10000))\nresult['Category'] = pred_list","execution_count":null,"outputs":[]},{"metadata":{"id":"esjYtjs-sGzt","outputId":"1e925ba7-c5f3-498f-c644-5e06030580f6","trusted":false},"cell_type":"code","source":"pred_list.detach().numpy()","execution_count":null,"outputs":[]},{"metadata":{"id":"jvW9SeaxsSYx","trusted":false},"cell_type":"code","source":"sample['Category'][1:] = pred_list.detach().numpy()","execution_count":null,"outputs":[]},{"metadata":{"id":"l6DXvv__sZVq","outputId":"2b243b89-698e-46b5-d124-c78c4c873db8","trusted":false},"cell_type":"code","source":"pd.DataFrame(sample)","execution_count":null,"outputs":[]},{"metadata":{"id":"zlMNwNI6sbpM","trusted":false},"cell_type":"code","source":"sample.to_csv('q2.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":4}