{"cells":[{"metadata":{"id":"lyA4oEJRjmTN","outputId":"1c424607-7f5d-43ea-d404-0c238d21e719","trusted":false},"cell_type":"code","source":"!pip uninstall --y kaggle\n!pip install --upgrade pip\n!pip install kaggle==1.5.6","execution_count":null,"outputs":[]},{"metadata":{"id":"5nWl_LTYlHp_","outputId":"902b8922-907d-4b4c-922e-b9f1eec81025","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":"5xAJCzhYlMHc","outputId":"e856d6b8-0d4b-4ef4-904c-204aa8c04095","trusted":false},"cell_type":"code","source":"!kaggle competitions download --force -c 2020-ai-exam-fashionmnist-1\n\n!unzip 2020-ai-exam-fashionmnist-1.zip","execution_count":null,"outputs":[]},{"metadata":{"id":"Z0EqzG27lacv","outputId":"a1a85cac-e528-4c4c-ddfc-ca1827cfa59d","trusted":false},"cell_type":"code","source":"# Library Import\n\nimport pandas as pd\nimport numpy as np\nimport torch\nimport torchvision.datasets as data\nimport torchvision.transforms as transforms\nimport random\n\ntorch.manual_seed(777)","execution_count":null,"outputs":[]},{"metadata":{"id":"qRczDiAMlah2","trusted":false},"cell_type":"code","source":"# Set Learning Parameter\n\nlearning_rate = 0.01\ntraining_epochs = 15\nbatch_size = 1","execution_count":null,"outputs":[]},{"metadata":{"id":"jKirSrG4lafc","trusted":false},"cell_type":"code","source":"# Data Load\n\ntrain_data=pd.read_csv('/content/mnist_train_label.csv',encoding='euc-kr',header=None, skiprows=1, usecols=range(0,10))\ntest_data=pd.read_csv('/content/mnist_test.csv',encoding='euc-kr',header=None, skiprows=1, usecols=range(1,10))","execution_count":null,"outputs":[]},{"metadata":{"id":"YIxQMT7TlamD","outputId":"58a95c32-35d6-4bc3-e840-f52b7b8f227a","trusted":false},"cell_type":"code","source":"train_data","execution_count":null,"outputs":[]},{"metadata":{"id":"bqDqLT-1laoj","outputId":"31ded881-fd14-4c1a-b172-1d96b1b08d42","trusted":false},"cell_type":"code","source":"test_data","execution_count":null,"outputs":[]},{"metadata":{"id":"XPyDQQhNlaj9","trusted":false},"cell_type":"code","source":" # Tensor형 데이터로 변형\n\nx_train_data=train_data.loc[:,0:8]\ny_train_data=train_data[9]\n\nx_train_data=np.array(x_train_data)\ny_train_data=np.array(y_train_data)\n\nx_train_data=torch.FloatTensor(x_train_data)\ny_train_data=torch.FloatTensor(y_train_data)","execution_count":null,"outputs":[]},{"metadata":{"id":"n1s1LWSpqR6a","outputId":"2b318a4a-b789-4d34-d8d8-967eb22dc204","trusted":false},"cell_type":"code","source":"x_train_data","execution_count":null,"outputs":[]},{"metadata":{"id":"Wx4pZHoEm3I1","outputId":"4b70629d-f0b0-4e77-df28-87cc3ccc9a96","trusted":false},"cell_type":"code","source":"# Neural Network 모델 정의\nlinear = torch.nn.Linear(9,1,bias=True)\ntorch.nn.init.normal_(linear.weight)\nmodel = torch.nn.Sequential(linear).to(device) # 'cuda'\nmodel","execution_count":null,"outputs":[]},{"metadata":{"id":"QbDt0EgWm3Lj","trusted":false},"cell_type":"code","source":"# 모델 학습\n\nloss = torch.nn.CrossEntropyLoss().to(device)\noptimizer = torch.optim.SGD(model.parameters(),learning_rate)","execution_count":null,"outputs":[]},{"metadata":{"id":"iTjgkTttm3N-","outputId":"46a59c8c-d397-42e2-c366-b3549b29555c","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\n        X = X.to(device)\n        Y = Y.to(device)\n\n        # 그래디언트 초기화\n        optimizer.zero_grad()\n\n        hypothesis = model(X)\n        cost = loss(hypothesis, Y)\n        cost.backward()\n        optimizer.step()\n\n        # 평균 Error 계산\n        avg_cost += cost / total_batch\n\n    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))\n\nprint('Learning finished')","execution_count":null,"outputs":[]},{"metadata":{"id":"k-g94GsWm3Qg","trusted":false},"cell_type":"code","source":"# test_data를 모델에 넣기\nwith torch.no_grad():\n\n  x_test_data=test_data.loc[:,1:10]\n  x_test_data=np.array(x_test_data)\n  x_test_data=torch.from_numpy(x_test_data).float()\n\n  prediction = model(x_test_data)","execution_count":null,"outputs":[]},{"metadata":{"id":"i3RIbJZOm3S4","trusted":false},"cell_type":"code","source":"x_test_data","execution_count":null,"outputs":[]},{"metadata":{"id":"76fZcpZHm3VZ","trusted":false},"cell_type":"code","source":"correct_prediction = prediction.cpu().numpy().reshape(-1,1)","execution_count":null,"outputs":[]},{"metadata":{"id":"FuAiEsg_m3YC","trusted":false},"cell_type":"code","source":"submit=pd.read_csv('/content/submission.csv')\nsubmit","execution_count":null,"outputs":[]},{"metadata":{"id":"te85_KtHwW0j","trusted":false},"cell_type":"code","source":"for i in range(len(correct_prediction)):\n  submit['Category'][i]=correct_prediction[i].item()","execution_count":null,"outputs":[]},{"metadata":{"id":"4tsgRA6twW5E","trusted":false},"cell_type":"code","source":"submit.to_csv('submit.csv',index=False,header=True)\n\n!kaggle competitions submit -c 2020-ai-exam-fashionmnist-1 -f submit.csv -m \"submit\"","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}