{"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-08T05:35:12.130814Z","iopub.execute_input":"2021-10-08T05:35:12.131154Z","iopub.status.idle":"2021-10-08T05:35:12.144602Z","shell.execute_reply.started":"2021-10-08T05:35:12.131117Z","shell.execute_reply":"2021-10-08T05:35:12.14378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom torch.utils.data import DataLoader\n\nimport torch\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport random","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:12.146508Z","iopub.execute_input":"2021-10-08T05:35:12.147233Z","iopub.status.idle":"2021-10-08T05:35:12.15335Z","shell.execute_reply.started":"2021-10-08T05:35:12.1472Z","shell.execute_reply":"2021-10-08T05:35:12.152439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device='cuda' if torch.cuda.is_available() else \"cpu\"\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:12.155029Z","iopub.execute_input":"2021-10-08T05:35:12.155757Z","iopub.status.idle":"2021-10-08T05:35:12.165881Z","shell.execute_reply.started":"2021-10-08T05:35:12.155721Z","shell.execute_reply":"2021-10-08T05:35:12.165046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/2021-ai-w7-p1/mnist_train.csv\")\n#y_train = train['label']\n#x_train = train.drop(['label'], axis=1)\ntest = pd.read_csv(\"/kaggle/input/2021-ai-w7-p1/mnist_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:12.168641Z","iopub.execute_input":"2021-10-08T05:35:12.16938Z","iopub.status.idle":"2021-10-08T05:35:18.983295Z","shell.execute_reply.started":"2021-10-08T05:35:12.169344Z","shell.execute_reply":"2021-10-08T05:35:18.982542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:18.985247Z","iopub.execute_input":"2021-10-08T05:35:18.985451Z","iopub.status.idle":"2021-10-08T05:35:19.015321Z","shell.execute_reply.started":"2021-10-08T05:35:18.985428Z","shell.execute_reply":"2021-10-08T05:35:19.014541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.016617Z","iopub.execute_input":"2021-10-08T05:35:19.017072Z","iopub.status.idle":"2021-10-08T05:35:19.044201Z","shell.execute_reply.started":"2021-10-08T05:35:19.017034Z","shell.execute_reply":"2021-10-08T05:35:19.043474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.drop(['Unnamed: 0'],axis=1)\ntest=test.drop(['Unnamed: 0'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.045479Z","iopub.execute_input":"2021-10-08T05:35:19.046394Z","iopub.status.idle":"2021-10-08T05:35:19.19175Z","shell.execute_reply.started":"2021-10-08T05:35:19.046354Z","shell.execute_reply":"2021-10-08T05:35:19.190933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['784']\nx_train = train.drop(['784'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.193141Z","iopub.execute_input":"2021-10-08T05:35:19.193695Z","iopub.status.idle":"2021-10-08T05:35:19.309756Z","shell.execute_reply.started":"2021-10-08T05:35:19.193654Z","shell.execute_reply":"2021-10-08T05:35:19.308813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=torch.FloatTensor(x_train.values).to(device)\ny_train=torch.LongTensor(y_train.values).to(device)\ntest=torch.FloatTensor(test.values).to(device)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.311295Z","iopub.execute_input":"2021-10-08T05:35:19.311731Z","iopub.status.idle":"2021-10-08T05:35:19.501985Z","shell.execute_reply.started":"2021-10-08T05:35:19.311673Z","shell.execute_reply":"2021-10-08T05:35:19.501232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.505258Z","iopub.execute_input":"2021-10-08T05:35:19.505533Z","iopub.status.idle":"2021-10-08T05:35:19.513313Z","shell.execute_reply.started":"2021-10-08T05:35:19.505498Z","shell.execute_reply":"2021-10-08T05:35:19.512371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.515052Z","iopub.execute_input":"2021-10-08T05:35:19.515312Z","iopub.status.idle":"2021-10-08T05:35:19.521044Z","shell.execute_reply.started":"2021-10-08T05:35:19.515278Z","shell.execute_reply":"2021-10-08T05:35:19.520081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.522761Z","iopub.execute_input":"2021-10-08T05:35:19.523332Z","iopub.status.idle":"2021-10-08T05:35:19.532099Z","shell.execute_reply.started":"2021-10-08T05:35:19.523297Z","shell.execute_reply":"2021-10-08T05:35:19.531173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 100","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.533564Z","iopub.execute_input":"2021-10-08T05:35:19.534144Z","iopub.status.idle":"2021-10-08T05:35:19.537919Z","shell.execute_reply.started":"2021-10-08T05:35:19.534111Z","shell.execute_reply":"2021-10-08T05:35:19.537023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader = DataLoader(dataset=train,batch_size=batch_size, drop_last=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.539547Z","iopub.execute_input":"2021-10-08T05:35:19.539803Z","iopub.status.idle":"2021-10-08T05:35:19.548175Z","shell.execute_reply.started":"2021-10-08T05:35:19.539772Z","shell.execute_reply":"2021-10-08T05:35:19.547251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.dtype","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.54996Z","iopub.execute_input":"2021-10-08T05:35:19.550516Z","iopub.status.idle":"2021-10-08T05:35:19.558295Z","shell.execute_reply.started":"2021-10-08T05:35:19.55048Z","shell.execute_reply":"2021-10-08T05:35:19.557445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear1=torch.nn.Linear(784,2048,bias=True)\nlinear2=torch.nn.Linear(2048,1024,bias=True)\nlinear3=torch.nn.Linear(1024,1024,bias=True)\nlinear4=torch.nn.Linear(1024,1024,bias=True)\nlinear5=torch.nn.Linear(1024,1024,bias=True)\nlinear6=torch.nn.Linear(1024,10,bias=True)\nLeakyReLU=torch.nn.LeakyReLU()\ndropout=torch.nn.Dropout(p=0.2)\nReLU=torch.nn.ReLU()\nELU=torch.nn.ELU()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.560073Z","iopub.execute_input":"2021-10-08T05:35:19.560374Z","iopub.status.idle":"2021-10-08T05:35:19.607882Z","shell.execute_reply.started":"2021-10-08T05:35:19.560309Z","shell.execute_reply":"2021-10-08T05:35:19.607265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=nn.Sequential(linear1,ELU,dropout,linear2,ELU,dropout,linear3,ELU,dropout,linear4,ELU,dropout,linear5,ELU,dropout,linear6).to(device)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.608968Z","iopub.execute_input":"2021-10-08T05:35:19.609219Z","iopub.status.idle":"2021-10-08T05:35:19.62109Z","shell.execute_reply.started":"2021-10-08T05:35:19.609189Z","shell.execute_reply":"2021-10-08T05:35:19.620446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss=torch.nn.CrossEntropyLoss().to(device)#softmax 포함되어있음\noptimizer=torch.optim.Adam(model.parameters(),lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.622328Z","iopub.execute_input":"2021-10-08T05:35:19.62258Z","iopub.status.idle":"2021-10-08T05:35:19.627963Z","shell.execute_reply.started":"2021-10-08T05:35:19.62255Z","shell.execute_reply":"2021-10-08T05:35:19.627183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(2000):\n    optimizer.zero_grad()\n    hypothesis=model(x_train)\n    cost=loss(hypothesis,y_train)\n    \n        \n    scheduler = optim.lr_scheduler.LambdaLR(optimizer=optimizer,\n                                lr_lambda=lambda epoch: 0.90** epoch,\n                                last_epoch=-1,\n                                verbose=False)\n    \n    cost.backward()\n    optimizer.step()\n    \n    if epoch%50==0:\n        print(epoch,cost.item())","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:35:19.629085Z","iopub.execute_input":"2021-10-08T05:35:19.629735Z","iopub.status.idle":"2021-10-08T05:39:30.224686Z","shell.execute_reply.started":"2021-10-08T05:35:19.629703Z","shell.execute_reply":"2021-10-08T05:39:30.223935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    prediction=model(test)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:30.226373Z","iopub.execute_input":"2021-10-08T05:39:30.227326Z","iopub.status.idle":"2021-10-08T05:39:30.231595Z","shell.execute_reply.started":"2021-10-08T05:39:30.227295Z","shell.execute_reply":"2021-10-08T05:39:30.230923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=(torch.argmax(prediction,1)).cpu().numpy().astype(int)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:30.232756Z","iopub.execute_input":"2021-10-08T05:39:30.233158Z","iopub.status.idle":"2021-10-08T05:39:32.010121Z","shell.execute_reply.started":"2021-10-08T05:39:30.233127Z","shell.execute_reply":"2021-10-08T05:39:32.009342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(result)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.011338Z","iopub.execute_input":"2021-10-08T05:39:32.011606Z","iopub.status.idle":"2021-10-08T05:39:32.020578Z","shell.execute_reply.started":"2021-10-08T05:39:32.011574Z","shell.execute_reply":"2021-10-08T05:39:32.019507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv('/kaggle/input/2021-ai-w7-p1/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.021947Z","iopub.execute_input":"2021-10-08T05:39:32.022807Z","iopub.status.idle":"2021-10-08T05:39:32.036264Z","shell.execute_reply.started":"2021-10-08T05:39:32.022756Z","shell.execute_reply":"2021-10-08T05:39:32.03558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.037475Z","iopub.execute_input":"2021-10-08T05:39:32.037722Z","iopub.status.idle":"2021-10-08T05:39:32.045599Z","shell.execute_reply.started":"2021-10-08T05:39:32.037689Z","shell.execute_reply":"2021-10-08T05:39:32.044881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label']=result[0:]","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.046939Z","iopub.execute_input":"2021-10-08T05:39:32.04745Z","iopub.status.idle":"2021-10-08T05:39:32.053696Z","shell.execute_reply.started":"2021-10-08T05:39:32.047384Z","shell.execute_reply":"2021-10-08T05:39:32.052931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.054936Z","iopub.execute_input":"2021-10-08T05:39:32.055437Z","iopub.status.idle":"2021-10-08T05:39:32.066581Z","shell.execute_reply.started":"2021-10-08T05:39:32.055403Z","shell.execute_reply":"2021-10-08T05:39:32.065762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T05:39:32.067822Z","iopub.execute_input":"2021-10-08T05:39:32.068347Z","iopub.status.idle":"2021-10-08T05:39:32.093589Z","shell.execute_reply.started":"2021-10-08T05:39:32.068314Z","shell.execute_reply":"2021-10-08T05:39:32.092926Z"},"trusted":true},"execution_count":null,"outputs":[]}]}