{"id":471,"date":"2018-02-12T21:02:37","date_gmt":"2018-02-12T12:02:37","guid":{"rendered":"http:\/\/www.ecobioinfo.com\/?p=471"},"modified":"2022-06-05T22:43:58","modified_gmt":"2022-06-05T13:43:58","slug":"%e6%b7%b1%e5%b1%a4%e5%ad%a6%e7%bf%92-tensorflow-%e3%82%92-mnist-%e3%81%a7%e8%a9%a6%e3%81%992","status":"publish","type":"post","link":"https:\/\/www.ecobioinfo.com\/?p=471","title":{"rendered":"\u6df1\u5c64\u5b66\u7fd2 TensorFlow \u3092 MNIST \u3067\u8a66\u3059(2)"},"content":{"rendered":"<p>\uff08\u524d\u56de\u306e\u7d9a\u304d\u3067\u3059\uff09<\/p>\n<p>\u203btflearn\u306e\u53c2\u8003\u30da\u30fc\u30b8<br \/>\nhttp:\/\/tflearn.org\/getting_started\/<\/p>\n<p>&nbsp;<\/p>\n<p>\u524d\u56de\u306f\u5168\u7d50\u5408\u578b\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u7573\u307f\u8fbc\u307f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u7573\u307f\u8fbc\u307f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u4e3b\u306b\u753b\u50cf\u51e6\u7406\u306b\u5229\u7528\u3055\u308c\u308b\u306e\u3067\u3001\u753b\u50cf\u3092\u6271\u3046\u3053\u3068\u306e\u591a\u3044\u751f\u7269\u5b66\u95a2\u4fc2\u3067\u306e\u5fdc\u7528\u304c\u671f\u5f85\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u4e2d\u9593\u5c64\u3067\u7573\u307f\u8fbc\u307f\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u4ee5\u4e0b\u306f\u3001Python\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<p>\u2015\u2015\u2015 \u7573\u307f\u8fbc\u307f\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af(CNN)\u306e\u30bd\u30fc\u30b9<\/p>\n<p>## \u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u9069\u5f53\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n## \u5b66\u7fd2\u6642\u9593\u304c\u9577\u3044\u306e\u3067\u5b66\u7fd2\u7d50\u679c\u3092\u518d\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u4fdd\u5b58\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002<br \/>\nimport numpy as np<br \/>\nimport datetime as dt<br \/>\nimport tensorflow as tf<br \/>\nimport tflearn as tfl<\/p>\n<p>## \u30c7\u30fc\u30bf\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<br \/>\ntrainX, trainY, testX, testY = tfl.datasets.mnist.load_data(&#8216;.\/data\/mnist\/&#8217;, one_hot=True)<br \/>\ntrainX = trainX.reshape([-1, 28, 28, 1])<br \/>\ntestX = testX.reshape([-1, 28, 28, 1])<\/p>\n<p>## \u521d\u671f\u51e6\u7406 28&#215;28\u306e\u30e2\u30ce\u30af\u30ed\u753b\u50cf<br \/>\ntf.reset_default_graph()<br \/>\nnet = tfl.input_data(shape=[None, 28, 28, 1])<\/p>\n<p>## \u4e2d\u9593\u5c64\uff11<br \/>\n## \u7573\u307f\u8fbc\u307f\u5c64\u3000\u30d5\u30a3\u30eb\u30bf\u657034 \u30b5\u30a4\u30ba7 \u6d3b\u6027\u5316 ReLU<br \/>\n## \u30bc\u30ed\u30d1\u30c7\u30a3\u30f3\u30b0\u3001\u30b9\u30e9\u30a4\u30c9\uff11<br \/>\nnet = tfl.layers.conv.conv_2d(net, 34, 7, activation=&#8217;relu&#8217;)<br \/>\n##\u30d7\u30fc\u30ea\u30f3\u30b0\u5c64\u3000\u30d7\u30fc\u30ea\u30f3\u30b0\u9818\u57df3<br \/>\nnet = tfl.layers.conv.max_pool_2d(net, 3)<\/p>\n<p>## \u5168\u7d50\u5408<br \/>\nnet = tfl.fully_connected(net, 68, activation=&#8217;relu&#8217;)<br \/>\nnet = tfl.dropout(net, 0.5)<\/p>\n<p>## \u51fa\u529b\u5c64<br \/>\nnet = tfl.fully_connected(net, 10, activation=&#8217;softmax&#8217;)<br \/>\nnet = tfl.regression(net, optimizer=&#8217;sgd&#8217;, learning_rate=0.5, loss=&#8217;categorical_crossentropy&#8217;)<\/p>\n<p>## \u5b66\u7fd2<br \/>\ntime1 = dt.datetime.now();<br \/>\nmodel = tfl.DNN(net)<br \/>\nmodel.fit(trainX, trainY, n_epoch=20, batch_size=100, validation_set=0.1, show_metric=True)<br \/>\ntime2 = dt.datetime.now();<\/p>\n<p>## \u5b66\u7fd2\u7d50\u679c\u3092\u4fdd\u5b58<br \/>\nsavepath = &#8216;.\/data\/mnist\/model1.ckpt&#8217;<br \/>\nmodel.save(savepath)<br \/>\nprint(&#8216;\u7d50\u679c\u4fdd\u5b58&#8217;)<br \/>\nprint(savepath)<\/p>\n<p>## \u30c6\u30b9\u30c8<br \/>\ntime3 = dt.datetime.now();<br \/>\nresult = model.predict(testX)<br \/>\ntime4 = dt.datetime.now();<\/p>\n<p>## \u7d50\u679c\u8868\u793a<br \/>\nprint(&#8216;\u5b66\u7fd2\u6642\u9593&#8217;)<br \/>\nprint(&#8216;start:&#8217; + str(time1))<br \/>\nprint(&#8216;end:&#8217; + str(time2))<br \/>\nprint(&#8216;\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593&#8217;)<br \/>\nprint(&#8216;start&#8217; + str(time3))<br \/>\nprint(&#8216;end&#8217; + str(time4))<\/p>\n<p>pred = np.array(result).argmax(axis=1)<br \/>\nlabel=testY.argmax(axis=1)<\/p>\n<p>print(&#8216;\u7cbe\u5ea6&#8217;)<br \/>\naccuracy = np.mean(pred == label, axis=0)<br \/>\nprint(accuracy)<\/p>\n<p>\u2015\u2015\u2015 \u5b9f\u884c\u7d50\u679c<\/p>\n<p>\u7d50\u679c\u4fdd\u5b58<br \/>\n.\/data\/mnist\/model1.ckpt<br \/>\n\u5b66\u7fd2\u6642\u9593<br \/>\nstart:2018-02-12 14:35:23.470289<br \/>\nend:2018-02-12 15:29:51.263895<br \/>\n\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593<br \/>\nstart2018-02-12 15:29:51.421936<br \/>\nend2018-02-12 15:30:01.542453<br \/>\n\u7cbe\u5ea6<br \/>\n0.9888<\/p>\n<p>\u5168\u7d50\u5408\u578b\u3088\u308a\u3082\u3060\u3044\u3076\u5b66\u7fd2\u6642\u9593\u304c\u304b\u304b\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u5236\u5ea6\u306f\u5c11\u3057\u826f\u3044\u7d50\u679c\u304c\u3067\u307e\u3057\u305f\u3002<br \/>\n\u6b21\u306f\u3001\u4fdd\u5b58\u3055\u308c\u305f\u5b66\u7fd2\u7d50\u679c\u3092\u8aad\u307f\u51fa\u3057\u3067\u5229\u7528\u3059\u308b\u30bd\u30fc\u30b9\u3067\u3059\u3002<\/p>\n<p>\u2015\u2015\u2015 \u4fdd\u5b58\u3055\u308c\u305f\u5b66\u7fd2\u7d50\u679c\u3092\u8aad\u307f\u51fa\u3057\u3066\u5b9f\u884c\u3059\u308b\u30bd\u30fc\u30b9<\/p>\n<p>import numpy as np<br \/>\nimport datetime as dt<br \/>\nimport tensorflow as tf<br \/>\nimport tflearn as tfl<\/p>\n<p>## \u30c7\u30fc\u30bf\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\uff08\u30c6\u30b9\u30c8\u7528\uff09<br \/>\ntrainX, trainY, testX, testY = tfl.datasets.mnist.load_data(&#8216;.\/data\/mnist\/&#8217;, one_hot=True)<br \/>\ntrainX = trainX.reshape([-1, 28, 28, 1])<br \/>\ntestX = testX.reshape([-1, 28, 28, 1])<\/p>\n<p>## \u521d\u671f\u51e6\u7406 28&#215;28\u306e\u30e2\u30ce\u30af\u30ed\u753b\u50cf<br \/>\ntf.reset_default_graph()<br \/>\nnet = tfl.input_data(shape=[None, 28, 28, 1])<\/p>\n<p>##\u3000\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u5b9a\u7fa9<br \/>\n## \u4e2d\u9593\u5c64\uff11<br \/>\n## \u7573\u307f\u8fbc\u307f\u5c64\u3000\u30d5\u30a3\u30eb\u30bf\u657034 \u30b5\u30a4\u30ba7 \u6d3b\u6027\u5316 ReLU<br \/>\n## \u30bc\u30ed\u30d1\u30c7\u30a3\u30f3\u30b0\u3001\u30b9\u30e9\u30a4\u30c9\uff11<br \/>\nnet = tfl.layers.conv.conv_2d(net, 34, 7, activation=&#8217;relu&#8217;)<br \/>\n##\u30d7\u30fc\u30ea\u30f3\u30b0\u5c64\u3000\u30d7\u30fc\u30ea\u30f3\u30b0\u9818\u57df3<br \/>\nnet = tfl.layers.conv.max_pool_2d(net, 3)<\/p>\n<p>## \u5168\u7d50\u5408<br \/>\nnet = tfl.fully_connected(net, 68, activation=&#8217;relu&#8217;)<br \/>\nnet = tfl.dropout(net, 0.5)<\/p>\n<p>## \u51fa\u529b\u5c64<br \/>\nnet = tfl.fully_connected(net, 10, activation=&#8217;softmax&#8217;)<br \/>\nnet = tfl.regression(net, optimizer=&#8217;sgd&#8217;, learning_rate=0.5, loss=&#8217;categorical_crossentropy&#8217;)<\/p>\n<p>## \u5b66\u7fd2\u7d50\u679c\u3092\u8aad\u307f\u8fbc\u307f<br \/>\nmodel = tfl.DNN(net)<br \/>\nloadpath = &#8216;.\/data\/mnist\/model1.ckpt&#8217;<br \/>\nmodel.load(loadpath)<br \/>\nprint(&#8216;\u5b66\u7fd2\u7d50\u679c\u306e\u8aad\u307f\u8fbc\u307f&#8217;)<br \/>\nprint(loadpath)<\/p>\n<p>## \u30c6\u30b9\u30c8<br \/>\ntime3 = dt.datetime.now();<br \/>\nresult = model.predict(testX)<br \/>\ntime4 = dt.datetime.now();<\/p>\n<p>## \u7d50\u679c\u8868\u793a<br \/>\nprint(&#8216;\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593&#8217;)<br \/>\nprint(&#8216;start&#8217; + str(time3))<br \/>\nprint(&#8216;end&#8217; + str(time4))<\/p>\n<p>pred = np.array(result).argmax(axis=1)<br \/>\nlabel=testY.argmax(axis=1)<\/p>\n<p>print(&#8216;\u7cbe\u5ea6&#8217;)<br \/>\naccuracy = np.mean(pred == label, axis=0)<br \/>\nprint(accuracy)<\/p>\n<p>\u2015\u2015\u2015 \u5b9f\u884c\u7d50\u679c<br \/>\n\u5b66\u7fd2\u7d50\u679c\u306e\u8aad\u307f\u8fbc\u307f<br \/>\n.\/data\/mnist\/model1.ckpt<br \/>\n\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593<br \/>\nstart2018-02-12 18:27:53.449303<br \/>\nend2018-02-12 18:28:01.692120<br \/>\n\u7cbe\u5ea6<br \/>\n0.9888<\/p>\n<p>\u3082\u3046\u5c11\u3057\u5236\u5ea6\u3092\u4e0a\u3052\u308b\u305f\u3081\u306b\u4e2d\u9593\u5c64\u3092\uff12\u5c64\u306b\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u69cb\u6210\u3067\uff12\u5c64\u76ee\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u2015\u2015\u2015 \u7573\u307f\u8fbc\uff12\u5c64\u306e\u30bd\u30fc\u30b9\uff08\u4e00\u90e8\u629c\u7c8b\uff09<\/p>\n<p>## \u4e2d\u9593\u5c64\uff11<br \/>\n## \u7573\u307f\u8fbc\u307f\u5c64\u3000\u30d5\u30a3\u30eb\u30bf\u657034 \u30b5\u30a4\u30ba7 \u6d3b\u6027\u5316 ReLU<br \/>\n## \u30bc\u30ed\u30d1\u30c7\u30a3\u30f3\u30b0\u3001\u30b9\u30e9\u30a4\u30c9\uff11<br \/>\nnet = tfl.layers.conv.conv_2d(net, 34, 7, activation=&#8217;relu&#8217;)<br \/>\n##\u30d7\u30fc\u30ea\u30f3\u30b0\u5c64\u3000\u30d7\u30fc\u30ea\u30f3\u30b0\u9818\u57df3<br \/>\nnet = tfl.layers.conv.max_pool_2d(net, 3)<\/p>\n<p>## \u4e2d\u9593\u5c642<br \/>\n## \u7573\u307f\u8fbc\u307f\u5c64\u3000\u30d5\u30a3\u30eb\u30bf\u657068 \u30b5\u30a4\u30ba 7 \u6d3b\u6027\u5316 ReLU<br \/>\n## \u30bc\u30ed\u30d1\u30c7\u30a3\u30f3\u30b0\u3001\u30b9\u30e9\u30a4\u30c9\uff11<br \/>\nnet = tfl.layers.conv.conv_2d(net, 68, 7, activation=&#8217;relu&#8217;)<br \/>\n##\u30d7\u30fc\u30ea\u30f3\u30b0\u5c64\u3000\u30d7\u30fc\u30ea\u30f3\u30b0\u9818\u57df3<br \/>\nnet = tfl.layers.conv.max_pool_2d(net, 3)<\/p>\n<p>## \u5168\u7d50\u5408<br \/>\nnet = tfl.fully_connected(net, 136, activation=&#8217;relu&#8217;)<br \/>\nnet = tfl.dropout(net, 0.5)<\/p>\n<p>\u2015\u2015\u2015 \u5b9f\u884c\u7d50\u679c<br \/>\n\u5b66\u7fd2\u6642\u9593<br \/>\nstart:2018-02-12 15:39:55.144169<br \/>\nend:2018-02-12 17:53:44.195134<br \/>\n\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593<br \/>\nstart2018-02-12 17:53:44.326100<br \/>\nend2018-02-12 17:54:05.020110<br \/>\n\u7cbe\u5ea6<br \/>\n0.9937<\/p>\n<p>\uff12\u5c64\u306b\u3057\u3066\u5b66\u7fd2\u6642\u9593\u304c\uff12\u6642\u9593\u4ee5\u4e0a\u304b\u304b\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u7cbe\u5ea6\u304c\u4e0a\u304c\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u7d50\u679c\u3067\u306f\uff11\uff10\uff10\uff10\u6587\u5b57\u4e2d\uff16\u6587\u5b57\u7a0b\u5ea6\u306e\u30df\u30b9\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u304c\u3001<br \/>\n\u4eba\u9593\u304c\u76ee\u3067\u898b\u3066\u3082\u3053\u306e\u7a0b\u5ea6\u306e\u30df\u30b9\u306f\u3042\u308a\u305d\u3046\u306a\u306e\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306f\u3053\u306e\u7a0b\u5ea6\u3067\u826f\u3057\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u7df4\u7fd2\u7528\u3067\u3057\u305f\u304c\u3001\u5b9f\u7528\u7684\u306a\u5229\u7528\u3067\u306f\u76ee\u7684\u306b\u5fdc\u3058\u3066\u624b\u6cd5\u3092\u9078\u3076\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u30bf\u30d6\u30ec\u30c3\u30c8\uff30\uff23\u306e\u624b\u66f8\u304d\u5165\u529b\u3067\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u901f\u5ea6\u3092\u91cd\u8996\u3057\u3066\u591a\u5c11\u306e\u7cbe\u5ea6\u306f\u72a0\u7272\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u7533\u3057\u8fbc\u307f\u66f8\u985e\u306e\u624b\u66f8\u304d\u6587\u5b57\u3092\u3001\u30b5\u30fc\u30d0\u3067\u4e00\u62ec\u51e6\u7406\u3057\u3066\u5f8c\u65e5\u8fd4\u4fe1\u3059\u308b\u5834\u5408\u306f\u3001\u901f\u5ea6\u3088\u308a\u3082\u7cbe\u5ea6\u3092\u4e0a\u3052\u308b\u65b9\u304c\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u7528\u9014\u306b\u5fdc\u3058\u305f\u4f7f\u3044\u5206\u3051\u8003\u3048\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>(S.Onda 2018\/2\/12)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"\uff08\u524d\u56de\u306e\u7d9a\u304d\u3067\u3059\uff09 \u203btflearn\u306e\u53c2\u8003\u30da\u30fc\u30b8 http:\/\/tflearn.org\/getting_started\/ &nbsp; \u524d\u56de\u306f\u5168\u7d50\u5408\u578b\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u7573\u307f\u8fbc\u307f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u8a66\u3057\u3066\u307f\u307e &#8230;","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-471","post","type-post","status-publish","format-standard","hentry","category-ai_ecobio"],"_links":{"self":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=471"}],"version-history":[{"count":4,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/471\/revisions\/1481"}],"wp:attachment":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}