{"id":9534,"date":"2016-07-14T15:53:24","date_gmt":"2016-07-14T06:53:24","guid":{"rendered":"http:\/\/www.itblog.jp\/?p=9534"},"modified":"2016-07-14T15:53:24","modified_gmt":"2016-07-14T06:53:24","slug":"mnist-for-ml-beginners%e3%81%be%e3%81%a8%e3%82%81","status":"publish","type":"post","link":"https:\/\/www.itblog.jp\/?p=9534","title":{"rendered":"MNIST For ML Beginners\u307e\u3068\u3081"},"content":{"rendered":"<p>Tensorflow\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u3001\u3044\u308f\u3086\u308bHello World\u306b\u8a72\u5f53\u3059\u308bMNIST\u304c\u3044\u304d\u306a\u308a\u96e3\u3057\u304f\u3066\u8a70\u307e\u3063\u3066\u3057\u307e\u3063\u305f\u3002<\/p>\n<p>\u304c\u3001\u305d\u308c\u3068\u306a\u304f\u5206\u304b\u3063\u3066\u304d\u305f\u306e\u3067\u3069\u3046\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u304b\u3092\u307e\u3068\u3081\u3066\u307f\u308b\u3002<\/p>\n<h2>MNIST\u306e\u76ee\u7684<\/h2>\n<p>\u5165\u529b\u3057\u305f\u624b\u66f8\u304d\u6587\u5b57\u306e\u753b\u50cf\u306b\u5bfe\u3057\u3066\u30010\u301c10\u4f55\u306e\u6570\u5b57\u304b\u3092\u985e\u63a8\u3057\u3066\u51fa\u529b\u3059\u308b\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3059\u308b\u3002<\/p>\n<h2>\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u6d41\u308c<\/h2>\n<p>\/\/\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u3002\u753b\u50cf\u30c7\u30fc\u30bf\u306b\u306f\u8a13\u7df4\u7528\u306e\u753b\u50cfmnist.train.image\u3068\u3001\u4f55\u306e\u6570\u5b57\u304b\u3092\u8868\u30590\u301c10\u306e\u30e9\u30d9\u30ebmnist.train.labels\u304c\u30bb\u30c3\u30c8\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<p>from tensorflow.examples.tutorials.mnist import input_data<br \/>\nmnist = input_data.read_data_sets(&#8220;MNIST_data\/&#8221;, one_hot=True)<\/p>\n<p>import tensorflow as tf <\/p>\n<p>\/\/\u5165\u529b\u306e\u753b\u50cf\u300228\u00d728\u3067784\u306e\u914d\u5217\u3067\u73fe\u308c\u308b\u3002\u8907\u6570\u306e\u753b\u50cf\u5165\u529b\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u306b[None,784]\u3068\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\nx = tf.placeholder(tf.float32, [None, 784])<\/p>\n<p>\/\/\u753b\u50cf\u306e\u91cd\u307f\u3068\u30d0\u30a4\u30a2\u30b9\u3002\u3053\u306e\u90e8\u5206\u306f\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u304a\u3044\u3066\u53ef\u5909\u3067\u3042\u308a\u3001\u3053\u306e\uff12\u3064\u306e\u5909\u6570\u3092\u8a13\u7df4\u3057\u3066\u8abf\u6574\u3059\u308b\u3053\u3068\u304c\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u76ee\u7684\u3002<br \/>\nW = tf.Variable(tf.zeros([784, 10]))<br \/>\nb = tf.Variable(tf.zeros([10]))<\/p>\n<p>\/\/\u5165\u529b\u753b\u50cf\u304b\u3089\u63a8\u5b9a\u3055\u308c\u308b\u6570\u5b57\u306e\u5024\u3092\u8868\u3057\u3066\u3044\u308b\u3002<br \/>\ny = tf.nn.softmax(tf.matmul(x, W) + b)<\/p>\n<p>\/\/\u6b63\u89e3\u306e\u6570\u5b57\u30e9\u30d9\u30eb\u306e\u5165\u529b\u914d\u5217\u30020\u301c10<br \/>\ny_ = tf.placeholder(tf.float32, [None, 10])<\/p>\n<p>\/\/\u30af\u30ed\u30b9\u30a8\u30f3\u30c8\u30ed\u30d4\u30fc\u3002\u5206\u304b\u308a\u3065\u3089\u3044\u304c\u3001\u5165\u529b\u753b\u50cf\u7fa4\u3092\u30e2\u30c7\u30eb\u3092\u901a\u3057\u3066\u5f97\u3089\u308c\u305f\u63a8\u5b9a\u306e\u6570\u5b57\u7fa4\u3068\u3001\u6b63\u89e3\u306e\u30e9\u30d9\u30eb\u7fa4\u306e\u9055\u3044\u306e\u5ea6\u5408\u3044\u3092\u8868\u3057\u3066\u3044\u308b<br \/>\ncross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))<\/p>\n<p>\/\/\u6700\u6025\u964d\u4e0b\u6cd5\u306b\u3066\u3001\u4e0a\u306e\u30af\u30ed\u30b9\u30a8\u30f3\u30c8\u30ed\u30d4\u30fc\u3092\u6700\u5c0f\u5316\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u3044\u308b\u51e6\u7406\u30020.5\u306f\u5b66\u7fd2\u4fc2\u6570\u3002<br \/>\ntrain_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)<\/p>\n<p> \/\/\u30bb\u30c3\u30b7\u30e7\u30f3\u958b\u59cb\u3068\u521d\u671f\u5316\u51e6\u7406<br \/>\ninit = tf.initialize_all_variables()<br \/>\nsess = tf.Session()<br \/>\nsess.run(init)<\/p>\n<p>\/\/1000\u56de\u306e\u30eb\u30fc\u30d7\u3002<br \/>\nfor i in range(1000):<br \/>\n  \/\/55000\u3042\u308b\u5165\u529b\u753b\u50cf\u306e\u4e2d\u304b\u3089\u3001100\u500b\u306e\u753b\u50cf\u3068\u30e9\u30d9\u30eb\u3092\u30e9\u30f3\u30c0\u30e0\u3067\u53d6\u308a\u51fa\u3059<br \/>\n  batch_xs, batch_ys = mnist.train.next_batch(100)<br \/>\n  \/\/\u6700\u6025\u964d\u4e0b\u6cd5\u306b\u3066\u3001\u30af\u30ed\u30b9\u30a8\u30f3\u30c8\u30ed\u30d4\u30fc\u3092\u6700\u5c0f\u5316\u3059\u308b\u3079\u304f\u91cd\u307f\u3068\u30d0\u30a4\u30a2\u30b9\u3092\u8abf\u6574\u3059\u308b\u3002\u3053\u306e\u904e\u7a0b\u3092\u30e2\u30c7\u30eb\u306e\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u3068\u66f8\u304b\u308c\u3066\u3044\u308b\u3002<br \/>\n  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})<\/p>\n<p>\/\/\u753b\u50cf\u5165\u529b\u306b\u5bfe\u3057\u3066\u306e\u51fa\u529b\u306ey\u3068\u3001\u6b63\u89e3\u306ey_\u3092\u6bd4\u8f03\u3057\u3001\u7b49\u3057\u3051\u308c\u3070true\u3001\u9055\u3048\u3070false\u306e\u914d\u5217\u3092\u5b9a\u7fa9\u3059\u308b\u3002<br \/>\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))<\/p>\n<p>\/\/cast\u3059\u308b\u3053\u3068\u3067true\u21921\u3001false\u21920\u306e\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3001\u5e73\u5747\u5024\u3092\u6c42\u3081\u3066\u3044\u308b\u3002\u5168\u3066\u6b63\u89e3\u3067\u3042\u308c\u30701\u306b\u306a\u308b\u304c\u3001\u3053\u306e\u30b1\u30fc\u30b9\u3060\u30680.9\u4ee5\u4e0a\u306e\u6570\u5024\u3067\u51fa\u529b\u3055\u308c\u308b\u3002<br \/>\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))<\/p>\n<p>\/\/\u30c6\u30b9\u30c8\u7528\u753b\u50cf\u30bb\u30c3\u30c8\u3092\u5165\u529b\u3068\u3057\u3066\u3001\u8a13\u7df4\u3057\u305f\u30e2\u30c7\u30eb\u3092\u4f7f\u3063\u3066\u6b63\u89e3\u7387\u3092\u51fa\u529b\u3059\u308b\u3002<br \/>\nprint(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u6700\u521d\u898b\u305f\u6642\u306f\u5206\u304b\u308a\u3065\u3089\u304b\u3063\u305f\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30dd\u30a4\u30f3\u30c8\u3060\u3068\u7406\u89e3\u3057\u305f\u3002<\/p>\n<p>\uff11\u30fb\u624b\u66f8\u304d\u6570\u5b57\u306e\u753b\u50cf\u3092784\u306e\u9577\u3055\u306e\u914d\u5217\u306b\u898b\u7acb\u3066\u308b\u3002\u305d\u308c\u305e\u308c\u6b63\u89e3\u306e\u6570\u5b57\u3082\u30bb\u30c3\u30c8\u3068\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\n\uff12\u30fb\u591a\u304f\u306e\u8a13\u7df4\u7528\u306e\u753b\u50cf\u3092\u6e96\u5099\u3059\u308b\u3053\u3068\u3067\u3001\u6b63\u89e3\u306e\u6570\u5b57(0\u301c9\uff09\u3092\u305d\u308c\u305e\u308c\u8868\u3059784\u306e\u9577\u3055\u306e\u914d\u5217(\u3053\u306e\u4f8b\u3067\u306f\u300c\u91cd\u307f\u300d\u3068\u547c\u3070\u308c\u3066\u3044\u308b)\u3092\u8abf\u6574\u3059\u308b\u3002\u5165\u529b\u3057\u305f\u753b\u50cf\u306b\u4e00\u756a\u8fd1\u4f3c\u3057\u3066\u3044\u308b\u91cd\u307f\u306e\u914d\u5217\u306e\u6570\u5b57\u304c\u51fa\u529b\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002\uff08\u3053\u308c\u304c y=softmax(Wx+b)<br \/>\n\uff13\u30fb\u8907\u6570\u306e\u5165\u529b\u753b\u50cf\u3068\u6b63\u89e3\u306e\u753b\u50cf\u7fa4\u3092\u6e96\u5099\u3059\u308b\u3053\u3068\u3067\u3001\u6700\u6025\u964d\u4e0b\u6cd5\u3092\u4f7f\u3046\u3053\u3068\u3067\u30010\u301c9\u306e\u6570\u5b57\u3092\u8868\u3059\u91cd\u307f\u306e\u914d\u5217\u3092\u8abf\u6574\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3068\u306a\u308b\u3002\u591a\u304f\u306e\u8a13\u7df4\u7528\u306e\u753b\u50cf\u306b\u8a13\u7df4\u3055\u305b\u308b\u307b\u3069\u30e2\u30c7\u30eb\u306e\u6b63\u78ba\u3055\u304c\u4e0a\u304c\u3063\u3066\u3044\u304f\u3002<br \/>\n\uff14\u30fb\u6700\u5f8c\u306b\u30c6\u30b9\u30c8\u7528\u306e\u753b\u50cf\u3092\u4f7f\u3063\u3066\u51fa\u6765\u4e0a\u304c\u3063\u305f\u30e2\u30c7\u30eb\u306e\u7cbe\u5ea6\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tensorflow\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u3001\u3044\u308f\u3086\u308bHello World\u306b\u8a72\u5f53\u3059\u308bMNIST\u304c\u3044\u304d\u306a\u308a\u96e3\u3057\u304f\u3066\u8a70\u307e\u3063\u3066\u3057\u307e\u3063\u305f\u3002 \u304c\u3001\u305d\u308c\u3068\u306a\u304f\u5206\u304b\u3063\u3066\u304d\u305f\u306e\u3067\u3069\u3046\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u304b\u3092\u307e\u3068\u3081\u3066\u307f\u308b\u3002 MNIST\u306e\u76ee\u7684 \u5165\u529b\u3057\u305f\u624b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-9534","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts\/9534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9534"}],"version-history":[{"count":0,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts\/9534\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}