{"id":2546,"date":"2020-03-04T14:16:41","date_gmt":"2020-03-04T05:16:41","guid":{"rendered":"https:\/\/julialang.kr\/?p=2546"},"modified":"2020-03-04T14:16:43","modified_gmt":"2020-03-04T05:16:43","slug":"flux-mnist-example-update","status":"publish","type":"post","link":"https:\/\/julialang.kr\/?p=2546","title":{"rendered":"[Flux] MNIST example update"},"content":{"rendered":"\n<p>\uc218\uc815\ubd80\ubb38:<\/p>\n\n\n\n<p>\uae30\uc874<br>accuracy(x,y) = mean(onecold(m(x) .== onecold(y))<br>loss(x,y) = crossentropy(m(x),y)<\/p>\n\n\n\n<p>\ubcc0\uacbd<br># \u03f5  : loss\ud568\uc218\uac00 NaN\uc774 \ub418\ub294\uac83\uc744 \ubc29\uc9c0<br>\u03f5 = 1.0f-32<br>loss(x,y) = crossentropy(m(x) .+ \u03f5,y)<br><br># onecold\uac00 GPU\uc5d0\uc11c\ub294 \uc5d0\ub7ec \ubc1c\uc0dd \ud558\uae30 \ub54c\ubb34\uc5d0 \uc544\ub798\ub85c \ub300\uccb4<br>\ucc38\uc870 : https:\/\/github.com\/FluxML\/Flux.jl\/issues\/556<br><br>compare(y::OneHotMatrix, y\u2032) = maximum(y\u2032, dims = 1) .== maximum(y .* y\u2032, dims = 1)<br>accuracy(x, y::OneHotMatrix) = mean(compare(y, m(x))) <\/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,OneHotMatrix,@epochs\nusing Base.Iterators: repeated\nusing CuArrays\nCuArrays.allowscalar(false)\n\n# loss\ud568\uc218\uc758 NaN \ubc29\uc9c0\n\u03f5 = 1.0f-32\n\n# Classify MNIST digits with a simple multi-layer-perceptron\n# imgs : &#91;(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,:) : &#91;(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)\n\n# GPU\ub85c \ubcf4\ub0b4\ub294 \uacbd\uc6b0 ;\ub97c \ubd99\uc5ec \uc5d0\ub7ec\uac00 \ub098\ub294 \uac83\uc744 \ubc29\uc9c0\nX = X |> gpu;\n\n# ;\uac00 \uc5c6\uc73c\uba74 \uc544\ub798 \ubb38\uc7a5\uc5d0\uc11c \uc5d0\ub7ec \ubc1c\uc0dd\n# \ub0b4\ubd80\uc801\uc73c\ub85c getindex\uc5d0\uc11c \uc5d0\ub7ec \ubc1c\uc0dd\nY = Y |> gpu;\n\nm = m |> gpu;\nloss(x,y) = crossentropy(m(x) .+ \u03f5,y)\n\n# \ucc38\uc870 : https:\/\/github.com\/FluxML\/Flux.jl\/issues\/556\ncompare(y::OneHotMatrix, y\u2032) = maximum(y\u2032, dims = 1) .== maximum(y .* y\u2032, dims = 1)\naccuracy(x, y::OneHotMatrix) = mean(compare(y, m(x)))\n\n# \uc544\ub798 accuracy\uc2e4\ud589\uc2dc \uc5d0\ub7ec \ubc1c\uc0dd : ERROR: scalar setindex! is disallowed\n\n# onecold\uac00 GPU\uae30\ubc18\uc5d0\uc11c \uc81c\ub300\ub85c \ub3d9\uc791 \ud558\uc9c0 \uc54a\uc74c\n# accuracy(x,y) = mean(onecold(m(x)) .== onecold(y))\n\ndataset = repeated((X,Y),200);\n\nopt = ADAM()\n\n@epochs 50 begin\n  Flux.train!(loss,params(m),dataset,opt)\n  println(\"Train loss:\",loss(X,Y))\n  println(\"Train accuracy:\", accuracy(X,Y))\nend\n\n# Test Accuracy\ntX = hcat(float.(reshape.(MNIST.images(:test),:))...)\ntY = onehotbatch(MNIST.labels(:test),0:9)\n\ntX = tX |> gpu;\ntY = tY |> gpu;\n\nprintln(\"Test loss:\",loss(tX,tY))\nprintln(\"Test accuracy:\", accuracy(tX,tY))<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc218\uc815\ubd80\ubb38: \uae30\uc874accuracy(x,y) = mean(onecold(m(x) .== onecold(y))loss(x,y) = crossentropy(m(x),y) \ubcc0\uacbd# \u03f5 : loss\ud568\uc218\uac00 NaN\uc774 \ub418\ub294\uac83\uc744 \ubc29\uc9c0\u03f5 = 1.0f-32loss(x,y) = crossentropy(m(x) .+ \u03f5,y) # onecold\uac00 GPU\uc5d0\uc11c\ub294 \uc5d0\ub7ec \ubc1c\uc0dd \ud558\uae30 \ub54c\ubb34\uc5d0 \uc544\ub798\ub85c \ub300\uccb4\ucc38\uc870 : https:\/\/github.com\/FluxML\/Flux.jl\/issues\/556 compare(y::OneHotMatrix, y\u2032) = maximum(y\u2032, dims = 1) .== maximum(y .* y\u2032, dims = 1)accuracy(x, y::OneHotMatrix) = mean(compare(y, m(x))) mnist.jl<\/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\/2546"}],"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=2546"}],"version-history":[{"count":11,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2546\/revisions"}],"predecessor-version":[{"id":2557,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2546\/revisions\/2557"}],"wp:attachment":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}