简体   繁体   English

在 Keras Tensorflow 中将 TimeseriesGenerator 与多元数据集结合使用

[英]Using TimeseriesGenerator with a multivariate dataset in Keras Tensorflow

I am trying to model the output from a TimeseriesGenerator in Keras which is to be used as in input to the LSTM network, but have been facing issues.我正在尝试对 Keras 中 TimeseriesGenerator 的输出进行建模,该输出将用作 LSTM 网络的输入,但一直面临问题。 The dataset has the following structure:数据集具有以下结构:

在此处输入图片说明

where the set of features is shown in green (F1 to F6) and the target variable (T) shown in red.其中特征集显示为绿色(F1 到 F6),目标变量 (T) 显示为红色。 I have partitioned the total data set consisting of 3170 observations into into three sets :我将包含 3170 个观测值的总数据集划分为三组:

在此处输入图片说明

Since, the LSTM in Keras requires a input size of three dimensions, I reshaped the dataset using the following command :由于 Keras 中的 LSTM 需要三个维度的输入大小,因此我使用以下命令重塑了数据集:

        train= train.reshape((train_df.shape[0], 1, train_df.shape[1]))
        validation= validation.reshape((validation.shape[0], 1, validation.shape[1]))
        test= test.reshape((test.shape[0], 1, test.shape[1]))

Thus, the size of the reshaped dataset is as follows:因此,重构数据集的大小如下:

在此处输入图片说明

where the three dimensions are (samples, timesteps, features).其中三个维度是(样本、时间步长、特征)。 But the real issue is when the dataset is now passed to the timeseriesgenerator in keras.但真正的问题是现在将数据集传递给 keras 中的时间序列生成器。 The generator code used is as follows:使用的生成器代码如下:

        generator = TimeseriesGenerator(train, train_target, length=1, batch_size=10)

The TimeseriesGenerator passes the dataset to the fit_generator, which is as below: TimeseriesGenerator 将数据集传递给 fit_generator,如下所示:

        model.fit_generator(generator, validation_data=(validation, validation_target),
                                       epochs=100, verbose=0,
                                       shuffle=False, workers=1, use_multiprocessing=True)

and my LSTM network configuration in Keras is as follows:而我在 Keras 中的 LSTM 网络配置如下:

                model = Sequential()
                model.add(LSTM(200, input_shape=(10, 6), return_sequences=True))
                model.add(LSTM(200, input_shape=(10, 6), return_sequences=False))
                model.add(Dense(1, kernel_initializer='uniform', activation='linear')) 

The input_shape to the first LSTM layer is (10,6) which means 10 samples/observations having 6 features.第一个 LSTM 层的 input_shape 是(10,6) ,这意味着 10 个具有 6 个特征的样本/观察。 I choose an input_shape of (10,6) because the TimeseriesGenerator was supposed to generate a batch_size of 10 samples each having 6 features.我选择 (10,6) 的 input_shape 是因为 TimeseriesGenerator 应该生成 10 个样本的 batch_size,每个样本具有 6 个特征。

But, this causes an error as below:但是,这会导致如下错误:

ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (10, 1, 1, 6)

The TimeseriesGenerator generates the input size of the train set as (10, 1, 1, 6) . TimeseriesGenerator 生成训练集的输入大小为(10, 1, 1, 6) The train dataset generated has four dimensions, but, I expected the TimeseriesGenerator to generate a batch_size of 10 samples with each sample having 6 features, ie a train dataset having an input size of (10,1,6) .生成的训练数据集有四个维度,但是,我希望 TimeseriesGenerator 生成 10 个样本的 batch_size,每个样本具有 6 个特征,即输入大小为(10,1,6)的训练数据集。

How do I get the TimeseriesGenerator to generate an input size of (10,1,6) ?如何让 TimeseriesGenerator 生成(10,1,6)的输入大小?

The mistake was... Dont reshape your train, validation and test data... Generator automatically make it in the shape of (10, 1,6)..because when you gave batch size 10 then it will take 10 batches... Of having length of 1 data and 6 features... Try it.错误是...不要重塑您的训练、验证和测试数据...生成器会自动将其设为 (10, 1,6)..因为当您指定批次大小为 10 时,它将需要 10 个批次.. . 长度为 1 个数据和 6 个特征的...试试吧。 It will work..它会工作..

Just give your train, validation, test data in (m, n) generator will reshape it by itself只需在 (m, n) 生成器中提供您的训练、验证、测试数据即可自行重塑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分别使用 timeseries_dataset_from_array 和 TimeseriesGenerator 对齐 tensorflow/keras 的批量滑动帧时间序列数据 - Aligning batched sliding frame timeseries data for tensorflow/keras using timeseries_dataset_from_array and TimeseriesGenerator respectively TimeseriesGenerator feed 和 Dense 层的形状不兼容 - Keras/Tensorflow - Incompatible shapes of TimeseriesGenerator feed and Dense layer - Keras/Tensorflow 使用 tensorflow,keras 在 python 中加载 GTZAN 数据集 - load GTZAN dataset in python using tensorflow,keras 如何使用 Keras TimeseriesGenerator - How to use Keras TimeseriesGenerator Keras / Tensorflow:使用tf.data.Dataset API预测 - Keras / Tensorflow: Predict Using tf.data.Dataset API 从本地文件夹导入图像,而不是使用 Keras/Tensorflow 数据集 - importing images from local folder instead of using Keras/Tensorflow dataset 使用 Tensorflow Dataset 和 Keras Tuner 处理高度不平衡的数据集 - Dealing with highly imbalanced datasets using Tensorflow Dataset and Keras Tuner 使用 Galaxy Zoo 数据集、TensorFlow 和 Keras 训练 GAN - Training GAN using Galaxy Zoo dataset, TensorFlow, and Keras 在tensorflow / keras中加载自定义数据集 - Load custom dataset in tensorflow/keras 具有tf数据集输入的Tensorflow keras - Tensorflow keras with tf dataset input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM