【问题标题】:Using TF timeseries_dataset_from_array with more samples使用带有更多样本的 TF timeseries_dataset_from_array
【发布时间】:2022-01-12 20:56:53
【问题描述】:

我必须处理大量样本,其中每个样本都包含唯一的时间序列。 目标是将这些数据输入到 Tensorflow LSTM 模型中并预测一些特征。 我已经创建了 tf timeseries_dataset_from_array 生成器函数来将数据提供给 TF 模型,但是当我有多个样本时,我还没有弄清楚如何创建生成器函数。如果我使用通常的管道,tf timeseries_dataset_from_array 会重叠两个单独样本的时间序列。

有谁知道如何有效地将多个样本的时间序列传递给 TF 模型?

例如Human Activity Recognition Dataset 就是这样一个数据集,其中每个人都有一个单独的长时间序列,并且每个用户的时间序列都可以使用类似 SLIDING/ROLLING WINDOS 的timeseries_dataset_from_array 函数进一步解析。

这是一个更简单的例子:

我想使用timeseries_dataset_from_array 为 TF 模型生成样本。 示例:样本 1,其中第 0 列有 0,样本 2 从第 0 列有 100 开始。 这是一个更简单的例子:

我想获得没有重叠的 3D 数据(样本、时间步长、特征)。例如 (6,2,7) 像这样:

这里是示例代码:

from tensorflow.keras.preprocessing import timeseries_dataset_from_array
import numpy as np

x = np.array([[0,1,2,3,4,5,6],
              [0,11,12,13,14,15,16],
              [0,21,22,23,24,25,26],
              [0,31,32,33,34,35,36],
              [0,41,42,43,44,45,46]   
              ])

xx = np.concatenate((x, x+100), axis=0)#.reshape(2,5,6)  
    
sequence_length=2
stride=1
rate=1
input_dataset = timeseries_dataset_from_array(xx,
                                              None,
                                              sequence_length,
                                              sequence_stride=stride,
                                              sampling_rate=rate)

x_test = np.concatenate([x for x in input_dataset], axis=0)

【问题讨论】:

    标签: python tensorflow time-series data-preprocessing


    【解决方案1】:

    我已经想出了解决办法,可以避免重叠问题

    ### solution ######            
      
    # reshape the dataset          
    xxx = xx.reshape(2,5,7)  
    
    # create two tensorflow datasets
    input_dataset0 = timeseries_dataset_from_array(xxx[0,:,:],
                                                          None,
                                                          sequence_length,
                                                          sequence_stride=stride,
                                                          sampling_rate=rate)
    input_dataset1 = timeseries_dataset_from_array(xxx[1,:,:],
                                                          None,
                                                          sequence_length,
                                                          sequence_stride=stride,
                                                          sampling_rate=rate)
    # concatenate the two tensorflow datasets
    input_dataset = input_dataset0.concatenate(input_dataset1)
    
    x_test = np.concatenate([x for x in input_dataset], axis=0)
    

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 1970-01-01
      • 2021-01-30
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多