{"id":464,"date":"2018-02-05T23:46:58","date_gmt":"2018-02-05T14:46:58","guid":{"rendered":"http:\/\/www.ecobioinfo.com\/?p=464"},"modified":"2022-06-05T22:42:51","modified_gmt":"2022-06-05T13:42:51","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%99","status":"publish","type":"post","link":"https:\/\/www.ecobioinfo.com\/?p=464","title":{"rendered":"\u6df1\u5c64\u5b66\u7fd2 TensorFlow \u3092 MNIST \u3067\u8a66\u3059"},"content":{"rendered":"<p>MNIST \u3068\u306f\u624b\u66f8\u304d\u306e\u6570\u5b57\u306e\u30e2\u30ce\u30af\u30ed\u753b\u50cf\u30c7\u30fc\u30bf\u3067AI\u753b\u50cf\u8a8d\u8b58\u306e\u7df4\u7fd2\u7528\u306b\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\nMNIST \u306e Web \u30da\u30fc\u30b8\u3092\u898b\u308b\u3068\u3001\u5b66\u7fd2\u7528\u3067 60,000, \u30c6\u30b9\u30c8\u7528 10,000 \u306e\u624b\u66f8\u304d\u6587\u5b57\u30c7\u30fc\u30bf\u304c\u5229\u7528\u3067\u304d\u308b\u305d\u3046\u3067\u3059\u3002<br \/>\n\u753b\u50cf\uff11\u679a\u306f28&#215;28\u30d4\u30af\u30bb\u30eb\u3067\u3059\u3002<br \/>\nhttp:\/\/yann.lecun.com\/exdb\/mnist\/<\/p>\n<p>TFLearn \u306b\u306f MNIST \u95a2\u9023\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308b\u306e\u3067\u3001\u3053\u308c\u3092\u5229\u7528\u3057\u3066Python\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u3001\u753b\u50cf\u3092\u5e7e\u3064\u304b\u8868\u793a\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u2015\u2015\u2015 MNIST\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3068\u753b\u50cf\u8868\u793a\u306e\u30d7\u30ed\u30b0\u30e9\u30e0<br \/>\n## TensorFlow TFLearn \u3092\u30a4\u30f3\u30dd\u30fc\u30c8<br \/>\nimport tensorflow as tf<br \/>\nimport tflearn as tfl<br \/>\n## \u30b0\u30e9\u30d5\u63cf\u753b\u7b49\u306b\u4f7f\u308f\u308c\u308b\u30e9\u30a4\u30d6\u30e9\u30ea<br \/>\nfrom matplotlib import cm<br \/>\nfrom matplotlib import pyplot as plt<\/p>\n<p>## \u6700\u521d\u306e\uff15\u679a\u3092\u8868\u793a<br \/>\n## \u753b\u50cf\u306f\u30d4\u30af\u30bb\u30eb\u30c7\u30fc\u30bf\u306e\u914d\u5217\u3068\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<br \/>\n## trainX \u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u3001trainY \u6b63\u89e3\u30c7\u30fc\u30bf<br \/>\n## testX \u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u3001testY \u6b63\u89e3\u30c7\u30fc\u30bf<br \/>\ntrainX, trainY, testX, testY = tfl.datasets.mnist.load_data(&#8216;.\/data\/mnist\/&#8217;, one_hot=True)<br \/>\nprint(&#8216;\u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u6570&#8217;)<br \/>\nprint(len(trainX))<br \/>\nprint(&#8216;\u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u6570&#8217;)<br \/>\nprint(len(testX))<\/p>\n<p>## \u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u3001\u6b63\u89e3\u30c7\u30fc\u30bf\u306e\u5148\u982d\uff15\u500b\u3092\u8868\u793a\u3057\u3066\u307f\u308b<br \/>\ni=0<br \/>\nwhile i &lt; 5:<br \/>\nplt.imshow(trainX[i].reshape(28, 28), cmap=cm.gray_r,\u00a0\u00a0\u00a0\u00a0 interpolation=&#8217;nearest&#8217;)<br \/>\nplt.show()<br \/>\nprint(trainY[i])<br \/>\ni =\u00a0 i+ 1<\/p>\n<p>\u2015\u2015\u2015 \u7d50\u679c<br \/>\n\u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u6570<br \/>\n55000<br \/>\n\u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u6570<br \/>\n10000<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-465\" src=\"https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2018\/02\/MNIST_TRAIN-187x300.png\" alt=\"MNIST_TRAIN\" width=\"187\" height=\"300\" srcset=\"https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2018\/02\/MNIST_TRAIN-187x300.png 187w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2018\/02\/MNIST_TRAIN.png 371w\" sizes=\"auto, (max-width: 187px) 100vw, 187px\" \/><\/p>\n<p>\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb(\u4ee5\u4e0b\u7701\u7565)<\/p>\n<p>\u6b63\u89e3\u30c7\u30fc\u30bf\u306f\u30011 \u306e\u7acb\u3063\u3066\u3044\u308b\u914d\u5217\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff080\uff5e9\uff09\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\uff08\u4e00\u756a\u76ee\u306e\u300c\u30f2\u300d\u306e\u3088\u3046\u306a\u6587\u5b57\u306f\u300c\uff17\u300d\u3060\u305d\u3046\u3067\u3059\u3002\uff17\u306b\u306f\u898b\u3048\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3053\u306e\u3050\u3089\u3044\u304c\u89e3\u8aad\u53ef\u80fd\u306e\u5883\u754c\u3067\u3057\u3087\u3046\u304b\uff1f\uff09<br \/>\n\u307e\u305a\u3001\u3082\u3063\u3068\u306e\u7c21\u5358\u306a\u7df4\u7fd2\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u3057\u3066\u3001<br \/>\nTFLearn \u306e fully_connected() \u3092\u4f7f\u3063\u3066\u4e2d\u9593\u5c64\u304c\u5168\u7d50\u5408\u578b\u306e\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u4f5c\u3063\u3066\u5b66\u7fd2\u3055\u305b\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u2015\u2015\u2015 \u5168\u7d50\u5408\u578b\u306e\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af<br \/>\n## TensorFlow TFLearn \u3092\u30a4\u30f3\u30dd\u30fc\u30c8<br \/>\nimport tensorflow as tf<br \/>\nimport tflearn as tfl<br \/>\n## \u5b9f\u884c\u6642\u9593\u3092\u898b\u305f\u3044\u306e\u3067\u3001\u524d\u8ff0\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u6642\u523b\u8868\u793a\u7528\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3092\u8ffd\u52a0<br \/>\nimport datetime as dt<br \/>\n## \u7d50\u679c\u306e\u8a55\u4fa1\u3000\u6570\u5024\u8a08\u7b97\u7528<br \/>\nimport numpy as np<\/p>\n<p>## \u753b\u50cf\u306f\u30d4\u30af\u30bb\u30eb\u30c7\u30fc\u30bf\u306e\u914d\u5217\u3068\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<br \/>\ntrainX, trainY, testX, testY = tfl.datasets.mnist.load_data(&#8216;.\/data\/mnist\/&#8217;, one_hot=True)<\/p>\n<p>## \u4e2d\u9593\u5c64\u5168\u7d50\u5408\u578b\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af<br \/>\n## \u30ce\u30fc\u30c9 256 \u306e DNN\u3001 \u6d3b\u6027\u5316\u95a2\u6570 ReLU<br \/>\ntf.reset_default_graph()<\/p>\n<p>## \u5165\u529b\u5c64<br \/>\nnet = tfl.input_data(shape=[None, 784])<\/p>\n<p>## \u4e2d\u9593\u5c64<br \/>\nnet = tfl.fully_connected(net, 256, 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>## \u30c6\u30b9\u30c8(\u4e88\u6e2c)<br \/>\ntime3 = dt.datetime.now();<br \/>\nresult = model.predict(testX)<br \/>\ntime4 = dt.datetime.now();<\/p>\n<p>## \u5b9f\u884c\u6642\u9593\u3092\u8868\u793a<br \/>\nprint(&#8216;learning start:&#8217; + str(time1))<br \/>\nprint(&#8216;learning end:&#8217; + str(time2))<br \/>\nprint(&#8216;predict start:&#8217; + str(time3))<br \/>\nprint(&#8216;predict end:&#8217; + str(time4))<\/p>\n<p>## \u7d50\u679c\u3092\u8868\u793a<br \/>\npred = np.array(result).argmax(axis=1)<br \/>\nprint(pred)<br \/>\nlabel=testY.argmax(axis=1)<br \/>\nprint(label)<\/p>\n<p>## \u4e88\u6e2c\u306e\u7cbe\u5ea6<br \/>\naccuracy = np.mean(pred == label, axis=0)<br \/>\nprint(accuracy)<\/p>\n<p>\u2015\u2015\u2015 \u7d50\u679c<br \/>\nlearning start:2018-02-03 20:33:12.646430<br \/>\nlearning end:2018-02-03 20:37:24.713400<br \/>\npredict start:2018-02-03 20:37:24.713400<br \/>\npredict end:2018-02-03 20:37:25.031950<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n0.9827<\/p>\n<p>\u30c6\u30b9\u30c8\u306f\u300198% \u4ee5\u4e0a\u306e\u6b63\u89e3\u3067\u3059\u3002<br \/>\n\u5b66\u7fd2\u306e\u5b9f\u884c\u6642\u9593\u306f\u3001\u7d04\uff14\u5206\u3001\u4e88\u6e2c\u306e\u5b9f\u884c\u6642\u9593\u306f\uff11\u79d2\u4ee5\u4e0b\u3067\u3057\u305f\u3002<\/p>\n<p>\uff08\u30cf\u30fc\u30c9\u30b9\u30da\u30c3\u30af CPU Intel\u00a0 Core2Duo 2.8GHz \u30e1\u30e2\u30ea 6GByte, GPU\u306a\u3057\uff09<\/p>\n<p>\uff12\u56de\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u7d50\u679c\u306f\u6bce\u56de\u5909\u5316\u3057\u307e\u3059\u3002<br \/>\n\uff08\u521d\u671f\u5024\u304c\u30e9\u30f3\u30c0\u30e0\u306a\u306e\u3067\u540c\u3058\u3068\u306f\u306a\u308a\u307e\u305b\u3093\u3002\uff09<\/p>\n<p>learning start:2018-02-03 20:39:56.831873<br \/>\nlearning end:2018-02-03 20:44:12.236173<br \/>\npredict start:2018-02-03 20:44:12.236173<br \/>\npredict end:2018-02-03 20:44:12.530747<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n0.9778<\/p>\n<p>&nbsp;<\/p>\n<p>\u30ce\u30fc\u30c9\u3092 512\u306b\u3057\u3066\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u2015\u2015\u2015 \u7d50\u679c\uff08\u30ce\u30fc\u30c9512\uff09<br \/>\nlearning start:2018-02-03 20:23:34.402772<br \/>\nlearning end:2018-02-03 20:29:41.604538<br \/>\npredict start:2018-02-03 20:29:41.604538<br \/>\npredict end:2018-02-03 20:29:42.121952<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n0.9828<\/p>\n<p>\u5b66\u7fd2\u306e\u5b9f\u884c\u6642\u9593\u306f\u3001\u7d04\uff16\u5206\u3001\u4e88\u6e2c\u306e\u5b9f\u884c\u6642\u9593\u306f\uff11\u79d2\u4ee5\u4e0b\u3002<br \/>\n\u7cbe\u5ea6\u306f\u5c11\u3057\u3060\u3051\u4e0a\u304c\u3063\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff08\u5fae\u5999\u3067\u3059\u304c\uff09\u3002<\/p>\n<p>\u30ce\u30fc\u30c9\u3092 4096\u306b\u3057\u3066\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u2015\u2015\u2015 \u7d50\u679c\uff08\u30ce\u30fc\u30c94096\uff09<br \/>\nlearning start:2018-02-03 19:44:00.629881<br \/>\nlearning end:2018-02-03 20:18:30.696156<br \/>\npredict start:2018-02-03 20:18:30.696156<br \/>\npredict end:2018-02-03 20:18:33.324433<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n[7 2 1 &#8230; 4 5 6]<br \/>\n0.9835<\/p>\n<p>\u5b66\u7fd2\u306e\u5b9f\u884c\u6642\u9593\u306f\u3001\u7d04\uff13\uff15\u5206\u3001\u4e88\u6e2c\u306e\u5b9f\u884c\u6642\u9593\u306f\uff13\u79d2\u7a0b\u5ea6\u3002<br \/>\n\u6642\u9593\u304c\u304b\u304b\u3063\u305f\u5272\u306b\u306f\u52b9\u679c\u306f\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u3059\u3001\u4eca\u56de\u306f\u3053\u3053\u307e\u3067\u3002<\/p>\n<p>(S.Onda 2018\/2\/5)<br \/>\n(S.Onda 2018\/2\/17 \u4e00\u90e8\u4fee\u6b63)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"MNIST \u3068\u306f\u624b\u66f8\u304d\u306e\u6570\u5b57\u306e\u30e2\u30ce\u30af\u30ed\u753b\u50cf\u30c7\u30fc\u30bf\u3067AI\u753b\u50cf\u8a8d\u8b58\u306e\u7df4\u7fd2\u7528\u306b\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002 MNIST \u306e Web \u30da\u30fc\u30b8\u3092\u898b\u308b\u3068\u3001\u5b66\u7fd2\u7528\u3067 60,000, \u30c6\u30b9\u30c8\u7528 10,000 \u306e\u624b\u66f8\u304d\u6587\u5b57\u30c7\u30fc\u30bf\u304c\u5229\u7528\u3067\u304d\u308b\u305d\u3046\u3067 &#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-464","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\/464","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=464"}],"version-history":[{"count":9,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions\/1480"}],"wp:attachment":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}