{"id":9515,"date":"2016-06-09T12:29:38","date_gmt":"2016-06-09T03:29:38","guid":{"rendered":"http:\/\/www.itblog.jp\/?p=9515"},"modified":"2016-06-09T12:31:16","modified_gmt":"2016-06-09T03:31:16","slug":"tensorflow%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%ef%bc%88mnist%e3%83%81%e3%83%a5%e3%83%bc%e3%83%88%e3%83%aa%e3%82%a2%e3%83%abmac%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.itblog.jp\/?p=9515","title":{"rendered":"Tensorflow\u306e\u4f7f\u3044\u65b9\uff08MNIST\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\/Mac\uff09"},"content":{"rendered":"<p>\u6700\u8fd1AI\u304c\u3044\u305f\u308b\u3068\u3053\u308d\u3067\u8a71\u984c\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001Google\u304c\u516c\u958b\u3057\u3066\u3044\u308b\u6a5f\u68b0\u5b66\u7fd2\u306e\u30c4\u30fc\u30eb\u300cTensorflow\u300d\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u30cd\u30c3\u30c8\u3067\u60c5\u5831\u3092\u691c\u7d22\u3057\u306a\u304c\u3089\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u8a18\u4e8b\u304c\u66f4\u65b0\u3055\u308c\u3066\u3044\u308b\u306e\u304b\u306a\u304b\u306a\u304b\u3046\u307e\u304f\u884c\u304b\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u3084\u3063\u3066\u307f\u3066\u4e0a\u624b\u304f\u3044\u3063\u305f\u3084\u308a\u65b9\u3092\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>Tensorflow\u306f\u3001\u8a00\u8a9e\u304cPython\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3001Python\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u4f7f\u7528\u3057\u3066\u3044\u308b\u74b0\u5883\u306fMac OSX\u3067\u3059\u3002<br \/>\nPython\u306f\uff12\u7cfb\u3067\u3059\u3002<br \/>\n<a href=\"https:\/\/www.tensorflow.org\/versions\/r0.9\/get_started\/os_setup.html#download-and-setup\">\u516c\u5f0f\u30b5\u30a4\u30c8\u306e\u89e3\u8aac\uff08\u82f1\u8a9e\uff09\u306f\u3053\u3061\u3089<\/a><\/p>\n<h3>Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u78ba\u8a8d<\/h3>\n<p>\u6700\u521d\u306b\u5ff5\u306e\u305f\u3081Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u78ba\u8a8d\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\n\u79c1\u306e\u5834\u5408\u306f\uff12\u7cfb\u3067\u3057\u305f\u306e\u3067\u3001\uff12\u7cfb\u3068\u3044\u3046\u3053\u3068\u3067\u9032\u3081\u3066\u3044\u307e\u3059\u3002<br \/>\n\u203b \uff13\u7cfb\u306e\u5834\u5408\u306f\u624b\u9806\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>$ python -V<br \/>\nPython 2.7.10<\/p>\n<h2>tensorflow\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>Virtualenv\u306fPython\u306e\u4eee\u60f3\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u3067\u3059\u3002<br \/>\n\u3053\u3061\u3089\u3092\u5c0e\u5165\u3057\u307e\u3059\u3002<br \/>\n\u203b Virtualenv\u4ee5\u5916\u306b\u3082Docker\u306fAnaconda\u3092\u4f7f\u3063\u305f\u5c0e\u5165\u65b9\u6cd5\u306a\u3069\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u304c\u3001\u672c\u8a18\u4e8b\u3067\u306f\u5272\u611b\u3057\u307e\u3059\u3002<\/p>\n<p>pip\u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>$ sudo easy_install pip \/\/pip\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n$ sudo pip install &#8211;upgrade virtualenv \/\/virtualenv\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<p>\u4eee\u60f3\u74b0\u5883\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>$ virtualenv &#8211;system-site-packages ~\/tensorflow<\/p>\n<p>\u4eee\u60f3\u74b0\u5883\u3092Activate\u3057\u307e\u3059\u3002<\/p>\n<p>$ source ~\/tensorflow\/bin\/activate  # bash\u3092\u4f7f\u3046\u5834\u5408<br \/>\n$ source ~\/tensorflow\/bin\/activate.csh  # csh\u3092\u4f7f\u3046\u5834\u5408<\/p>\n<p>\u4e0a\u8a18\u5b9f\u884c\u5f8c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30d7\u30ed\u30f3\u30d7\u30c8\u304c\u5207\u308a\u66ff\u308f\u308a\u307e\u3059\u3002<\/p>\n<p>(tensorflow)$<\/p>\n<p>\u4eee\u60f3\u74b0\u5883\u306btensorflow\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>(tensorflow)$ sudo pip install &#8211;upgrade https:\/\/storage.googleapis.com\/tensorflow\/mac\/tensorflow-0.9.0rc0-py2-none-any.whl<br \/>\n\u203b MacOSX\u3001Python2\u7cfb\u306e\u5834\u5408\u3067\u3059\u3002\u4ed6\u306eOS\u306e\u5834\u5408\u30d1\u30b9\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u203b \u901a\u5e38\u306e\u30d7\u30ed\u30f3\u30d7\u30c8\u306b\u623b\u3059\u5834\u5408\u306f\u4ee5\u4e0b\u3067\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n(tensorflow)$ deactivate<\/p>\n<p>\u4ee5\u4e0a\u3067tensorflow\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u5b8c\u4e86\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>tensorflow\u306e\u52d5\u4f5c\u30c6\u30b9\u30c8<\/h2>\n<p>tensorflow\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u4e0a\u624b\u304f\u3044\u3063\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<p><code>$ python<br \/>\n>>> import tensorflow as tf<br \/>\n>>> hello = tf.constant('Hello, TensorFlow!')<br \/>\n>>> sess = tf.Session()<br \/>\n>>> print(sess.run(hello))<br \/>\nHello, TensorFlow!<br \/>\n>>> a = tf.constant(10)<br \/>\n>>> b = tf.constant(32)<br \/>\n>>> print(sess.run(a + b))<br \/>\n42<\/code><\/p>\n<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067 python\u3000\u3092\u5b9f\u884c\u3059\u308b\u3068\u5bfe\u8a71\u5f62\u5f0f\u3067\u5b9f\u884c\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u4e0a\u8a18\u306e\u901a\u308a\u306b\u8a18\u8ff0\u3057\u3066\u307f\u3066\u30a8\u30e9\u30fc\u304c\u8d77\u304d\u305a\u3001\u5b9f\u884c\u7d50\u679c\u304c\u4e0a\u8a18\u306e\u901a\u308a\u306b\u8fd4\u3063\u3066\u304f\u308c\u3070\u554f\u984c\u306a\u304f\u52d5\u4f5c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>MNIST\u306e\u52d5\u4f5c\u30c6\u30b9\u30c8<\/h2>\n<p>MNIST\u3068\u3044\u3046\u306e\u306f\u753b\u50cf\u8a8d\u8b58\u306e\u6a5f\u68b0\u5b66\u7fd2\u306e\u7cbe\u5ea6\u3092\u8a66\u3059\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u901a\u5e38\u306e\u8a00\u8a9e\u306e\u300cHello World\u300d\u306b\u76f8\u5f53\u3059\u308b\u30ec\u30d9\u30eb\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3060\u305d\u3046\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.tensorflow.org\/versions\/r0.9\/tutorials\/mnist\/beginners\/index.html#mnist-for-ml-beginners\">\u516c\u5f0f\u30b5\u30a4\u30c8<\/a>\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u8aac\u660e\u306f\u8f09\u3063\u3066\u3044\u307e\u3059\u306e\u3067\u8a73\u3057\u3044\u89e3\u8aac\u306f\u305d\u3061\u3089\u3092\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n\u8a18\u4e8b\u306b\u65ad\u7247\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u63b2\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u305d\u308c\u3089\u3092\u5168\u3066\u63b2\u8f09\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u300cmnist.py\u300d\u306a\u3069\u9069\u5f53\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u540d\u3092\u3064\u3051\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >from tensorflow.examples.tutorials.mnist import input_data\r\nmnist = input_data.read_data_sets(\"MNIST_data\/\", one_hot=True)\r\n\r\nimport tensorflow as tf\r\n\r\nx = tf.placeholder(tf.float32, [None, 784])\r\nW = tf.Variable(tf.zeros([784, 10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n\r\ny = tf.nn.softmax(tf.matmul(x, W) + b)\r\n\r\ny_ = tf.placeholder(tf.float32, [None, 10])\r\n\r\ncross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))\r\n\r\ntrain_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\r\n\r\ninit = tf.initialize_all_variables()\r\n\r\nsess = tf.Session()\r\nsess.run(init)\r\n\r\nfor i in range(1000):\r\n  batch_xs, batch_ys = mnist.train.next_batch(100)\r\n  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\r\n\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\r\n\r\nprint(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7d50\u679c\u304c\u304b\u3048\u3063\u3066\u304d\u305f\u306e\u3067\u554f\u984c\u306a\u304f\u52d5\u4f5c\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u203b\u3000\u4e00\u5ea6\u76ee\u306e\u5b9f\u884c\u306f\u30c7\u30fc\u30bf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u304f\u308b\u306e\u3067\u5c11\u3057\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>$ python mnist.py<br \/>\nExtracting MNIST_data\/train-images-idx3-ubyte.gz<br \/>\nExtracting MNIST_data\/train-labels-idx1-ubyte.gz<br \/>\nExtracting MNIST_data\/t10k-images-idx3-ubyte.gz<br \/>\nExtracting MNIST_data\/t10k-labels-idx1-ubyte.gz<br \/>\n0.9187<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1AI\u304c\u3044\u305f\u308b\u3068\u3053\u308d\u3067\u8a71\u984c\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001Google\u304c\u516c\u958b\u3057\u3066\u3044\u308b\u6a5f\u68b0\u5b66\u7fd2\u306e\u30c4\u30fc\u30eb\u300cTensorflow\u300d\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u30cd\u30c3\u30c8\u3067\u60c5\u5831\u3092\u691c\u7d22\u3057\u306a\u304c\u3089\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u8a18\u4e8b\u304c\u66f4\u65b0\u3055\u308c\u3066\u3044\u308b\u306e\u304b\u306a\u304b\u306a\u304b\u3046\u307e\u304f\u884c\u304b\u306a [&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-9515","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts\/9515","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=9515"}],"version-history":[{"count":1,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts\/9515\/revisions"}],"predecessor-version":[{"id":9516,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=\/wp\/v2\/posts\/9515\/revisions\/9516"}],"wp:attachment":[{"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itblog.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}