{"id":4989,"date":"2022-09-19T01:10:58","date_gmt":"2022-09-18T16:10:58","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=4989"},"modified":"2022-09-19T01:11:13","modified_gmt":"2022-09-18T16:11:13","slug":"%e3%80%90keras%e3%80%91preprocessing-timeseries_dataset_from_array","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=4989","title":{"rendered":"\u3010keras\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=4989\/#%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=4989\/#%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%83%87%E3%83%BC%E3%82%BF\" >\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf<\/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=4989\/#preprocessingtimeseries_dataset_from_array%E3%82%92%E4%BD%BF%E3%81%86\" >preprocessing.timeseries_dataset_from_array\u3092\u4f7f\u3046<\/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=4989\/#%E7%B5%90%E6%9E%9C%E3%82%92%E7%A2%BA%E8%AA%8D\" >\u7d50\u679c\u3092\u78ba\u8a8d<\/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>\u6642\u7cfb\u5217\u306e\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306b\u4fbf\u5229\u306akeras\uff08tensorflow\uff09\u306epreprocessing.timesiereis_dataset_from_array\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b\u306e\u3067\u3001\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u4f7f\u3044\u65b9\u3092\u899a\u3048\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u7c21\u5358\u306b\u8a00\u3046\u3068\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u30ec\u30fc\u30c8\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u9577\u3055\u3001\u30b9\u30c8\u30e9\u30a4\u30c9\u3001\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u306a\u3069\u3092\u8a2d\u5b9a\u3059\u308b\u3068\u3001\u6df1\u5c64\u5b66\u7fd2\u306e\u5b66\u7fd2\u30c7\u30fc\u30bf\u5411\u3051\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u751f\u6210\u3057\u3066\u304f\u308c\u308b\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%83%87%E3%83%BC%E3%82%BF\"><\/span>\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u307e\u305a\u306f\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u307e\u3059\u3002\u5358\u7d14\u306b\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306a\u30c7\u30fc\u30bf\u3092200\u500b\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>sample_data = [i for i in range(200)]<\/code><\/pre><\/div>\n\n\n\n<p>\u307e\u305f\u3001\u3053\u306e\u30c7\u30fc\u30bf\u304b\u3089\u8aac\u660e\u5909\u6570\u3068\u76ee\u7684\u5909\u6570\u3092\u9069\u5f53\u306b\u7528\u610f\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u4eca\u56de\u306f\u5358\u7d14\u306b\u30e1\u30bd\u30c3\u30c9\u306e\u6319\u52d5\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306a\u306e\u3067\u3001\u305d\u308c\u305e\u308c\u306b\u8a2d\u5b9a\u3059\u308b\u7bc4\u56f2\u81ea\u4f53\u306b\u306f\u7279\u306b\u610f\u5473\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>x_train = sample_data[:150]\ny_train = sample_data[50:200]<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"preprocessingtimeseries_dataset_from_array%E3%82%92%E4%BD%BF%E3%81%86\"><\/span>preprocessing.timeseries_dataset_from_array\u3092\u4f7f\u3046<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>keras\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001preprocessing.timeseries_dataset_from_array\u3092\u4f7f\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u6642\u3001x_train, y_train\u306f\u5148\u307b\u3069\u8a2d\u5b9a\u3057\u305f\u8aac\u660e\u5909\u6570\u3001\u76ee\u7684\u5909\u6570\u3092\u5165\u529b\u3057\u307e\u3059\u3002seauence_length, sequence_stride, sampling_rate, batch_size\u306b\u3064\u3044\u3066\u306f\u6df1\u5c64\u5b66\u7fd2\u306b\u7cbe\u901a\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3059\u3067\u306b\u610f\u5473\u304c\u5206\u304b\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u308f\u304b\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u3067\u3082\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u7d50\u679c\u3092\u898b\u308c\u3070\u3042\u308b\u7a0b\u5ea6\u7406\u89e3\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de\u306f\u305d\u308c\u305e\u308c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>sequence_length : \u30d0\u30c3\u30c1\u5185\u306e\u4e00\u3064\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\uff08\u30a2\u30ec\u30a4\uff09\u306e\u9577\u3055<\/li><li>sampling_rate : \u5143\u30c7\u30fc\u30bf\u304b\u3089\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3059\u308b\u9593\u9694\u3002\u30c7\u30fc\u30bf\u304c\u9593\u5f15\u304d\u3055\u308c\u307e\u3059<\/li><li>sequence_stride : \u30b9\u30c8\u30e9\u30a4\u30c9\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u9593\u306e\u9593\u9694\u306e\u3088\u3046\u306a\u611f\u3058<\/li><li>batch_size : \u30c7\u30fc\u30bf\u3092\u30d0\u30c3\u30c1\u306b\u5206\u3051\u305f\u6642\u306e\u4e00\u3064\u306e\u30d0\u30c3\u30c1\u306e\u30b5\u30a4\u30ba<\/li><\/ul>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from tensorflow import keras\ndataset_train = keras.preprocessing.timeseries_dataset_from_array(\n    x_train,\n    y_train,\n    sequence_length=5,\n    sequence_stride=2,\n    sampling_rate=2,\n    batch_size=10,\n)<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%B5%90%E6%9E%9C%E3%82%92%E7%A2%BA%E8%AA%8D\"><\/span>\u7d50\u679c\u3092\u78ba\u8a8d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u3067\u306f\u3001\u6b21\u306b\u30e1\u30bd\u30c3\u30c9\u306e\u51fa\u529b\u3092\u78ba\u8a8d\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>print(&#39;len_dataset_train:&#39;,len(dataset_train))\nfor batch in dataset_train:\n    print(batch)<\/code><\/pre><\/div>\n\n\n\n<p>\u7d50\u679c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">len_dataset_train: 8\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[ 0,  2,  4,  6,  8],\n       [ 2,  4,  6,  8, 10],\n       [ 4,  6,  8, 10, 12],\n       [ 6,  8, 10, 12, 14],\n       [ 8, 10, 12, 14, 16],\n       [10, 12, 14, 16, 18],\n       [12, 14, 16, 18, 20],\n       [14, 16, 18, 20, 22],\n       [16, 18, 20, 22, 24],\n       [18, 20, 22, 24, 26]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([50, 52, 54, 56, 58, 60, 62, 64, 66, 68])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[20, 22, 24, 26, 28],\n       [22, 24, 26, 28, 30],\n       [24, 26, 28, 30, 32],\n       [26, 28, 30, 32, 34],\n       [28, 30, 32, 34, 36],\n       [30, 32, 34, 36, 38],\n       [32, 34, 36, 38, 40],\n       [34, 36, 38, 40, 42],\n       [36, 38, 40, 42, 44],\n       [38, 40, 42, 44, 46]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([70, 72, 74, 76, 78, 80, 82, 84, 86, 88])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[40, 42, 44, 46, 48],\n       [42, 44, 46, 48, 50],\n       [44, 46, 48, 50, 52],\n       [46, 48, 50, 52, 54],\n       [48, 50, 52, 54, 56],\n       [50, 52, 54, 56, 58],\n       [52, 54, 56, 58, 60],\n       [54, 56, 58, 60, 62],\n       [56, 58, 60, 62, 64],\n       [58, 60, 62, 64, 66]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([ 90,  92,  94,  96,  98, 100, 102, 104, 106, 108])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[60, 62, 64, 66, 68],\n       [62, 64, 66, 68, 70],\n       [64, 66, 68, 70, 72],\n       [66, 68, 70, 72, 74],\n       [68, 70, 72, 74, 76],\n       [70, 72, 74, 76, 78],\n       [72, 74, 76, 78, 80],\n       [74, 76, 78, 80, 82],\n       [76, 78, 80, 82, 84],\n       [78, 80, 82, 84, 86]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([110, 112, 114, 116, 118, 120, 122, 124, 126, 128])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[ 80,  82,  84,  86,  88],\n       [ 82,  84,  86,  88,  90],\n       [ 84,  86,  88,  90,  92],\n       [ 86,  88,  90,  92,  94],\n       [ 88,  90,  92,  94,  96],\n       [ 90,  92,  94,  96,  98],\n       [ 92,  94,  96,  98, 100],\n       [ 94,  96,  98, 100, 102],\n       [ 96,  98, 100, 102, 104],\n       [ 98, 100, 102, 104, 106]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([130, 132, 134, 136, 138, 140, 142, 144, 146, 148])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[100, 102, 104, 106, 108],\n       [102, 104, 106, 108, 110],\n       [104, 106, 108, 110, 112],\n       [106, 108, 110, 112, 114],\n       [108, 110, 112, 114, 116],\n       [110, 112, 114, 116, 118],\n       [112, 114, 116, 118, 120],\n       [114, 116, 118, 120, 122],\n       [116, 118, 120, 122, 124],\n       [118, 120, 122, 124, 126]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([150, 152, 154, 156, 158, 160, 162, 164, 166, 168])&gt;)\n(&lt;tf.Tensor: shape=(10, 5), dtype=int32, numpy=\narray([[120, 122, 124, 126, 128],\n       [122, 124, 126, 128, 130],\n       [124, 126, 128, 130, 132],\n       [126, 128, 130, 132, 134],\n       [128, 130, 132, 134, 136],\n       [130, 132, 134, 136, 138],\n       [132, 134, 136, 138, 140],\n       [134, 136, 138, 140, 142],\n       [136, 138, 140, 142, 144],\n       [138, 140, 142, 144, 146]])&gt;, &lt;tf.Tensor: shape=(10,), dtype=int32, numpy=array([170, 172, 174, 176, 178, 180, 182, 184, 186, 188])&gt;)\n(&lt;tf.Tensor: shape=(1, 5), dtype=int32, numpy=array([[140, 142, 144, 146, 148]])&gt;, &lt;tf.Tensor: shape=(1,), dtype=int32, numpy=array([190])&gt;)<\/pre>\n\n\n\n<p>\u30e1\u30bd\u30c3\u30c9\u306b\u8a2d\u5b9a\u3057\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u51fa\u529b\u306e\u95a2\u4fc2\u3092\u8868\u3059\u3068\u6b21\u306e\u3088\u3046\u306a\u5bfe\u5fdc\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/09\/image-4-700x300.png\" alt=\"\" class=\"wp-image-4994\" width=\"838\" height=\"359\" srcset=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/09\/image-4-700x300.png 700w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/09\/image-4-300x129.png 300w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/09\/image-4-768x329.png 768w, https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/09\/image-4.png 1523w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u81ea\u5206\u3067\u4f5c\u3063\u305f\u914d\u5217\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u8a66\u3059\u3068\u7406\u89e3\u3057\u3084\u3059\u3044\u3067\u3059\u306d\u3002\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u306a\u306e\u3067\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306b\u3064\u3044\u3066\u306f\u4eca\u5f8c\u3082\u591a\u7528\u3067\u304d\u305d\u3046\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u6642\u7cfb\u5217\u306e\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306b\u4fbf\u5229\u306akeras\uff08tensorfl&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4910,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=4989","footnotes":""},"categories":[103,7],"tags":[],"class_list":["post-4989","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras","category-python","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4989","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=4989"}],"version-history":[{"count":5,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4989\/revisions"}],"predecessor-version":[{"id":4996,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4989\/revisions\/4996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/4910"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}