{"id":2485,"date":"2020-02-03T21:14:00","date_gmt":"2020-02-03T12:14:00","guid":{"rendered":"https:\/\/julialang.kr\/?p=2485"},"modified":"2020-02-05T03:34:24","modified_gmt":"2020-02-04T18:34:24","slug":"flux-mnist-example","status":"publish","type":"post","link":"https:\/\/julialang.kr\/?p=2485","title":{"rendered":"[Flux] MNIST Example"},"content":{"rendered":"\n<p><strong>\uc5ec\uae30\uc11c \uc0ac\uc6a9\ud558\ub294 Flux\ub294 v0.10.1 \uc774\ub2e4.<\/strong><\/p>\n\n\n\n<p>Model-zoo\uc758 MNIST \uc608\uc81c\ub97c \ucc38\uace0(<a href=\"https:\/\/github.com\/FluxML\/model-zoo\/blob\/master\/vision\/mnist\/mlp.jl\">https:\/\/github.com\/FluxML\/model-zoo\/blob\/master\/vision\/mnist\/mlp.jl<\/a>)<\/p>\n\n\n\n<p>zoo\uc758 mnist\uc608\uc81c mlp\uac00 gpu\ubaa8\ub4dc\uc5d0\uc11c \uc7ac\ub300\ub85c \ub3d9\uc791\ud558\uc9c0 \uc54a\ub294\ub370 \uc8fc\uc694 \uc6d0\uc778\uc740 onecold function\uc774 gpu\ubaa8\ub4dc\uc5d0\uc11c \ubb38\uc81c\ub97c \uc77c\uc73c\ud0a8\ub2e4.<\/p>\n\n\n\n<p>\uc704 \uc608\uc81c\uc5d0\uc11c accuracy function\uc5d0 onecold\ub97c \uc0ac\uc6a9\ud558\uace0 train!\uc5d0\uc11c accuracy\ub97c \uc0ac\uc6a9\ud558\uae30 \ub54c\ubb38\uc5d0 \ubb38\uc81c\uac00 \ub41c\ub2e4.<\/p>\n\n\n\n<p>\ub530\ub77c\uc11c \uc544\ub798\uc640 \uac19\uc774 \ubc14\uafd4 \uc900\ub2e4<\/p>\n\n\n\n<p>accuracy(x,y) = mean(onecold(m(x)) .== onecold(y))  ==&gt;  accuracy(x,y) = mean(onecold(m(x)|&gt;cpu) .== onecold(y|&gt;cpu))<\/p>\n\n\n\n<p>\ub610\ud55c scalar\uc5f0\uc0b0\uc744 \ud560  \ub54c GPU\uac00 \ub290\ub824\uc9c4\ub2e4\ub294 \uacbd\uace0\uac00 \ub098\uc624\ub294\ub370 \uc544\ub798 \ucc98\ub7fc \ubc14\uafd4  \ub450\uba74 \ub41c\ub2e4.<br>(Warning: Performing scalar operations on GPU arrays: This is very slow, consider disallowing these operations with <code>allowscalar(false)<\/code>)<\/p>\n\n\n\n<p> allowscalar(false) \ub97c \uc124\uc815 \ud558\ub294 \uacbd\uc6b0 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud558\uae30 \ub54c\ubb34\uc5d0 \uc544\ub798 \uc640 \uac19\uc774 \ubcc0\uacbd \ud574\uc900\ub2e4.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p> Y = onehotbatch(labels,0:9) |&gt; gpu    ==&gt; Y = cu.(onehotbatch(labels,0:9)) |&gt; gpu<\/p>\n\n\n\n<p class=\"has-text-color has-normal-font-size has-vivid-red-color\"><strong>\uc218\uc815\ud55c \uc608\uc81c\ub294 jyputer notebook(html\ubc84\uc804) \uc5ec\uae30\ub97c \ub20c\ub7ec \uc8fc\uc138\uc694 &#8211;&gt; <a rel=\"noreferrer noopener\" aria-label=\"\uc18c\uc2a4 (\uc0c8\ud0ed\uc73c\ub85c \uc5f4\uae30)\" href=\"https:\/\/julialang.kr\/wp-content\/uploads\/2020\/02\/mlp-mnist.html\" target=\"_blank\">\uc18c\uc2a4<\/a><\/strong><\/p>\n\n\n\n<p>\ucc38\uace0 : \uc785\ub825\ub370\uc774\ud130\uc758 \ubc30\uc5f4\uc774 (data,batch_size)\uc774\ub2e4 . \uc774 \uc608\uc5d0\uc11c 28&#215;28 \ud06c\uae30\uc758  \uc774\ubbf8\uc9c0 60,000\uac1c\ub294 784\uae38\uc758 \uc774\ubbf8\uc9c0 60,000\uac1c\ub85c \ud45c\ud604 \ud558\ub294\ub370<br>tensorflow\uc758 \uacbd\uc6b0 (batch_size,data) \ud615\ud0dc \uc8fd (60,000, 784)\ub85c \ub418\ub294\ub370 Flux \uc5d0\uc11c\ub294 (data,batch_size) \ud615\ud0dc\ub85c (784, 60,000) \uc774\ub2e4.<\/p>\n\n\n\n<p>Flux \uc5d0\uc11c y = W*x + b \ud615\ud0dc \uc778\ub370 W\ub294 (32,784) \ud615\ud0dc, \ub530\ub77c\uc11c (32,784) (784,60,000) =&gt; (32, 60,000) \uc73c\ub85c \uc9c1\uad00\uc801\uc774\ub2e4.<br>TensorFlow\uc5d0\uc11c W\ub294 (784,32) \ud615\ud0dc\ub85c \uc8fc\uc5b4\uc9c0\uace0 y = Transpose(W)*x + b \ud615\ud0dc\ub97c \ucde8\ud55c\ub2e4.<br><\/p>\n\n\n\n<p>mnist.jl<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Flux\nusing Flux.Data.MNIST, Statistics\nusing Flux: onehotbatch, onecold, crossentropy\nusing Base.Iterators: repeated\nusing CuArrays\nCuArrays.allowscalar(false)\n\n# Classify MNIST digits with a simple multi-layer-perceptron\n# imgs : [(28x28),(28x28),...,(28x28))]  60,000\uac1c\uc758 \ub370\uc774\ud130\n# MNIST.images()\ub294 MNIST.images(:train)\uacfc \ub3d9\uc77c\ud558\uba70 60,000\uac1c\uc758 \ud559\uc2b5\ub370\uc774\ud130\ub97c \uac00\uc838\uc628\ub2e4.\nimgs = MNIST.images()\n# reshape.(imgs,:) : [(784,),(784,),...,(784,)]  60,000\uac1c\uc758 \ub370\uc774\ud130\n# X : (784x60,000)\nX = hcat(float.(reshape.(imgs,:))...)\n# labels : (60,000,)\nlabels = MNIST.labels()\n# label : 0 ~ 9\n# Y : (10x60,000)\nY = onehotbatch(labels,0:9)\n# Model\nm = Chain(\n  Dense(28^2,32,relu), # y1 = relu(W1*x + b1), y1 : (32x?), W1 : (32x784), b1 : (32,)\n  Dense(32,10), # y2 = W2*y1 + b2, y2 : (10,?), W2: (10x32), b2:(10,)\n  softmax\n)\nX = X |> gpu\nY = cu.(Y) |> gpu\nm = m |> gpu\nloss(x,y) = crossentropy(m(x),y)\naccuracy(x,y) = mean(onecold(m(x)|>cpu) .== onecold(y|>cpu))\n\ndataset = repeated((X,Y),200)\n\nopt = ADAM()\n\nFlux.train!(loss,params(m),dataset,opt)\n\nprintln(\"Train loss:\",loss(X,Y))\n\nprintln(\"Train accuracy:\", accuracy(X,Y))\n\n# Test Accuracy\ntX = hcat(float.(reshape.(MNIST.images(:test),:))...)\ntY = onehotbatch(MNIST.labels(:test),0:9)\n\ntX = tX |> gpu\ntY = cu.(tY) |> gpu\n\nprintln(\"Test loss:\",loss(tX,tY))\nprintln(\"Test accuracy:\", accuracy(tX,tY))\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uc5ec\uae30\uc11c \uc0ac\uc6a9\ud558\ub294 Flux\ub294 v0.10.1 \uc774\ub2e4. Model-zoo\uc758 MNIST \uc608\uc81c\ub97c \ucc38\uace0(https:\/\/github.com\/FluxML\/model-zoo\/blob\/master\/vision\/mnist\/mlp.jl) zoo\uc758 mnist\uc608\uc81c mlp\uac00 gpu\ubaa8\ub4dc\uc5d0\uc11c \uc7ac\ub300\ub85c \ub3d9\uc791\ud558\uc9c0 \uc54a\ub294\ub370 \uc8fc\uc694 \uc6d0\uc778\uc740 onecold function\uc774 gpu\ubaa8\ub4dc\uc5d0\uc11c \ubb38\uc81c\ub97c \uc77c\uc73c\ud0a8\ub2e4. \uc704 \uc608\uc81c\uc5d0\uc11c accuracy function\uc5d0 onecold\ub97c \uc0ac\uc6a9\ud558\uace0 train!\uc5d0\uc11c accuracy\ub97c \uc0ac\uc6a9\ud558\uae30 \ub54c\ubb38\uc5d0 \ubb38\uc81c\uac00 \ub41c\ub2e4. \ub530\ub77c\uc11c \uc544\ub798\uc640 \uac19\uc774 \ubc14\uafd4 \uc900\ub2e4 accuracy(x,y) = mean(onecold(m(x)) .== onecold(y)) ==&gt; accuracy(x,y) = mean(onecold(m(x)|&gt;cpu) .== onecold(y|&gt;cpu)) \ub610\ud55c scalar\uc5f0\uc0b0\uc744 \ud560 \ub54c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18,21],"tags":[],"_links":{"self":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2485"}],"collection":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2485"}],"version-history":[{"count":7,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2485\/revisions"}],"predecessor-version":[{"id":2494,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2485\/revisions\/2494"}],"wp:attachment":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}