{"id":1033,"date":"2020-10-10T03:45:35","date_gmt":"2020-10-09T18:45:35","guid":{"rendered":"http:\/\/leenux.kr\/?p=1033"},"modified":"2020-10-10T03:47:45","modified_gmt":"2020-10-09T18:47:45","slug":"pytorch-mnist-data-preprocess","status":"publish","type":"post","link":"https:\/\/leenux.kr\/?p=1033","title":{"rendered":"PyTorch MNIST Data Preprocess"},"content":{"rendered":"\n<h2>PyTorch \ud504\ub808\uc784\uc6cc\ud06c\ub97c \uc0ac\uc6a9\ud574 Tensorflow\ub54c\uc640 \uac19\uc774 MNIST \uc608\uc81c\ub97c \ud1b5\ud574 \uc774\ubbf8\uc9c0 \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud560 \uac83\uc774\ub2e4.  <\/h2>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ub77c\uc774\ube0c\ub7ec\ub9ac \ubd88\ub7ec\uc624\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nfrom torchvision import datasets, transforms<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>Batch Size \uc815\ud558\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>batch_size = 32\ntest_batch_size = 32<\/code><\/pre>\n\n\n\n<h1 id=\"Data-Loader-\ubd80\ub974\uae30\">Data Loader \ubd80\ub974\uae30<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>train_loader = torch.utils.data.DataLoader(\n                datasets.MNIST('dataset\/',\n                train= True,\n                download=True, \n                transform = transforms.Compose([\n                    transforms.ToTensor(),\n                    transforms.Normalize(mean=(0.5,), std=(0.5,))\n                ])),\n    batch_size= batch_size,\n    shuffle=True)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>test_loader = torch.utils.data.DataLoader(\n                datasets.MNIST('dataset\/',\n                train= True,\n                download=True, \n                transform = transforms.Compose([\n                    transforms.ToTensor(),\n                    transforms.Normalize(mean=(0.5,), std=(0.5,))\n                ])),\n    batch_size= test_batch_size,\n    shuffle=True)<\/code><\/pre>\n\n\n\n<h2 id=\"\uccab\ubc88\uc7ac-iteration\uc5d0\uc11c-\ub098\uc624\ub294-\ub370\uc774\ud130-\ud655\uc778\">\uccab\ubc88\uc7ac iteration\uc5d0\uc11c \ub098\uc624\ub294 \ub370\uc774\ud130 \ud655\uc778<\/h2>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\"><strong> \ud30c\uc774\ud1a0\uce58\ub294 DataLoader\ub97c \ubd88\ub7ec model\uc5d0 \ub123\uc74c <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>images, label = next(iter(train_loader))\nimages.shape, label.shape<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/10\/image-6.png\" alt=\"\" class=\"wp-image-1034\" width=\"706\" height=\"123\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-6.png 397w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-6-300x52.png 300w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-6-230x40.png 230w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-6-350x61.png 350w\" sizes=\"(max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<h1 id=\"\ub370\uc774\ud130-\uc2dc\uac01\ud654\">\ub370\uc774\ud130 \uc2dc\uac01\ud654<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>images[0].shape # \uc774\ubbf8\uc9c0 shape<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/10\/image-7.png\" alt=\"\" class=\"wp-image-1035\" width=\"334\" height=\"115\"\/><\/figure>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\uc790\uc6d0\uc218 \uc904\uc774\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>torch_image = torch.squeeze(images[0]) # \uc55e\uc5d0 \ucc28\uc6d0\uc218 \uc904\uc774\uae30\ntorch_image.shape<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>label = label[0].numpy()\n\nlabel.shape # \ud655\uc778 \uacb0\uacfc ()<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\"><strong>label \ubcc0\uc218\ub97c \ud655\uc778\ud574\ubcf4\uba74 1\uc758 \uac12\uc774 \uc788\ub2e4<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"188\" height=\"75\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/10\/image-9.png\" alt=\"\" class=\"wp-image-1037\"\/><\/figure>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\uc774\ubbf8\uc9c0 \ub744\uc6b0\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.imshow(torch_image)\nplt.title(label)\nplt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"292\" height=\"380\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/10\/image-8.png\" alt=\"\" class=\"wp-image-1036\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-8.png 292w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-8-231x300.png 231w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/10\/image-8-230x299.png 230w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>PyTorch \ud504\ub808\uc784\uc6cc\ud06c\ub97c \uc0ac\uc6a9\ud574 Tensorflow\ub54c\uc640 \uac19\uc774 MNIST \uc608\uc81c\ub97c \ud1b5\ud574 \uc774\ubbf8\uc9c0 \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud560 \uac83\uc774\ub2e4. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1039,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1033"}],"collection":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1033"}],"version-history":[{"count":1,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1038,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions\/1038"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/media\/1039"}],"wp:attachment":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}