{"id":1916,"date":"2018-09-19T08:29:52","date_gmt":"2018-09-18T23:29:52","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=1916"},"modified":"2018-09-26T20:41:19","modified_gmt":"2018-09-26T11:41:19","slug":"blog103","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog103\/","title":{"rendered":"(103) TensorFlow #1 : \u306f\u3058\u3081\u3066\u306e TensorFlow"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u3068\u308a\u3042\u3048\u305a\u89e6\u308c\u3066\u307f\u308b\u3060\u3051\u3002<br \/>\n\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u516c\u5f0f\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff06\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u3084\u3063\u3066\u307f\u308b\u3002<br \/>\n<a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/001-2.png\" alt=\"\" \/><\/a><\/p>\n<h1 class=\"my_h\">2. \u74b0\u5883<\/h1>\n<p>TensorFlow\u306f\u3001\u73fe\u5728\u9032\u884c\u5f62\u3067\u65e5\u3005\u6539\u826f\u304c\u52a0\u3048\u3089\u308c\u3066\u3044\u308b\u3088\u3046\u3060\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u306f&#8230;<br \/>\n\u3042\u308b\u3068\u304d\u7a81\u7136\u4e0d\u5177\u5408\u306e\u3042\u308b\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u5165\u308c\u305f\u304c\u305f\u3081\u306b\u74b0\u5883\u304c\u58ca\u308c\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067&#8230;<br \/>\n\u4ee5\u4e0b\u306e\u4eee\u60f3\u30de\u30b7\u30f3\u4e0a\u306b\u5b9f\u9a13\u74b0\u5883\u3092\u4f5c\u308b\u3053\u3068\u306b\u3057\u305f\u3002<br \/>\n\u30fbVirtualBox : 5.2.18<br \/>\n\u30fbUbuntu : 18.04<br \/>\n<span class=\"my_fc_crimson\">\u74b0\u5883\u3092\u66f4\u65b0\u3059\u308b\u524d\u306b VM\u306e\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u3092\u8a18\u9332\u3057\u3066\u304a\u3051\u3070\u5b89\u5fc3\u3060\u3002<\/span><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/005-1.png\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">3. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<h2 class=\"my_h\">(1) \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/h2>\n<p>\u4eee\u60f3\u30de\u30b7\u30f3\u4e0a\u306b Ubuntu18\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001\u3053\u3053\u306b TensorFlow\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<br \/>\n\u3059\u3067\u306b Ubuntu\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u7d42\u308f\u3063\u305f\u72b6\u614b\u304b\u3089\u306e\u4f5c\u696d\u624b\u9806\u3092\u8a18\u9332\u3059\u308b\u3002<\/p>\n<h3 class=\"my_h\">1) \u5fc5\u8981\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u5165\u308c\u308b\u3002<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get update\r\nsudo apt-get install python-virtualenv python3-pip python3-dev python-launchpadlib\r\npip install --upgrade pip\r\n<\/pre>\n<h3 class=\"my_h\">2) TensorFlow\u5b9f\u9a13\u7528\u306b Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b\u3002<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmkdir ~\/tensorflow\r\ncd ~\/tensorflow\r\nvirtualenv --system-site-packages -p python3 venv\r\nsource ~\/tensorflow\/venv\/bin\/activate\r\npython -V    # Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u78ba\u8a8d\r\n<\/pre>\n<h3 class=\"my_h\">3) TensorFlow\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install --upgrade pip\r\n# pip install --upgrade tensorflow  # \u3053\u308c\u3060\u3068 import tensorflow \u3067\u30b3\u30a2\u30c0\u30f3\u30d7\u3059\u308b...\r\npip install tensorflow==1.5\r\n<\/pre>\n<p>Python\u304c\u52d5\u304f\u304b\u8a66\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport tensorflow as tf\r\nprint(tf.__version__)\r\n<\/pre>\n<p>\u30a8\u30e9\u30fc\u306a\u304f TensorFlow\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u8868\u793a\u3055\u308c\u308c\u3070OK<\/p>\n<pre>\r\n1.5.0\r\n<\/pre>\n<h2 class=\"my_h\">(2) \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u52d5\u304b\u3059\u3002<\/h2>\n<p>\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u5b9f\u884c\u3057\u3066\u307f\u308b\u3002<br \/>\n<a href=\"https:\/\/www.tensorflow.org\/tutorials\/\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/002-3.png\" alt=\"\" \/><\/a><\/p>\n<p>TensorFlow\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport tensorflow as tf\r\n<\/pre>\n<p>\u307e\u305a\u306f MNIST\u624b\u66f8\u304d\u6587\u5b57\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3057\u30010\uff5e255\u306e\u753b\u7d20\u5024\u3092 0\uff5e1\u306b\u6b63\u898f\u5316\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# MNIST\u624b\u66f8\u304d\u6587\u5b57\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\r\nmnist = tf.keras.datasets.mnist\r\n(x_train, y_train),(x_test, y_test) = mnist.load_data()\r\n\r\n# \u753b\u7d20\u5024\u3092 0\uff5e1 \u306b\u6b63\u898f\u5316\r\nx_train, x_test = x_train \/ 255.0, x_test \/ 255.0\r\n<\/pre>\n<p>\u30e2\u30c7\u30eb\u3092\u4f5c\u6210<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmodel = tf.keras.models.Sequential(&#x5B;\r\n  tf.keras.layers.Flatten(input_shape=(28, 28)),\r\n  tf.keras.layers.Dense(512, activation=tf.nn.relu),\r\n  tf.keras.layers.Dropout(0.2),\r\n  tf.keras.layers.Dense(10, activation=tf.nn.softmax)\r\n])\r\nmodel.compile(optimizer='adam',\r\n              loss='sparse_categorical_crossentropy',\r\n              metrics=&#x5B;'accuracy'])\r\n<\/pre>\n<p>\u5b66\u7fd2\u3092\u5b9f\u884c<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmodel.fit(x_train, y_train, epochs=5)\r\n<\/pre>\n<p>\u5b9f\u884c\u4e2d\u306e\u30ed\u30b0\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u51fa\u529b\u3055\u308c\u308b\u3002<br \/>\n5 epochs\u3067\u6b63\u89e3\u7387\uff08accuracy\uff09\u304c 96.3%\u304b\u3089 98.6%\u307e\u3067\u4e0a\u6607\u3057\u305f\u3002<br \/>\n1 epoch\u76ee\u3067 96.3%\u3068\u306f\u304b\u306a\u308a\u512a\u79c0\u306a\u30e2\u30c7\u30eb\u3060\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/003-2.png\" alt=\"\" \/><\/p>\n<p>\u30c6\u30b9\u30c8\u3092\u5b9f\u884c<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmodel.evaluate(x_test, y_test)\r\n<\/pre>\n<p>\u5b66\u7fd2\u306b\u4f7f\u3063\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3067\u9ad8\u6b63\u89e3\u7387\u306a\u306e\u306f\u5f53\u7136\u306a\u3053\u3068\u3002<br \/>\n\u5b66\u7fd2\u306b\u4f7f\u3063\u3066\u3044\u306a\u3044\u753b\u50cf\u30c7\u30fc\u30bf\u3067\u30c6\u30b9\u30c8\u3057\u305f\u7d50\u679c\u306f\uff1f<\/p>\n<p>\u306a\u3093\u3068 97.99% \u3068\u9ad8\u6b63\u89e3\u7387\uff01<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/004-2.png\" alt=\"\" \/><\/p>\n<p>\u3068\u3063\u3066\u3082\u512a\u79c0\u306a\u30e2\u30c7\u30eb\u3060\u306a\u3002<\/p>\n<h1 class=\"my_h\">4. \u304a\u307e\u3051<\/h1>\n<h2 class=\"my_h\">(1) MNIST\u753b\u50cf\u3092\u8997\u3044\u3066\u307f\u308b\u3002<\/h2>\n<p>\u307e\u305a\u306f MNIST\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport tensorflow as tf\r\nmnist = tf.keras.datasets.mnist\r\n(x_train, y_train),(x_test, y_test) = mnist.load_data()\r\n<\/pre>\n<p>\u3069\u3046\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\uff1f<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nx_train.shape\r\n<\/pre>\n<pre>\r\n(60000, 28, 28)\r\n<\/pre>\n<p>x_train[60000][28][28] \u306e\u4e09\u6b21\u5143\u914d\u5217\u3060\u3063\u305f\u3002<\/p>\n<p>\u3059\u306a\u308f\u3061&#8230;<br \/>\n\u5b66\u7fd2\u7528\u306e\u753b\u50cf\u30c7\u30fc\u30bf\u306f 28 x 28\u30d4\u30af\u30bb\u30eb\u30b5\u30a4\u30ba\u306e\u753b\u50cf\u304c 60000\u679a\u3042\u308b\u3002<\/p>\n<p>\u5148\u982d 1\u500b\uff08x_train[0]\uff09\u3092\u753b\u50cf\u5316\u3057\u3066\u898b\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom PIL import Image\r\nimg = Image.fromarray(x_train&#x5B;0])\r\nimg.show()\r\n<\/pre>\n<p>\u305f\u3076\u3093 5 \u3060\u308d\u3046\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/006.png\" alt=\"\" \/><\/p>\n<p>y_train\u306e\u65b9\u3082\u898b\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ny_train.shape\r\n<\/pre>\n<pre>\r\n(60000,)\r\n<\/pre>\n<p>y_train[60000] \u306e\u4e00\u6b21\u5143\u914d\u5217\u3060\u3063\u305f\u3002<\/p>\n<p>\u5148\u982d\u304b\u3089 3\u500b\u3060\u3051\u4e2d\u8eab\u3092\u898b\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ny_train&#x5B;0:3]\r\n<\/pre>\n<pre>\r\narray([5, 0, 4], dtype=uint8)\r\n<\/pre>\n<p>x_train\u306e\u5148\u982d\u306b\u306f 5 \u306e\u753b\u50cf\u304c\u5165\u3063\u3066\u3044\u305f\u3002<br \/>\ny_train\u306e\u65b9\u306b\u306f\u6b63\u89e3\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u308b\u3088\u3046\u3060\u3002<br \/>\n3\u500b\u76ee\u304c 4 \u304b\u3092\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimg = Image.fromarray(x_train&#x5B;2])\r\nimg.show()\r\n<\/pre>\n<p>\u305f\u3076\u3093 4 \u3060\u308d\u3046\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/007.png\" alt=\"\" \/><\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u5148\u982d\u304b\u3089 100\u500b\u3060\u3051 PIL\u3067 PNG\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3066\u307f\u305f\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport tensorflow as tf\r\nfrom PIL import Image\r\n\r\nmnist = tf.keras.datasets.mnist\r\n(x_train, y_train),(x_test, y_test) = mnist.load_data()\r\n\r\nfor i in range(0,100):\r\n    img = Image.fromarray(x_train&#x5B;i])\r\n    img.save(&quot;.\/mnist_%05d.png&quot; % i, 'PNG')\r\n<\/pre>\n<p>\u65e5\u672c\u4eba\u306f\u5b57\u304c\u30ad\u30ec\u30a4\u3068\u8a00\u308f\u308c\u308b\u306e\u304c\u4f55\u3068\u306a\u304f\u308f\u304b\u308b\u306a\u3041<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/008.png\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">5. \u5099\u5fd8\u9332<\/h1>\n<p>1) \u4eee\u60f3\u30de\u30b7\u30f3\u4e0a\u3067\u306e\u5b9f\u884c\u306f\u9045\u3044\u3002\u3044\u305a\u308c\u306f\u7269\u7406\u30b5\u30fc\u30d0\u30fc\u4e0a\u306b\u74b0\u5883\u3092\u4f5c\u308d\u3046\u3002<\/p>\n<p>2) \u4ee5\u4e0b\u306e\u8b66\u544a\u304c\u51fa\u305f\u3002SIMD\u547d\u4ee4\u3092\u6709\u52b9\u306b\u3059\u308b\u305f\u3081 TensorFlow\u3092\u30bd\u30fc\u30b9\u304b\u3089\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u4f5c\u3063\u305f\u65b9\u304c\u3088\u3044\u3002<\/p>\n<pre>\r\nYour CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2\r\n<\/pre>\n<p>3) \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u7269\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u3088\u3063\u3066\u306f\u6b63\u3057\u304f\u52d5\u304b\u306a\u3044\u5834\u5408\u304c\u3042\u308b\u307f\u305f\u3044\u3002<br \/>\n\u3000\u30b3\u30a2\u30c0\u30f3\u30d7\u3059\u308b\u5834\u5408\u3082\u3042\u3063\u305f\u3002\u767a\u5c55\u9014\u4e0a\u306e\u7269\u306a\u306e\u3067\u3053\u308c\u306f\u4ed5\u65b9\u304c\u306a\u3044\u304b&#8230;<br \/>\n\u3000\u4eca\u5f8c\u3001\u56de\u907f\u7b56\u3092\u8981\u691c\u8a0e\u3002<br \/>\n\u3000\u30fbTensorFlow\u306f\u30bd\u30fc\u30b9\u304b\u3089\u4f5c\u308b\u3002<br \/>\n\u3000\u30fb\u305d\u306e\u4ed6<\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 \u3068\u308a\u3042\u3048\u305a\u89e6\u308c\u3066\u307f\u308b\u3060\u3051\u3002 \u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u516c\u5f0f\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff06\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u3084\u3063\u3066\u307f\u308b\u3002 2. \u74b0\u5883 TensorFlow\u306f\u3001\u73fe\u5728\u9032\u884c\u5f62\u3067\u65e5\u3005\u6539\u826f\u304c\u52a0\u3048\u3089\u308c\u3066\u3044\u308b\u3088\u3046\u3060\u3002 \u3068\u3044\u3046\u3053\u3068\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog103\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1916","post","type-post","status-publish","format-standard","hentry","category-tensorflow"],"views":4207,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/comments?post=1916"}],"version-history":[{"count":17,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1916\/revisions"}],"predecessor-version":[{"id":1988,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1916\/revisions\/1988"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=1916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=1916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=1916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}