{"id":4843,"date":"2022-04-16T00:35:22","date_gmt":"2022-04-15T15:35:22","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=4843"},"modified":"2022-04-16T00:35:22","modified_gmt":"2022-04-15T15:35:22","slug":"%e3%80%90tensorflow%e3%80%91preprocessing-timeseries_dataset_from_array%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=4843","title":{"rendered":"\u3010TensorFlow\u3011preprocessing.timeseries_dataset_from_array\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=4843\/#%E6%A6%82%E8%A6%81\" >\u6982\u8981<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=4843\/#%E4%BD%BF%E3%81%84%E3%81%A9%E3%81%93%E3%82%8D\" >\u4f7f\u3044\u3069\u3053\u308d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=4843\/#%E6%A4%9C%E8%A8%BC%E7%92%B0%E5%A2%83\" >\u691c\u8a3c\u74b0\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=4843\/#%E5%AE%9F%E4%BE%8B\" >\u5b9f\u4f8b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/obenkyolab.com\/?p=4843\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>keras\u306e\u6642\u7cfb\u5217\u4e88\u6e2c\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff08<a href=\"https:\/\/keras.io\/examples\/timeseries\/timeseries_weather_forecasting\/\">Timeseries forecasting for weather prediction (keras.io)<\/a>\uff09\u3092\u898b\u3066\u3044\u305f\u3068\u3053\u308d\u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u969b\u306btf.keras.utils.timeseries_dataset_from_array\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u4f7f\u308f\u308c\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u5f15\u6570\u304c\u3044\u308d\u3044\u308d\u3042\u308b\u306e\u3067\u6700\u521d\u306b\u4f7f\u3044\u65b9\u306b\u5c11\u3057\u60a9\u3093\u3067\u3057\u307e\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u4f7f\u3044\u65b9\u3092\u899a\u3048\u3066\u3057\u307e\u3048\u3070\u975e\u5e38\u306b\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3060\u3063\u305f\u306e\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E3%81%84%E3%81%A9%E3%81%93%E3%82%8D\"><\/span>\u4f7f\u3044\u3069\u3053\u308d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306a\u3069\u306b\u304a\u3044\u3066\u5b66\u7fd2\u7528\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u6709\u7528\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3042\u308b\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089\u3001\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u5468\u671f\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u9577\u3055\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u30b9\u30c8\u30e9\u30a4\u30c9\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u81ea\u52d5\u7684\u306b\u5b66\u7fd2\u7528\u306e\u30d0\u30c3\u30c1\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>TensorFlow\u306e\u30b5\u30a4\u30c8\u306b\u30e1\u30bd\u30c3\u30c9\u306e\u8aac\u660e\u3084\u4f8b\u304c\u8f09\u3063\u3066\u3044\u307e\u3059\u306e\u3067\u6b63\u78ba\u306a\u6319\u52d5\u306f\u3053\u3061\u3089\u3092\u53c2\u8003\u306b\u3059\u308b\u306e\u304c\u826f\u3044\u3067\u3059\u3002<br><a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/utils\/timeseries_dataset_from_array\">tf.keras.utils.timeseries_dataset_from_array \u00a0|\u00a0 TensorFlow Core v2.8.0<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A4%9C%E8%A8%BC%E7%92%B0%E5%A2%83\"><\/span>\u691c\u8a3c\u74b0\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>python 3.8.10<\/p>\n\n\n\n<p>keras 2.7.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9F%E4%BE%8B\"><\/span>\u5b9f\u4f8b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u95a2\u6570\u306e\u6319\u52d5\u306b\u3064\u3044\u3066\u5b9f\u4f8b\u3092\u8e0f\u307e\u3048\u3066\u7d39\u4ecb\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u306f\u3001\u30c6\u30b9\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>data = [i for i in range(20)]\nprint(data)<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]<\/pre>\n\n\n\n<p>\u6b21\u306b\u3001timeseries_dataset_from_array\u3092\u5b9f\u969b\u306b\u4f7f\u7528\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import tensorflow as tf\ndataset = tf.keras.preprocessing.timeseries_dataset_from_array(\n    data,\n    targets=data[10:],\n    sequence_length=5,\n    sequence_stride=3,\n    sampling_rate=2,\n    shuffle=False,\n) <\/code><\/pre><\/div>\n\n\n\n<p>\u79c1\u306a\u308a\u306b\u89e3\u91c8\u3057\u305f\u7d50\u679c\u3001\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u610f\u5473\u5408\u3044\u3060\u3068\u601d\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>data : \u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u4f7f\u7528\u3059\u308b\u5165\u529b\u30c7\u30fc\u30bf\u3002<\/p>\n\n\n\n<p>targets : \u5b66\u7fd2\u3059\u308b\u969b\u306e\u30bf\u30fc\u30b2\u30c3\u30c8\u30c7\u30fc\u30bf\u3002\u4f8b\u3048\u3070\u3001\u6642\u7cfb\u5217\u4e88\u6e2c\u306a\u3069\u3092\u884c\u3046\u5834\u5408\u306f\u4e88\u6e2c\u5024\u306e\u6b63\u89e3\u30c7\u30fc\u30bf\u306a\u3069\u3002<\/p>\n\n\n\n<p>sequence_length : \u5404\u30b7\u30fc\u30b1\u30f3\u30b9\uff08\u30a6\u30a3\u30f3\u30c9\u30a6\uff09\u306b\u304a\u3051\u308b\u8981\u7d20\u6570\u3002<\/p>\n\n\n\n<p>sequence_stride : \u30b7\u30fc\u30b1\u30f3\u30b9\uff08\u30a6\u30a3\u30f3\u30c9\u30a6\uff09\u306e\u9593\u9694<\/p>\n\n\n\n<p>\u3053\u308c\u3060\u3051\u3067\u306f\u3044\u307e\u3044\u3061\u308f\u304b\u308a\u306b\u304f\u3044\u305f\u3081\u3001\u4f5c\u6210\u3055\u308c\u305fdataset\u306e\u4e2d\u8eab\u3092\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>for batch in dataset:\n    print(batch)<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">(&lt;tf.Tensor: shape=(4, 5), dtype=int32, numpy=\narray([[ 0,  2,  4,  6,  8],\n       [ 3,  5,  7,  9, 11],\n       [ 6,  8, 10, 12, 14],\n       [ 9, 11, 13, 15, 17]])&gt;, &lt;tf.Tensor: shape=(4,), dtype=int32, numpy=array([10, 13, 16, 19])&gt;)<\/pre>\n\n\n\n<p>BatchDataset\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4e0a\u8a18\u306e\u3088\u3046\u306a\u7d50\u679c\u304c\u5f97\u3089\u308c\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u3092\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u5408\u308f\u305b\u3066\u898b\u3066\u307f\u308b\u3068\u6b21\u306e\u3088\u3046\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"355\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/04\/image-12.png\" alt=\"\" class=\"wp-image-4845\" srcset=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/04\/image-12.png 601w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/04\/image-12-300x177.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>\u305d\u3057\u3066targets = data[10:]\u3068\u3057\u3066\u5f15\u304d\u6570\u3078\u4e0e\u3048\u305f\u7d50\u679c\u306farray[10, 13, 16, 19]\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u3053\u308c\u306f\u3064\u307e\u308a\u3001\u6642\u7cfb\u5217\u4e88\u6e2c\u306e\u5b66\u7fd2\u3092\u884c\u308f\u305b\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u5834\u5408\u306b\u3001<\/p>\n\n\n\n[0, 2, 4, 6, 8]\u306b\u5bfe\u3057\u3066\u6b63\u89e3\u304c 10<\/p>\n\n\n\n[3, 5, 7, 9, 10]\u306b\u5bfe\u3057\u3066\u6b63\u89e3\u304c 13<\/p>\n\n\n\n[6, 8, 10, 12, 14]\u306b\u5bfe\u3057\u3066\u6b63\u89e3\u304c 17<\/p>\n\n\n\n[9, 11, 13, 15, 17]\u306b\u5bfe\u3057\u3066\u6b63\u89e3\u304c 19<\/p>\n\n\n\n<p>\u3068\u3057\u3066\u5b66\u7fd2\u3067\u304d\u308b\u3088\u3046\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u6b63\u89e3\u30e9\u30d9\u30eb\u5074\u3092shape\u306b\u5408\u308f\u305b\u3066\u8abf\u6574\u3057\u3066\u304f\u308c\u308b\u3068\u3053\u308d\u304c\u975e\u5e38\u306b\u4fbf\u5229\u3060\u3068\u601d\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001tf.keras.utils.timeseries_dataset_from_array\u306f\u6642\u7cfb\u5217\u4e88\u6e2c\u306a\u3069\u306e\u30bf\u30b9\u30af\u3092\u884c\u3046\u969b\u306b\u5b66\u7fd2\u7528\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u306e\u306b\u975e\u5e38\u306b\u52b9\u7387\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u4eca\u5f8c\u6df1\u5c64\u5b66\u7fd2\u30bf\u30b9\u30af\u3092\u8a66\u3059\u5834\u5408\u306a\u3069\u306b\u7a4d\u6975\u7684\u306b\u6d3b\u7528\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 keras\u306e\u6642\u7cfb\u5217\u4e88\u6e2c\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff08Timeseries &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=4843","footnotes":""},"categories":[7,53],"tags":[99,101],"class_list":["post-4843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-53","tag-keras","tag-tensorflow","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4843"}],"version-history":[{"count":3,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4843\/revisions"}],"predecessor-version":[{"id":4847,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4843\/revisions\/4847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}