{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-15T02:25:00.312011Z","iopub.execute_input":"2021-10-15T02:25:00.312283Z","iopub.status.idle":"2021-10-15T02:25:00.318925Z","shell.execute_reply.started":"2021-10-15T02:25:00.312258Z","shell.execute_reply":"2021-10-15T02:25:00.317888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = pd.read_csv(\"/kaggle/input/2021-ai-w7-p1/mnist_train.csv\",index_col=0)\nx_test = pd.read_csv(\"/kaggle/input/2021-ai-w7-p1/mnist_test.csv\",index_col=0)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:00.324110Z","iopub.execute_input":"2021-10-15T02:25:00.324397Z","iopub.status.idle":"2021-10-15T02:25:10.134671Z","shell.execute_reply.started":"2021-10-15T02:25:00.324372Z","shell.execute_reply":"2021-10-15T02:25:10.133835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\ny_train = x_train[:,-1]\nx_train = x_train[:,:-1]\nx_test = np.array(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:10.138218Z","iopub.execute_input":"2021-10-15T02:25:10.138465Z","iopub.status.idle":"2021-10-15T02:25:10.306085Z","shell.execute_reply.started":"2021-10-15T02:25:10.138440Z","shell.execute_reply":"2021-10-15T02:25:10.305129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, axes = plt.subplots(1, 4)\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(1 - x_train[i].reshape((28, 28)), cmap=\"gray\")\n    ax.set(title=f\"{y_train[i]}\")\n    ax.set_axis_off()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:23.900875Z","iopub.execute_input":"2021-10-15T02:25:23.901192Z","iopub.status.idle":"2021-10-15T02:25:24.107386Z","shell.execute_reply.started":"2021-10-15T02:25:23.901164Z","shell.execute_reply":"2021-10-15T02:25:24.106511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport random\n\ndevice = '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)\n\nx_train = torch.tensor(x_train, dtype=torch.float)/255\nx_test = torch.tensor(x_test, dtype=torch.float)/255\ny_train = torch.tensor([int(x) for x in y_train])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:33.916468Z","iopub.execute_input":"2021-10-15T02:25:33.916800Z","iopub.status.idle":"2021-10-15T02:25:35.626593Z","shell.execute_reply.started":"2021-10-15T02:25:33.916767Z","shell.execute_reply":"2021-10-15T02:25:35.625665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습 파라미터 설정\nlearning_rate = 0.001\ntraining_epochs = 15\nbatch_size = 100\ndrop_prob = 0.3","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:45.495590Z","iopub.execute_input":"2021-10-15T02:25:45.495997Z","iopub.status.idle":"2021-10-15T02:25:45.501507Z","shell.execute_reply.started":"2021-10-15T02:25:45.495963Z","shell.execute_reply":"2021-10-15T02:25:45.500425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear1 = torch.nn.Linear(784,512,bias=True)\nlinear2 = torch.nn.Linear(512,512,bias=True)\nlinear3 = torch.nn.Linear(512,512,bias=True)\nlinear4 = torch.nn.Linear(512,512,bias=True)\nlinear5 = torch.nn.Linear(512,10,bias=True)\nrelu = torch.nn.ReLU()\ndropout = torch.nn.Dropout(p=drop_prob)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:47.746514Z","iopub.execute_input":"2021-10-15T02:25:47.746878Z","iopub.status.idle":"2021-10-15T02:25:47.770128Z","shell.execute_reply.started":"2021-10-15T02:25:47.746847Z","shell.execute_reply":"2021-10-15T02:25:47.769091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Init => Xavier Init\ntorch.nn.init.xavier_uniform_(linear1.weight)\ntorch.nn.init.xavier_uniform_(linear2.weight)\ntorch.nn.init.xavier_uniform_(linear3.weight)\ntorch.nn.init.xavier_uniform_(linear4.weight)\ntorch.nn.init.xavier_uniform_(linear5.weight)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:48.174894Z","iopub.execute_input":"2021-10-15T02:25:48.175276Z","iopub.status.idle":"2021-10-15T02:25:48.244014Z","shell.execute_reply.started":"2021-10-15T02:25:48.175247Z","shell.execute_reply":"2021-10-15T02:25:48.242984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader, TensorDataset\n\ntrain_dataset = TensorDataset(x_train, y_train)\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:25:59.130136Z","iopub.execute_input":"2021-10-15T02:25:59.130480Z","iopub.status.idle":"2021-10-15T02:25:59.135522Z","shell.execute_reply.started":"2021-10-15T02:25:59.130451Z","shell.execute_reply":"2021-10-15T02:25:59.134451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ======================================\n# relu는 맨 마지막 레이어에서 빼는 것이 좋다.\n# ======================================\nmodel = torch.nn.Sequential(linear1,relu,dropout,\n                            linear2,relu,dropout,\n                            linear3,relu,dropout,\n                            linear4,relu,dropout,\n                            linear5).to(device)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:27:17.864808Z","iopub.execute_input":"2021-10-15T02:27:17.865140Z","iopub.status.idle":"2021-10-15T02:27:22.973778Z","shell.execute_reply.started":"2021-10-15T02:27:17.865113Z","shell.execute_reply":"2021-10-15T02:27:22.972882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 손실함수와 최적화 함수\nloss = torch.nn.CrossEntropyLoss().to(device) # softmax 내부적으로 계산\noptimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) ","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:27:26.804061Z","iopub.execute_input":"2021-10-15T02:27:26.804398Z","iopub.status.idle":"2021-10-15T02:27:26.811961Z","shell.execute_reply.started":"2021-10-15T02:27:26.804369Z","shell.execute_reply":"2021-10-15T02:27:26.810924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_batch = len(train_loader)\n\nmodel.train() # 주의사항 drop_out = True\n\nfor epoch in range(training_epochs):\n    avg_cost = 0\n\n    for X, Y in train_loader:\n\n        X = X.to(device)\n        Y = Y.to(device)\n\n        # 그래디언트 초기화\n        optimizer.zero_grad()\n        # Forward 계산\n        hypothesis = model(X)\n        # Error 계산\n        cost = loss(hypothesis, Y)\n        # Backparopagation\n        cost.backward()\n        # 가중치 갱신\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')","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:28:28.879317Z","iopub.execute_input":"2021-10-15T02:28:28.879661Z","iopub.status.idle":"2021-10-15T02:29:05.454690Z","shell.execute_reply.started":"2021-10-15T02:28:28.879631Z","shell.execute_reply":"2021-10-15T02:29:05.453806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    model.eval()\n    \n    x_test = x_test.to(device)\n    y_test_pred = model(x_test)\n    y_test_pred = torch.argmax(y_test_pred,1)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:29:07.618349Z","iopub.execute_input":"2021-10-15T02:29:07.618661Z","iopub.status.idle":"2021-10-15T02:29:07.636676Z","shell.execute_reply.started":"2021-10-15T02:29:07.618632Z","shell.execute_reply":"2021-10-15T02:29:07.635809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred = y_test_pred.cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:30:17.979782Z","iopub.execute_input":"2021-10-15T02:30:17.980131Z","iopub.status.idle":"2021-10-15T02:30:17.984967Z","shell.execute_reply.started":"2021-10-15T02:30:17.980104Z","shell.execute_reply":"2021-10-15T02:30:17.983788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv(\"/kaggle/input/2021-ai-w7-p1/submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:30:24.011854Z","iopub.execute_input":"2021-10-15T02:30:24.012292Z","iopub.status.idle":"2021-10-15T02:30:24.031852Z","shell.execute_reply.started":"2021-10-15T02:30:24.012257Z","shell.execute_reply":"2021-10-15T02:30:24.030962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit['Label'] = y_test_pred\nsubmit","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:30:24.938196Z","iopub.execute_input":"2021-10-15T02:30:24.938496Z","iopub.status.idle":"2021-10-15T02:30:24.956901Z","shell.execute_reply.started":"2021-10-15T02:30:24.938469Z","shell.execute_reply":"2021-10-15T02:30:24.955932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('./DNN_MNIST_prediction.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T02:30:26.058866Z","iopub.execute_input":"2021-10-15T02:30:26.059200Z","iopub.status.idle":"2021-10-15T02:30:26.083196Z","shell.execute_reply.started":"2021-10-15T02:30:26.059173Z","shell.execute_reply":"2021-10-15T02:30:26.082464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}