{"id":8436,"date":"2019-11-04T14:50:23","date_gmt":"2019-11-04T05:50:23","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=8436"},"modified":"2020-05-28T10:00:19","modified_gmt":"2020-05-28T01:00:19","slug":"mnist-%ec%88%ab%ec%9e%90-%eb%b6%84%eb%a5%98-%ec%a0%95%ed%99%95%eb%8f%84-99-3-%eb%aa%a8%eb%8d%b8","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=8436","title":{"rendered":"MNIST \uc22b\uc790 \ubd84\ub958 \uc815\ud655\ub3c4 99.5% \ubaa8\ub378"},"content":{"rendered":"<p>0~9\uae4c\uc9c0\uc758 \uc190\uae00\uc528 \uc22b\uc790 \ub370\uc774\ud130\uc778 MNIST\uc744 \ub300\uc0c1\uc73c\ub85c 99% \uc774\uc0c1\uc758 \ubd84\ub958 \uc815\ud655\ub3c4\uc5d0 \ub300\ud55c \ubaa8\ub378 \ud14c\uc2a4\ud2b8\uc5d0 \ub300\ud55c \uae00\uc785\ub2c8\ub2e4. \uc774\uc640 \uc720\uc0ac\ud55c \uae00\uc774 2\uac1c \uc788\uc5c8\ub294\ub370, \uccab\ubc88\uc9f8\ub294 \ud37c\uc149\ud2b8\ub860 \ubc29\uc2dd\uc774\uace0 \ub450\ubc88\uc9f8\ub294 DNN \ubc29\uc2dd\uc785\ub2c8\ub2e4. \uc544\ub798\ub294 MNIST \ub370\uc774\ud130\uc14b\uc744 DNN \ubc29\uc2dd\uc73c\ub85c \ubd84\ub958\ud55c \uae00\uc785\ub2c8\ub2e4.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"CGrVjO9zcr\"><p><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=7805\">Model \ud655\uc7a5\uacfc \uac00\uc911\uce58\uac12 \ubcc0\uacbd\uc744 \ud1b5\ud55c \uc608\uce21 \uc815\ud655\ub3c4 \ud5a5\uc0c1<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Model \ud655\uc7a5\uacfc \uac00\uc911\uce58\uac12 \ubcc0\uacbd\uc744 \ud1b5\ud55c \uc608\uce21 \uc815\ud655\ub3c4 \ud5a5\uc0c1&#8221; &#8212; GIS Developer\" src=\"http:\/\/www.gisdeveloper.co.kr\/?p=7805&#038;embed=true#?secret=t47QrMQlh7#?secret=CGrVjO9zcr\" data-secret=\"CGrVjO9zcr\" width=\"525\" height=\"296\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\uc704\uc758 \uae00\uacfc\ub294 \ub2e4\ub974\uac8c \uc774 \uae00\uc740 CNN \ubc29\uc2dd\uc73c\ub85c \ubd84\ub958 \uc815\ud655\ub3c4\ub97c \uac1c\uc120\ud588\uc2b5\ub2c8\ub2e4. <a href='https:\/\/rodrigob.github.io\/are_we_there_yet\/build\/classification_datasets_results.html'>MNIST \ubd84\ub958\uc5d0 \ub300\ud55c \uc815\ud655\ub3c4 \uc21c\uc704<\/a>\ub97c \ubcf4\uba74 \uc57d 40\uac1c\uc5ec\uc758 \uc21c\uc704\uac00 \uc874\uc7ac\ud569\ub2c8\ub2e4. \ub2e4\ud589\uc778\uac74 \uc774 \uae00\uc758 \ubaa8\ub378\uc774 \uc774 \uc21c\uc704\uc758 \uc0c1\ub2e8\uc5d0 \uc704\uce58\ud55c\ub2e4\ub294 \uac83\uc785\ub2c8\ub2e4.<\/p>\n<p>\ud30c\uc774\ud1a0\uce58\ub97c \uc0ac\uc6a9\ud588\uc73c\uba70, \ud574\ub2f9 \ucf54\ub4dc\ub97c \ub300\ub7b5\uc801\uc73c\ub85c \uc0b4\ud3b4\ubcf4\uba74 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4. \ub298 \uadf8\ub807\ub4ef\uc774 \ud544\uc694\ud55c \ud328\ud0a4\uc9c0\ub97c \ud3ec\ud568\ud569\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport torch\r\nimport torch.nn as nn\r\nimport torch.optim as optim\r\nimport torchvision.datasets as dset\r\nimport torchvision.transforms as transforms\r\nfrom torch.utils.data import DataLoader\r\nfrom torch.optim import lr_scheduler\r\n<\/pre>\n<p>\ubc30\uce58 \ud06c\uae30\uc640 \ud559\uc2b5\ub960, \uc5d0\ud3ed\ub2e8\uc704\uc758 \ubc18\ubcf5\ud560 \ud559\uc2b5\uc218\ub97c \uba3c\uc800 \uc9c0\uc815\ud569\ub2c8\ub2e4. \uc190\uc2e4\uac12\uc5d0 \ud070 \uc601\ud5a5\uc744 \uc8fc\ub294 \ud558\uc774\ud37c \ud30c\ub77c\uba54\ud130\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nbatch_size = 100\r\nlearning_rate = 0.001\r\nnum_epoch = 20\r\n<\/pre>\n<p>\ud559\uc2b5\uacfc \ud14c\uc2a4\ud2b8\ub97c \uc704\ud55c MNSIT \ub370\uc774\ud130\ub97c \ub85c\ub529\ud569\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nmnist_train = dset.MNIST(\".\/\", train=True, \r\n    transform = transforms.Compose([\r\n        transforms.ToTensor(),\r\n        transforms.Normalize(mean=(0.1307,), std=(0.3081,))\r\n    ]),\r\n    target_transform=None,\r\n    download=True)\r\n\r\nmnist_test = dset.MNIST(\".\/\", train=False, \r\n    transform=transforms.Compose([\r\n        transforms.ToTensor(),\r\n        transforms.Normalize(mean=(0.1307,), std=(0.3081,))\r\n    ]), target_transform=None, download=True)\r\n\r\ntrain_loader = DataLoader(mnist_train, batch_size=batch_size, shuffle=True, drop_last=True)\r\ntest_loader = DataLoader(mnist_test, batch_size=batch_size, shuffle=False, drop_last=True)\r\n<\/pre>\n<p>\uc911\uc694\ud55c \uc810\uc740 \ub370\uc774\ud130\uc5d0 \ub300\ud55c \uc815\uaddc\ud654\uac00 \uc801\uc6a9\ub429\ub2c8\ub2e4. \uc815\uaddc\ud654 \ubc29\uc2dd\uc740 \uc804\uccb4 \ub370\uc774\ud130\uc5d0 \ub300\ud55c \ud3c9\uade0\uacfc \ud45c\uc900\ud3b8\ucc28\ub97c \uc0ac\uc6a9\ud588\uc2b5\ub2c8\ub2e4. \uc774 \ud45c\uc900\uacfc \ud45c\uc900\ud3b8\ucc28\ub97c \uad6c\ud558\ub294 \ubc29\uc2dd\uc740 \uc544\ub798\uae00\uc744 \ucc38\uace0\ud558\uae30 \ubc14\ub78d\ub2c8\ub2e4.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"BwfEUggyvf\"><p><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=8168\">\uc774\ubbf8\uc9c0 Dataset\uc5d0 \ub300\ud55c \ud3c9\uade0\uacfc \ud45c\uc900\ud3b8\ucc28 \uad6c\ud558\uae30<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;\uc774\ubbf8\uc9c0 Dataset\uc5d0 \ub300\ud55c \ud3c9\uade0\uacfc \ud45c\uc900\ud3b8\ucc28 \uad6c\ud558\uae30&#8221; &#8212; GIS Developer\" src=\"http:\/\/www.gisdeveloper.co.kr\/?p=8168&#038;embed=true#?secret=RngreIoHex#?secret=BwfEUggyvf\" data-secret=\"BwfEUggyvf\" width=\"525\" height=\"296\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\ub2e4\uc74c\uc740 \uc2e0\uacbd\ub9dd\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nclass CNN(nn.Module):\r\n    def __init__(self):\r\n        super(CNN,self).__init__()\r\n        self.layer = nn.Sequential(\r\n            nn.Conv2d(1, 16, 3, padding=1),\r\n            nn.BatchNorm2d(16),\r\n            nn.ReLU(),\r\n            nn.Conv2d(16, 32, 3,padding=1),\r\n            nn.BatchNorm2d(32),\r\n            nn.ReLU(),\r\n            nn.MaxPool2d(2, 2),           \r\n            nn.Conv2d(32, 64, 3,padding=1),\r\n            nn.BatchNorm2d(64),\r\n            nn.ReLU(),\r\n            nn.MaxPool2d(2, 2)        \r\n        )\r\n        self.fc_layer = nn.Sequential(\r\n            nn.Linear(64*7*7, 128),\r\n            nn.BatchNorm1d(128),\r\n            nn.ReLU(),\r\n            nn.Linear(128, 64),\r\n            nn.BatchNorm1d(64),\r\n            nn.ReLU(),            \r\n            nn.Linear(64, 10),\r\n        )       \r\n\r\n    def forward(self,x):\r\n        out = self.layer(x)\r\n        out = out.view(batch_size, -1)\r\n        out = self.fc_layer(out)\r\n        return out\r\n<\/pre>\n<p>\uc911\uc694\ud55c \ubd80\ubd84\uc740 Convolution Layer\uc640 Affine Layer\uc5d0 \ubc30\uce58\uc815\uaddc\ud654\uac00 \ubc18\uc601\ub418\uc5b4 \uc788\ub2e4\ub294 \uc810\uc785\ub2c8\ub2e4. \ubb3c\ub860 \uc2e0\uacbd\ub9dd \uad6c\uc131 \ub808\uc774\uc5b4\uc640 \uac01 \ub808\uc774\uc5b4\uc758 \ub274\ub7f0\uc218\ub3c4 \uc911\uc694\ud55c \ud558\uc774\ud37c\ud30c\ub77c\uba54\ud130\uc785\ub2c8\ub2e4.<\/p>\n<p>\ud559\uc2b5 \ucf54\ub4dc\uc785\ub2c8\ub2e4. \ubbf8\ubd84\uac12 \uacc4\uc0b0\uc744 \ud1b5\ud574 \uc5bb\uc5b4\uc9c4 \uacbd\uc0ac\ub3c4 \ubc29\ud5a5\uc73c\ub85c\uc758 \ud558\uac15 \ubc29\uc2dd\uc740 Adam \ubc29\uc2dd\uc744 \uc0ac\uc6a9\ud588\uace0, \ud559\uc2b5\ub960\uc744 \uace0\uc815\ud558\uc9c0 \uc54a\uace0 ReduceLROnPlateau \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc190\uc2e4\uac12\uc758 \uac10\uc18c \uacbd\ud5a5\uc5d0 \ub530\ub77c \ubcc0\uacbd\ud558\ub3c4\ub85d \ud558\uc600\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\r\n\r\nmodel = CNN().to(device)\r\nloss_func = nn.CrossEntropyLoss().to(device)\r\noptimizer = optim.Adam(model.parameters(), lr=learning_rate)\r\nscheduler = lr_scheduler.ReduceLROnPlateau(optimizer,threshold=0.1, patience=1, mode='min')    \r\n\r\nfor i in range(1, num_epoch+1):\r\n    for _,[image,label] in enumerate(train_loader):\r\n        x = image.to(device)\r\n        y_= label.to(device)\r\n        \r\n        optimizer.zero_grad()\r\n        output = model.forward(x)\r\n        loss = loss_func(output, y_)\r\n        loss.backward()\r\n        optimizer.step()\r\n\r\n    scheduler.step(loss)      \r\n    print('Epoch: {}, Loss: {}, LR: {}'.format(i, loss.item(), scheduler.optimizer.state_dict()['param_groups'][0]['lr']))\r\n<\/pre>\n<p>\ud559\uc2b5 \ub370\uc774\ud130\ub97c \ud1b5\ud574 \ud559\uc2b5\ub41c \ubaa8\ub378\uc744 \ud14c\uc2a4\ud2b8 \ub370\uc774\ud130\ub97c \ud1b5\ud574 \uc815\ud655\ub3c4\ub97c \uc5bb\ub294 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ncorrect = 0\r\ntotal = 0\r\n\r\nmodel.eval()\r\nwith torch.no_grad():\r\n  for image,label in test_loader:\r\n      x = image.to(device)\r\n      y_= label.to(device)\r\n\r\n      output = model.forward(x)\r\n      _,output_index = torch.max(output, 1)\r\n\r\n      total += label.size(0)\r\n      correct += (output_index == y_).sum().float()\r\n\r\n  print(\"Accuracy of Test Data: {}%\".format(100.0*correct\/total))\r\n<\/pre>\n<p>\uc2e4\uc81c \uc2e4\ud589\ud574 \ubcf4\uba74, \uc815\ud655\ub3c4\uac00 99.5\uc744 \ub118\uae30\ub294 \uac83\uc744 \ubcfc \uc218 \uc788\uc5c8\uc2b5\ub2c8\ub2e4. \uc544\ub798\ub294 \uadf8 \uc911 \ud558\ub098\uc758 \ucd9c\ub825 \uacb0\uacfc\uc785\ub2c8\ub2e4.<\/p>\n<div>\nEpoch:1, Loss: 0.015423115342855453, LR: 0.001<br \/>\nEpoch:2, Loss: 0.00971189048141241, LR: 0.001<br \/>\nEpoch:3, Loss: 0.030652683228254318, LR: 0.001<br \/>\nEpoch:4, Loss: 0.011247940361499786, LR: 0.0001<br \/>\nEpoch:5, Loss: 0.001827826490625739, LR: 0.0001<br \/>\nEpoch:6, Loss: 0.0049532032571733, LR: 0.0001<br \/>\nEpoch:7, Loss: 0.0009714126354083419, LR: 0.0001<br \/>\nEpoch:8, Loss: 0.001510353060439229, LR: 0.0001<br \/>\nEpoch:9, Loss: 0.00044545173295773566, LR: 0.0001<br \/>\nEpoch:10, Loss: 0.0010514688910916448, LR: 0.0001<br \/>\nEpoch:11, Loss: 0.0006617116741836071, LR: 1e-05<br \/>\nEpoch:12, Loss: 0.0009317684452980757, LR: 1e-05<br \/>\nEpoch:13, Loss: 0.00043862819438800216, LR: 1.0000000000000002e-06<br \/>\nEpoch:14, Loss: 0.011570921167731285, LR: 1.0000000000000002e-06<br \/>\nEpoch:15, Loss: 0.0028407489880919456, LR: 1.0000000000000002e-07<br \/>\nEpoch:16, Loss: 0.00031417846912518144, LR: 1.0000000000000002e-07<br \/>\nEpoch:17, Loss: 0.0014804458478465676, LR: 1.0000000000000002e-07<br \/>\nEpoch:18, Loss: 0.012818637304008007, LR: 1.0000000000000004e-08<br \/>\nEpoch:19, Loss: 0.0010410761460661888, LR: 1.0000000000000004e-08<br \/>\nEpoch:20, Loss: 0.00025289534823969007, LR: 1.0000000000000004e-08<br \/>\nAccuracy of Test Data: 99.52999877929688%\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>0~9\uae4c\uc9c0\uc758 \uc190\uae00\uc528 \uc22b\uc790 \ub370\uc774\ud130\uc778 MNIST\uc744 \ub300\uc0c1\uc73c\ub85c 99% \uc774\uc0c1\uc758 \ubd84\ub958 \uc815\ud655\ub3c4\uc5d0 \ub300\ud55c \ubaa8\ub378 \ud14c\uc2a4\ud2b8\uc5d0 \ub300\ud55c \uae00\uc785\ub2c8\ub2e4. \uc774\uc640 \uc720\uc0ac\ud55c \uae00\uc774 2\uac1c \uc788\uc5c8\ub294\ub370, \uccab\ubc88\uc9f8\ub294 \ud37c\uc149\ud2b8\ub860 \ubc29\uc2dd\uc774\uace0 \ub450\ubc88\uc9f8\ub294 DNN \ubc29\uc2dd\uc785\ub2c8\ub2e4. \uc544\ub798\ub294 MNIST \ub370\uc774\ud130\uc14b\uc744 DNN \ubc29\uc2dd\uc73c\ub85c \ubd84\ub958\ud55c \uae00\uc785\ub2c8\ub2e4. Model \ud655\uc7a5\uacfc \uac00\uc911\uce58\uac12 \ubcc0\uacbd\uc744 \ud1b5\ud55c \uc608\uce21 \uc815\ud655\ub3c4 \ud5a5\uc0c1 \uc704\uc758 \uae00\uacfc\ub294 \ub2e4\ub974\uac8c \uc774 \uae00\uc740 CNN \ubc29\uc2dd\uc73c\ub85c \ubd84\ub958 \uc815\ud655\ub3c4\ub97c \uac1c\uc120\ud588\uc2b5\ub2c8\ub2e4. MNIST \ubd84\ub958\uc5d0 \ub300\ud55c \uc815\ud655\ub3c4 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=8436\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;MNIST \uc22b\uc790 \ubd84\ub958 \uc815\ud655\ub3c4 99.5% \ubaa8\ub378&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131,132],"tags":[],"class_list":["post-8436","post","type-post","status-publish","format-standard","hentry","category-python","category-deep-machine-learning"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8436"}],"version-history":[{"count":9,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8436\/revisions"}],"predecessor-version":[{"id":9357,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8436\/revisions\/9357"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8436"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}