{"id":1619,"date":"2021-04-03T00:04:46","date_gmt":"2021-04-02T15:04:46","guid":{"rendered":"https:\/\/tanico-kazuyo.net\/?p=1619"},"modified":"2021-04-03T00:11:18","modified_gmt":"2021-04-02T15:11:18","slug":"tpu_tensorflow_beginner","status":"publish","type":"post","link":"https:\/\/tanico-kazuyo.net\/?p=1619","title":{"rendered":"TPU\u3092\u4ed5\u7d44\u307f\u7406\u89e3\u304b\u3089TensorFlow\u3067\u5b9f\u88c5\u307e\u3067"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">TPU\u3063\u3066\u4f55\uff1f<\/h1>\n\n\n\n<p>\u898b\u3066\u3044\u308b\u3068\u6c17\u306b\u306a\u308b\u3053\u3068\u306f\u3044\u3063\u3071\u3044\u3042\u308b<br>\u4e0a\u3052\u308c\u3070\u30ad\u30ea\u304c\u306a\u3044\u3082\u306e\u3060\u304c\u3001\u6614\u306e\u30e1\u30e2\u3092\u3055\u30b0\u30c3\u305f\u3089TPU\u3068\u3044\u3046\u6587\u5b57\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u3053\u306e\u969b\u306b\u8abf\u3079\u3066\u304a\u3053\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tensor Processing Unit<\/h2>\n\n\n\n<p>\u6700\u3082\u4e8b\u7d30\u304b\u306b\u304b\u304b\u306a\u306e\u3067\u3001\u3042\u3057\u304b\u3089\u305a\u3002<br>\u4ed5\u7d44\u307f\u306e\u8aac\u660e\u306fGoogleCloud\u306e\u8aac\u660e\u304c\u3057\u3063\u304f\u308a\u304d\u305f\u3002<br>\u307e\u305a\u3001CPU\u30fbGPU\u306e\u5fa9\u7fd2<\/p>\n\n\n\n<ul><li>CPU<ul><li>ALU\u306b\u4f55\u3067\u3082\u3084\u3089\u305b\u308b\u304b\u3089\u67d4\u8edf\u6027\u306f\u9ad8\u3044\u3002<\/li><li>\u3067\u3082\u30ad\u30e3\u30c3\u30b7\u30e5\u8aad\u307f\u8fbc\u307f\u2192\u547d\u4ee4\u51e6\u7406\u2192\u66f8\u304d\u8fbc\u307f\u307e\u3067\u304c\u6642\u9593\u3068\u30a8\u30cd\u30eb\u30ae\u30fc\u306e\u7121\u99c4<\/li><li>\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u51e6\u7406\u3067\u9811\u5f35\u3063\u3066\u3082\u3001\u7d50\u5c40\u305f\u304b\u304c\u77e5\u308c\u3066\u308b<\/li><\/ul><\/li><li>GPU<ul><li>1\u500b\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u5927\u91cf\u306eALU\u3092\u5165\u308c\u305f\u3082\u306e\u3002\u5358\u7d14\u306a\u547d\u4ee4\u3092\u300c\u6570\u300d\u3067\u5012\u305b\u308b\u3002<\/li><li>\u6570\u306e\u5206\u3060\u3051\u9ad8\u901f\u5316\u3059\u308b\u304c\u3001\u96fb\u529b\u3092\u98df\u3046\u3002<\/li><li>CPU\u3068\u4ed5\u7d44\u307f\u306f\u540c\u3058\u306a\u306e\u3067\u3001\u4e2d\u9593\u3067\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u7d4c\u7531\u3059\u308b\u305f\u3081\u6642\u9593\u3068\u30a8\u30cd\u30eb\u30ae\u30fc\u306e\u7121\u99c4<ul><li>\u3053\u306e\u546a\u7e1b\u3092\u30ce\u30a4\u30de\u30f3\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3068\u3044\u3046\u3089\u3057\u3044\u3002\u3053\u306e\u540d\u306f\u521d\u3081\u3066\u77e5\u3063\u305f<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Google\u306f\u3046\u307e\u3044\u3053\u3068\u3092\u8003\u3048\u305f\u3002GPU\u30fbCPU\u306e\u6b20\u70b9\u3092\u514b\u670d\u3059\u308b\u306b\u306f\u3001\u69cb\u9020\u3092\u304b\u3048\u3088\u3046\u3068<br>\u30ad\u30e3\u30c3\u30b7\u30e5\u306b\u884c\u304f\u524d\u306b\u3069\u3093\u3069\u3093\u8db3\u3057\u3066\u3057\u307e\u3048\u3070\u3088\u3044\uff01<br><a href=\"https:\/\/cloud.google.com\/tpu\/docs\/beginners-guide#whats_next\">Google\u516c\u5f0f<\/a>\u306eGIF\u304c\u3088\u3044\u306d<\/p>\n\n\n\n<p>\u30c7\u30fc\u30bf\u3092\u5de6\u304b\u3089\u5217\u65b9\u5411\u306b\u5c55\u958b\u3057\u3066\u2192\u306b\u9032\u3081\u3066\u3044\u304f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-1.png\" alt=\"\" class=\"wp-image-1621\" width=\"512\" height=\"235\" srcset=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-1.png 661w, https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-1-300x138.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4e0b\u304b\u3089\u884c\u65b9\u5411\u306b\u5c55\u958b\u3057\u3066\u2191\u306b\u9032\u3081\u3066\u3044\u304f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"282\" src=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-2.png\" alt=\"\" class=\"wp-image-1622\" srcset=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-2.png 476w, https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-2-300x178.png 300w\" sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/figure>\n\n\n\n<p>NN\u3063\u3066\u3044\u3046\u306e\u306f\u5165\u529b\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u30921\u5bfe1(y=ax+b)\u3067\u3064\u306a\u3052\u308b\u3082\u306e\u306a\u306e\u3067\u3001<br>\u4e0a\uff12\u56f3\u306e\u3088\u3046\u306b\u6d41\u3057\u3066\u3044\u3051\u3070\u3001\u5426\u304c\u5fdc\u3067\u3082\u6f14\u7b97\u5668\u306b\u3076\u3061\u5f53\u305f\u308a\u8a08\u7b97\u305b\u3056\u308b\u3092\u5f97\u306a\u3044\u3002<br>\u51fa\u53e3\u3067\u3042\u308b\u53f3\u7aef\u3067\u306f\u6700\u7d42\u7684\u306b\u4e0b\u307f\u305f\u3044\u306a\u884c\u5217\u6f14\u7b97(matmul)\u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u306b\u306a\u308b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"100\" src=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-3.png\" alt=\"\" class=\"wp-image-1623\" srcset=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-3.png 637w, https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-3-300x47.png 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u611f\u52d5\u3057\u305f\u3002\u982d\u3044\u3044\u3002<\/h2>\n\n\n\n<p>\u3053\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u901a\u3055\u306a\u3044\u4f5c\u6226\u3092<strong>\u30b7\u30b9\u30c8\u30ea\u30c3\u30af\u30a2\u30ec\u30a4<\/strong>\u3063\u3066\u8a00\u3046\u3089\u3057\u3044\u3002<br>\u307e\u305f\u3001NN\u306e\u8a08\u7b97\u904e\u7a0b\u3067\u306f\u7cbe\u5ea6\u306f\u305d\u3053\u307e\u3067\u91cd\u8981\u3067\u306f\u306a\u3044\u3089\u3057\u304f\u3001<br>\u91cf\u5b50\u5316\u3092\u884c\u3063\u3066\u4e00\u822c\u7684\u306a32\/64bit\u6f14\u7b97\u304b\u30898bit\u6f14\u7b97\u306e\u4ed5\u7d44\u307f\u3092\u63a1\u7528\u3057\u305f\u305f\u3081\u3001\u5b9f\u8cea\uff14\u500d\u9ad8\u901f\u5316(=\u7701\u30a8\u30cd)<br>\u3053\u306e\u7cbe\u5ea6\u5468\u308a\u306e\u4e0b\u308a\u306f\u7d50\u69cb\u30a2\u30c4\u3044\u3089\u3057\u3044<br><a href=\"https:\/\/ascii.jp\/elem\/000\/004\/014\/4014066\/\">https:\/\/ascii.jp\/elem\/000\/004\/014\/4014066\/<\/a><\/p>\n\n\n\n<p>\u30e0\u30fc\u30a2\u306e\u6cd5\u5247\u3063\u3066\u546a\u7e1b\u306b\u805e\u3053\u3048\u3066\u304d\u305f&#8230;<\/p>\n\n\n\n<p>GPU\u3068CPU\u306e\u304a\u4e92\u3044\u3044\u3044\u3068\u3053\u308d\u304c\u3042\u308b\u3088\u3046\u306b\u3001TPU\u306b\u3082\u5411\u304d\u4e0d\u5411\u304d\u304c\u3042\u308b\u3002<br><a href=\"https:\/\/cloud.google.com\/tpu\/docs\/tpus?hl=ja\">Google<\/a>\u304b\u3089\u76f4\u63a5\u62dd\u501f\u3059\u308b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u30fbCPU<br>\u3000\u6700\u5927\u9650\u306e\u67d4\u8edf\u6027\u3092\u5fc5\u8981\u3068\u3059\u308b\u8fc5\u901f\u306a\u30d7\u30ed\u30c8\u30bf\u30a4\u30d4\u30f3\u30b0<br>\u3000\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u306b\u6642\u9593\u304c\u304b\u304b\u3089\u306a\u3044\u5358\u7d14\u306a\u30e2\u30c7\u30eb<br>\u3000\u5b9f\u969b\u306e\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u304c\u5c0f\u3055\u3044\u5c0f\u898f\u6a21\u306a\u30e2\u30c7\u30eb<br>\u3000C++ \u3067\u8a18\u8ff0\u3055\u308c\u305f\u30ab\u30b9\u30bf\u30e0 TensorFlow \u6f14\u7b97\u304c\u591a\u304f\u3092\u5360\u3081\u308b\u30e2\u30c7\u30eb<br>\u3000\u30db\u30b9\u30c8\u30b7\u30b9\u30c6\u30e0\u306e\u4f7f\u7528\u53ef\u80fd\u306a I\/O \u307e\u305f\u306f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u5e2f\u57df\u5e45\u306b\u3088\u3063\u3066\u5236\u9650\u304c\u8ab2\u305b\u3089\u308c\u308b\u30e2\u30c7\u30eb<br>\u30fbGPU<br>\u3000\u30bd\u30fc\u30b9\u304c\u5b58\u5728\u3057\u306a\u3044\u30e2\u30c7\u30eb\u307e\u305f\u306f\u30bd\u30fc\u30b9\u3092\u5909\u66f4\u3059\u308b\u306e\u304c\u7169\u96d1\u3059\u304e\u308b\u30e2\u30c7\u30eb<br>\u3000CPU \u4e0a\u3067\u5c11\u306a\u304f\u3068\u3082\u90e8\u5206\u7684\u306b\u5b9f\u884c\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u591a\u6570\u306e\u30ab\u30b9\u30bf\u30e0 TensorFlow \u6f14\u7b97\u3092\u4f7f\u7528\u3059\u308b\u30e2\u30c7\u30eb<br>\u3000Cloud TPU \u3067\u5229\u7528\u3067\u304d\u306a\u3044 TensorFlow \u6f14\u7b97\u3092\u4f7f\u7528\u3059\u308b\u30e2\u30c7\u30eb\uff08\u5229\u7528\u53ef\u80fd\u306a TensorFlow \u6f14\u7b97\u306e\u30ea\u30b9\u30c8\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\uff09<br>\u3000\u5b9f\u969b\u306e\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u304c\u5927\u304d\u3044\u4e2d\u301c\u5927\u898f\u6a21\u306a\u30e2\u30c7\u30eb<br>\u30fbTPU<br>\u3000\u884c\u5217\u8a08\u7b97\u304c\u591a\u304f\u3092\u5360\u3081\u308b\u30e2\u30c7\u30eb<br>\u3000\u30e1\u30a4\u30f3\u306e\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0 \u30eb\u30fc\u30d7\u5185\u306b\u30ab\u30b9\u30bf\u30e0 TensorFlow \u6f14\u7b97\u304c\u306a\u3044\u30e2\u30c7\u30eb<br>\u3000\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u306b\u6570\u9031\u9593\u307e\u305f\u306f\u6570\u304b\u6708\u304b\u304b\u308b\u30e2\u30c7\u30eb<br>\u3000\u5b9f\u969b\u306e\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u304c\u975e\u5e38\u306b\u5927\u304d\u3044\u975e\u5e38\u306b\u5927\u898f\u6a21\u306a\u30e2\u30c7\u30eb<\/p><cite>https:\/\/cloud.google.com\/tpu\/docs\/tpus?hl=ja<\/cite><\/blockquote>\n\n\n\n<p>GPU\u306f\u3042\u308b\u7a0b\u5ea6\u8907\u96d1\u306a\u8a08\u7b97\u3067\u3082\u3042\u308b\u7a0b\u5ea6\u5fdc\u7528(\u9069\u5fdc\uff1f)\u304c\u52b9\u3044\u305f\u304c\u3001TPU\u306f\u4e00\u5207\u305d\u308c\u304c\u52b9\u304b\u306a\u3044\u3002<br>\u305d\u306e\u4ee3\u308f\u308a\u3001\u5358\u7d14\u306a\u884c\u5217\u6f14\u7b97\u306e\u584a\u306a\u3089\u5fc5\u6bba\uff01\u3068\u3044\u3063\u305f\u6b21\u7b2c\u3060\u308d\u3046<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Colab\u3068\u304bKaggle\u3068\u304b\u3067\u306f\u4f7f\u3048\u308b\u306e\u3067\u3064\u304b\u3063\u3066\u307f\u305f<\/h2>\n\n\n\n<p>\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3059\u3002\u3068\u306f\u8a00\u3063\u3066\u3082\u4ed6\u4eba\u30b3\u30fc\u30c9\u3092\u62dd\u501f\u3059\u308b\u3002\u4e00\u90e8\u52a0\u3048\u3064\u3064\u3001\u3001<br>\uff11\u5e74\u524d\u306bkaggale\u3067TPU\u3064\u304b\u304a\u3046\u305c\uff01\u307f\u305f\u3044\u306a\u306e\u3092\u601d\u3044\u51fa\u3057\u305f\u306e\u3067\u3001\u305d\u308c\u3092\u984c\u6750\u306b\u3057\u307e\u3059\u3002<br><a href=\"https:\/\/www.kaggle.com\/c\/flower-classification-with-tpus\">\u304a\u82b1\u5206\u985e<\/a>\u3067\u3059\u306d<\/p>\n\n\n\n<p>\u3053\u3061\u3089\u306b\u66f8\u3044\u3066\u3042\u308bTPU\u306e\u4f7f\u3044\u65b9\u306f\u3068\u3066\u3082\u308f\u304b\u308a\u3084\u3059\u3044\u3068\u601d\u3044\u307e\u3059\uff08\u3042\u304f\u307e\u3067\u7406\u89e3\u306e\u3046\u3048\u3067\u306f\uff09<br>SAMPLE\u30b3\u30fc\u30c9\u306b\u3081\u3082\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<br><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"header\">import re\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport numpy as np\nfrom matplotlib import pyplot as plt\nprint(\"Tensorflow version \" + tf.__version__)\n\n# Prefetch\u7528\u306b\u3069\u306e\u7a0b\u5ea6\u5148\u53d6\u308a\u3057\u3066\u304a\u304f\u304b\u81ea\u52d5\u8abf\u6574\n#\u3000\u3053\u308c\u3082\u3044\u3064\u3082\u3069\u304a\u308a\u3067OK\n# https:\/\/tensorflow.classcat.com\/2019\/03\/23\/tf20-alpha-guide-data-performance\/\nAUTO = tf.data.experimental.AUTOTUNE\n# Kaggle\u306e\u30c7\u30fc\u30bf\u62dd\u501f\nfrom kaggle_datasets import KaggleDatasets\n\n<\/pre><\/div>\n\n\n\n<p>TPU\u3068GPU\u3092\u5207\u308a\u66ff\u3048\u308b\u90e8\u5206\u3002<br>\u5404TPU\u3092\u5236\u5fa1\u3059\u308bcluster_resolver\u3092\u4f7f\u3063\u3066TPU\u3092\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u63a2\u7d22\u3002<br>\u5404TPU\u9593\u306e\u540c\u671f\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3059\u308bstrategy\u306f\u5fc5\u9808\u3002strategy\u306f\u3042\u304f\u307e\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306a\u306e\u3067\u3001\u306e\u3061\u307b\u3069\u5b9a\u7fa9\u3059\u308b<br>TPUStrategy\u306e\u5834\u5408\u3001\u30a2\u30af\u30bb\u30e9\u30ec\u30fc\u30bf\u30fc\u3067\u5206\u6563\u3059\u308b\u969b\u306b\u5909\u6570\u306e\u30df\u30e9\u30fc\u30ea\u30f3\u30b0\u304c\u3055\u308c\u3001\u5206\u6563\u5b66\u7fd2\u306e\u91cd\u307f\u3092\u96c6\u7d04\u2192\u91cd\u307f\u3092\u66f4\u65b0(\u540c\u671f)\u2192\u5171\u6709\u3059\u308b\u3002\u3053\u306e\u4ed5\u7d44\u307f\u3092all-reduce\u3063\u3066\u3044\u3046\u3002<br>TPU\u306f\u4f7f\u3048\u306a\u3044\u5834\u5408\u306fGPU\u306e\u540c\u3058\u30df\u30e9\u30fc\u30ea\u30f3\u30b0\u6226\u7565\u3067\u3042\u308bMirroredStrategy\u3092\u3064\u304b\u3046<br>\u4ee5\u4e0b\u3001<a href=\"https:\/\/xtech.nikkei.com\/atcl\/nxt\/mag\/ne\/18\/00001\/00091\/\">\u65e5\u7d4c xTEC<\/a>H\u3092\u62dd\u501f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-5.png\" alt=\"\" class=\"wp-image-1629\" width=\"455\" height=\"166\" srcset=\"https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-5.png 508w, https:\/\/tanico-kazuyo.net\/wp-content\/uploads\/2021\/04\/image-5-300x109.png 300w\" sizes=\"(max-width: 455px) 100vw, 455px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"strategy\">try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect() # TPU detection\n    strategy = tf.distribute.TPUStrategy(tpu) \n    print(\"Detect: TPU\")\nexcept ValueError:\n     # detect GPUs\n    strategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines\n    #strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU(\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u5165\u308c\u305f\u969b\u306b)\n    #strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() # for clusters of multi-GPU machines\n    print(\"Detect: GPU\")\n\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)\n# tf.distribute.Strategy\u306f\u3001\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u3092\u8907\u6570\u306eGPU\u3001\u8907\u6570\u306e\u30de\u30b7\u30f3\u3001\u307e\u305f\u306fTPU\u306b\u5206\u6563\u3059\u308b\u305f\u3081\u306eTensorFlowAPI\n# https:\/\/tensorflow.classcat.com\/2019\/03\/21\/tf20-alpha-guide-distribute-strategy\/<\/pre><\/div>\n\n\n\n<p>\u307e\u305a\u306f\u30c7\u30fc\u30bf\u3092\u64ae\u3063\u3066\u304f\u308b\u3002<br>\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u304c\u5927\u4e8b\u3067\u3001\u3053\u3053\u306f\uff18\u306e\u500d\u6570(GoogleTPUv3.8\u306e\u5834\u54088bit\u91cf\u5b50\u5316\u3057\u3066\u3044\u308b\u306e\u3067)\u306b\u3057\u306a\u3044\u3068\u884c\u3051\u306a\u3044\u3002<br>\u3053\u308c\u306f\u5f8c\u3067\u4f7f\u3046Steps_per_execution\u3082\u540c\u3058\u3053\u3068\u304c\u8a00\u3048\u308b\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\"\nDefine image info\n\"\"\"\n# Dataset: https:\/\/www.kaggle.com\/mgornergoogle\/five-flowers\n# TFRecord \u30d5\u30a1\u30a4\u30eb\u7fa4\nEPOCHS = 12\nIMAGE_SIZE = [331, 331]\n\n# available image sizes\n# train\/test\/val\nFLOWERS_DATASETS = { \n    192: GCS_PATH + '\/tfrecords-jpeg-192x192\/*.tfrec',\n    224: GCS_PATH + '\/tfrecords-jpeg-224x224\/*.tfrec',\n    331: GCS_PATH + '\/tfrecords-jpeg-331x331\/*.tfrec',\n    512: GCS_PATH + '\/tfrecords-jpeg-512x512\/*.tfrec'\n}\n# Labels\nCLASSES = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']\nassert IMAGE_SIZE[0] == IMAGE_SIZE[1], \"only square images are supported\"\nassert IMAGE_SIZE[0] in FLOWERS_DATASETS, \"this image size is not supported\"\n\n# strategy.num_replicas_in_sync: #TPU  or #GPU: \u30ec\u30d7\u30ea\u30ab\u3092\u5404GPU\/TPU\u3067\u4f5c\u6210\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync <\/pre><\/div>\n\n\n\n<p>\u5b66\u7fd2\u7387\u3092\u9014\u4e2d\u3067\u5909\u3048\u308b\u3089\u3057\u3044\u3067\u3059\u3002TPU\u306b\u9650\u5b9a\u3057\u3066\u306a\u305c\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u306e\u304b\u306f\u4e0d\u660e\u3002<br>\u305f\u3060\u81ea\u5206\u81ea\u8eab\u3067\u5909\u66f4\u3059\u308b\u306e\u306f\u521d\u3081\u3066\u3002\u3088\u3044\u77e5\u898b\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"lr\">\"\"\"\nlearning rate scheduler\n\"\"\"\n\nLR_START = 0.00001\nLR_MAX = 0.00005 * strategy.num_replicas_in_sync\nLR_MIN = 0.00001\nLR_RAMPUP_EPOCHS = 5\nLR_SUSTAIN_EPOCHS = 0\nLR_EXP_DECAY = .8\n\ndef lrfn(epoch):\n    if epoch &lt; LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) \/ LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch &lt; LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        #\u3000\u4ee5\u964d\u6307\u6570\u7684\u306b\u6e1b\u8870\n        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr\n    \nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)\n\nrng = [i for i in range(EPOCHS)]\ny = [lrfn(x) for x in rng]\nplt.plot(rng, y)\nprint(\"Learning rate schedule: {:.3g} to {:.3g} to {:.3g}\".format(y[0], max(y), y[-1]))<\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u8fba\u306f\u753b\u50cf\u3092\u64ae\u3063\u3066\u304f\u308b\u3060\u3051\u3002TFRecord\u306a\u306e\u3067\u3001\u305d\u308c\u306b\u5fdc\u3058\u3066\u8aad\u307f\u8fbc\u3080\u3002<br>\u3053\u3053\u306f\u5168\u3066\u501f\u308a\u7269\u3002\u304a\u30d1\u30af\u308a\u3002<br>\u91cd\u8981\u306a\u70b9\u306f\u6700\u521d\u306b\u8a2d\u5b9a\u3057\u305f\u201dAUTO\u201d\u3002<br>TPU\u30fbGPU\u306fPrefetch\u304c\u901f\u5ea6\u306e\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u306a\u306e\u3067\u3053\u3053\u306f\u5927\u4e8b\u3002\u305d\u308c\u4ee5\u5916\u306f\u30c7\u30fc\u30bf\u3088\u308a\u3051\u308a\u3060\u3068\u601d\u3046<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"Handle_dataset\">\"\"\"\nData Loading Funcs\n\u5168\u3066\u501f\u308a\u7269\u95a2\u6570\n\"\"\"\n#TPU\u306f\u30b5\u30a4\u30ba\u306b\u6c17\u3092\u3064\u3051\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3089\u3057\u3044\n#https:\/\/cloud.google.com\/tpu\/docs\/performance-guide#tf-fcts\n# https:\/\/qiita.com\/ohtaman\/items\/325615358f008a4005e2\n\ndef count_data_items(filenames):\n    # the number of data items is written in the name of the .tfrec files, i.e. flowers00-230.tfrec = 230 data items\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\n        \ndef read_tfrecord(example):\n    features = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means scalar\n        \"one_hot_class\": tf.io.VarLenFeature(tf.float32),# Load variable length feature\n    }\n    example = tf.io.parse_single_example(example, features)\n    image = tf.image.decode_jpeg(example['image'], channels=3) # pixel format uint8 [0,255] range\n    class_label = tf.cast(example['class'], tf.int32) # not used\n    one_hot_class = tf.sparse.to_dense(example['one_hot_class'])\n    one_hot_class = tf.reshape(one_hot_class, [5])\n    return image, one_hot_class\n\n\ndef force_image_sizes(dataset, image_size):\n    # explicit size needed for TPU\n    reshape_images = lambda image, label: (tf.reshape(image, [*image_size, 3]), label)\n    dataset = dataset.map(reshape_images, num_parallel_calls=AUTO)\n    return dataset\n\ndef load_dataset(filenames):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\n    dataset = force_image_sizes(dataset, IMAGE_SIZE)\n    return dataset\n\ndef data_augment(image, one_hot_class):\n    # data augmentation. Thanks to the dataset.prefetch(AUTO) statement in the next function (below),\n    # this happens essentially for free on TPU. Data pipeline code is executed on the \"CPU\" part\n    # of the TPU while the TPU itself is computing gradients.\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_saturation(image, 0, 2)\n    return image, one_hot_class   \n\ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES)\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    dataset = dataset.repeat()\n    dataset = dataset.shuffle(2048)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef get_validation_dataset():\n    dataset = load_dataset(VALIDATION_FILENAMES)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset<\/pre><\/div>\n\n\n\n<p>\u3088\u3057\u306a\u306b\u5206\u5272\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"split_data\">\"\"\"\nSplit Data\n\"\"\"\ngcs_pattern = FLOWERS_DATASETS[IMAGE_SIZE[0]]\nvalidation_split = 0.19\nfilenames = tf.io.gfile.glob(gcs_pattern)\nsplit = len(filenames) - int(len(filenames) * validation_split)\nTRAINING_FILENAMES = filenames[:split]\nVALIDATION_FILENAMES = filenames[split:]\nTRAIN_STEPS = count_data_items(TRAINING_FILENAMES) \/\/ BATCH_SIZE\nVALIDATION_STEPS = -(-count_data_items(VALIDATION_FILENAMES) \/\/ BATCH_SIZE) # The \"-(-\/\/)\" trick rounds up instead of down :-)\nprint(\"TRAINING IMAGES: \", count_data_items(TRAINING_FILENAMES), \", STEPS PER EPOCH: \", TRAIN_STEPS)\nprint(\"VALIDATION IMAGES: \", count_data_items(VALIDATION_FILENAMES))<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u30e2\u30c7\u30eb\u4f5c\u6210\u3002<\/h3>\n\n\n\n<p>strategy.scope()\u3067\u304f\u304f\u308b\u3053\u3068\u3067\u3001\u5148\u7a0b\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u3088\u3046\u3084\u304f\u767b\u5834\u3002<br>\u3053\u306e\u4e2d\u306e\u51e6\u7406\u306e\u307f\u304cTPU\u3067\u5171\u6709\u3055\u308c\u308b\u3002\u3064\u307e\u308a\u3001\u30e2\u30c7\u30eb\u90e8\u5206\u3060\u3051\u304c\u5171\u6709\u3055\u308c\u308b\u3002<br>\u4eca\u56de\u306e\u4f8b\u3067\u306fSAMPLE\u306b\u5f93\u3063\u3066\u3001xception\u3092\u63a1\u7528\u3002\u4e0b\u8a18\u306e\u901a\u308a\u4ed6\u306e\u30e2\u30c7\u30eb\u3092\u4f7f\u3044\u305f\u3044\u306a\u3089\u30c6\u30ad\u30c8\u30a6\u306b\u5909\u66f4<br>steps_per_execution=8\u306f\u30011call\u3067\u306a\u3093\u30d0\u30c3\u30c1\u8aad\u307f\u8fbc\u3080\u304b\u3092\u5b9a\u7fa9\u3059\u308b\u3002\u4e0a\u306e\u201dAUTO\u201d\u3068\u540c\u3058\u3067\u9ad8\u901f\u5316\u306b\u8ca2\u732e<br>\u5358\u7d14\u306a\u30ae\u30e2\u30f3\u3068\u3057\u3066GPU\u3067\u3082\u540c\u3058\u3053\u3068\u8a00\u3048\u308b\u3088\u306d\uff1f\u3068\u601d\u3063\u305f\u304c\u3001GPU\u306f\u95a2\u4fc2\u306a\u3044\u3089\u3057\u3044\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"create_model\"># xception\u3092\u5229\u7528\n# https:\/\/note.nkmk.me\/python-tensorflow-keras-applications-pretrained-models\/\n#\u3000VGG\u3068\u304b\u3053\u306e\u90e8\u5206\u3092\u5909\u3048\u308c\u3070\u3088\u3044\u3002\n# \u30d9\u3064\u306b\u81ea\u4f5c\u3057\u3066\u3082\u3088\u3044\u3002\u666e\u901a\u306e\u753b\u50cf\u5206\u985e\nwith strategy.scope():\n    \"\"\"\n    Distributed Execution\n    \"\"\"\n    # xception\u7528\u306e\uff4e\u524d\u51e6\u7406\u5b9f\u884c\n    img_adjust_layer = tf.keras.layers.Lambda(\n        lambda data: tf.keras.applications.xception.preprocess_input(\n            tf.cast(data, tf.float32)), input_shape=[*IMAGE_SIZE, 3])\n    # xception\u30e2\u30c7\u30eb\u306e\u7d44\u307f\u8fbc\u307f\n    pretrained_model = tf.keras.applications.Xception(weights='imagenet', include_top=False)\n    #\u5b66\u7fd2\u30e2\u30fc\u30c9\n    pretrained_model.trainable = True\n\n    model = tf.keras.Sequential([\n        img_adjust_layer,\n        pretrained_model,\n        #  GAP&gt;Flat&gt;Dence\u3067\u7d42\u7aef\u51e6\u7406\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(5, activation='softmax')\n    ])\n\n# steps_per_execution: 1\u56de\u306erun\u3067\u4f55\u30d0\u30c3\u30c1\u3092\u547c\u3076\u304b\uff1f\n# TPU\u307e\u305f\u306f\u5c0f\u3055\u3044\u30e2\u30c7\u30eb\u306a\u3089\u52b9\u679c\u3042\u308a\uff0850%\u9ad8\u901f\u5316\uff09\u3002\n# GPU\u304c\u306a\u305c\u52b9\u679c\u5185\u306e\u304b\u4e0d\u660e\u3002\n#https:\/\/keras.io\/api\/models\/model_training_apis\/\n# https:\/\/qiita.com\/T-STAR\/items\/e2998d4c22c882039ffb\nmodel.compile(\n    optimizer='adam',\n    loss = 'categorical_crossentropy',\n    metrics=['accuracy'],\n\u3000\u3000steps_per_execution=8\n)\n\nmodel.summary()<\/pre><\/div>\n\n\n\n<p>\u5b66\u7fd2\u3002\u3082\u306f\u3084GPU\u306e\u3068\u304d\u3068\u5909\u308f\u3089\u306a\u3044\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"fit\">history = model.fit(get_training_dataset(), steps_per_epoch=TRAIN_STEPS, epochs=EPOCHS,\n                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS,\n                    callbacks=[lr_callback])\n\nfinal_accuracy = history.history[\"val_accuracy\"][-5:]\nprint(\"FINAL ACCURACY MEAN-5: \", np.mean(final_accuracy))<\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u3042\u3068\u4fdd\u5b58\u3068\u8aad\u307f\u8fbc\u307f\u304c\u3042\u308b\u304c\u3001GPU\u3068\u8aad\u307f\u8fbc\u307f\u65b9\u6cd5\u306f\u7570\u306a\u308b\u3082\u306e\u306e\u3001\u4f59\u308a\u672c\u8cea\u3067\u306f\u306a\u3044\u3093\u306e\u3067\u7701\u7565\u3002<br>\u3044\u307e\u306fTenswflow lite\u3068\u3044\u3046\u3082\u306e\u304c\u3042\u3063\u3066\u3001\u3044\u308f\u3086\u308b\u30a8\u30c3\u30b8\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u7528\u306b\u7279\u5316\u3055\u308c\u305f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u3042\u308b\u3051\u3069\u3001\u305d\u3053\u3067\u3082\u6d3b\u7528\u3067\u304d\u308b\u307f\u305f\u3044\u3002<br>\u96fb\u5b50\u5de5\u4f5c\u3067\u304a\u306a\u3058\u307f\u306eswitchScience\u304b\u3089\u51fa\u3066\u3044\u308b\u3002\u4eca\u5ea6\u904a\u3093\u3067\u307f\u305f\u3044\u30021\u4e07\u3059\u308b\u3051\u3069\uff57\uff57\uff57<br><a href=\"https:\/\/www.switch-science.com\/catalog\/5817\/\">https:\/\/www.switch-science.com\/catalog\/5817\/<\/a><\/p>\n\n\n\n<p>gmo\u304c\u306a\u3093\u304b\u3084\u3063\u3066\u3044\u305f\u3002<br><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-gmo\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8-\u6b21\u4e16\u4ee3\u30b7\u30b9\u30c6\u30e0\u7814\u7a76\u5ba4 wp-block-embed-gmo\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8-\u6b21\u4e16\u4ee3\u30b7\u30b9\u30c6\u30e0\u7814\u7a76\u5ba4\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/recruit.gmo.jp\/engineer\/jisedai\/blog\/edge_tpu_tf_lite_basics\/\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u611f\u60f3<\/h2>\n\n\n\n<ul><li>TPU\u30ca\u30cb\u305d\u308c\u3059\u3054\u305d\u3046\uff01\u3068\u3044\u3046\u5370\u8c61\u3060\u3063\u305f\u304c\u3001\u6848\u5916GPU\u3068\u5b9f\u88c5\u306e\u624b\u9593\u304c\u307b\u307c\u5909\u308f\u3089\u306a\u3044\u3002\u30af\u30e9\u30a6\u30c9\u306a\u3089\u304b\u3093\u305f\u3093\u306b\u4f7f\u3048\u305d\u3046<\/li><li>\u4e45\u3057\u3076\u308a\u306bCS\u30c1\u30c3\u30af\u306a\u8a00\u8449\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u8208\u596e\u3057\u305f\u3002\u5927\u5b66\u9662\u306e\u3068\u304d\u3088\u308a\u3082<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u88dc\u8db3<\/h2>\n\n\n\n<ul><li>\u8ab2\u91d1\u3068\u304b\u5b9f\u969b\u306eGCP\u3067\u306e\u5229\u7528\u306f\u4ee5\u4e0b\u304c\u4f7f\u3048\u305d\u3046<ul><li><a href=\"# https:\/\/www.apps-gcp.com\/machine-learning-tpu-v3-pod\/\">\u00a0https:\/\/www.apps-gcp.com\/machine-learning-tpu-v3-pod\/<\/a><\/li><\/ul><\/li><li>\u4eca\u56de\u306e\u30b3\u30fc\u30c9<ul><li><a href=\"https:\/\/github.com\/tanico-rikudo\/adhoc\/tree\/main\/tpu-beginner\">https:\/\/github.com\/tanico-rikudo\/adhoc\/tree\/main\/tpu-beginner<\/a><\/li><\/ul><\/li><\/ul>\n\n\n","protected":false},"excerpt":{"rendered":"<p>TPU\u3063\u3066\u4f55\uff1f \u898b\u3066\u3044\u308b\u3068\u6c17\u306b\u306a\u308b\u3053\u3068\u306f\u3044\u3063\u3071\u3044\u3042\u308b\u4e0a\u3052\u308c\u3070\u30ad\u30ea\u304c\u306a\u3044\u3082\u306e\u3060\u304c\u3001\u6614\u306e\u30e1\u30e2\u3092\u3055\u30b0\u30c3\u305f\u3089TPU\u3068\u3044\u3046\u6587\u5b57\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u3053\u306e\u969b\u306b\u8abf\u3079\u3066\u304a\u3053\u3046\u3002 Tensor Processing Unit \u6700\u3082\u4e8b\u7d30\u304b\u306b\u304b\u304b\u306a\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1620,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[42,41,40],"_links":{"self":[{"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/posts\/1619"}],"collection":[{"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1619"}],"version-history":[{"count":10,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/posts\/1619\/revisions"}],"predecessor-version":[{"id":1640,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/posts\/1619\/revisions\/1640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=\/wp\/v2\/media\/1620"}],"wp:attachment":[{"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tanico-kazuyo.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}