简体   繁体   English

用于Keras LSTM模型的batch_input_shape

[英]batch_input_shape for Keras LSTM model

I am trying to build a neural network with an LSTM as first hidden layer with the Keras library (tensorflow backend). 我正在尝试使用Keras库(tensorflow后端)使用LSTM作为第一个隐藏层构建神经网络。 I am having problem understanding how to reshape my data and feed it to a stateful LSTM with the batch_input_size parameter. 我在理解如何重塑数据并将其使用batch_input_size参数将其馈送到有状态LSTM时遇到问题。

My input is a 100 second time series sampled at 10 Khz. 我的输入是在10 Khz采样的100秒时间序列。 So basically, I have 100*10000 different values of time. 所以基本上,我有100 * 10000个不同的时间值。 I have 3 different observables being sampled, so the number of features is 3. Let us call X the input matrix, with shape: 我采样了3个不同的可观察对象,因此特征数量为3。让我们将X称为输入矩阵,其形状为:

np.shape(X) = (1000000,1,3)

My target has one value per each point in time --> 100*10000 values: 我的目标在每个时间点都有一个值-> 100 * 10000个值:

np.shape(Y) = (1000000,1,1)

I want my model to take one second at a time, and therefore predict 10000 target values. 我希望我的模型一次花费一秒钟,因此可以预测10000个目标值。 I suppose this should be the batch_size . 我想这应该是batch_size target values Say I want to put 32 nodes in the LSTM layer, this is what I wrote: 目标值假设我想在LSTM层中放置32个节点,这就是我写的:

model_LSTM = Sequential()
model_LSTM.add(LSTM(32, stateful=True, batch_input_shape=(10000,1,3)))
model_LSTM.compile(optimizer=keras.optimizers.Adam(lr=0.00039, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), loss='mean_squared_error')

To fit the model I feed it one batch at a time: 为了适应模型,我一次一批进料:

batch_size=10000
for i in range(int(X.shape[0] / batch_size)):
    X = X_l[(i * batch_size):((i + 1) * batch_size)][:][:]
    Y = Y_l[(i * batch_size):((i + 1) * batch_size)]
    model_hist = model_LSTM.fit(X, Y, epochs=1, 
                                batch_size=batch_size, 
                                verbose=1, shuffle=False)

Am I doing this correctly? 我这样做正确吗? The script runs without errors, but when using model_LSTM.predict() on a new set it just outputs the same value for each time step. 该脚本运行时没有错误,但是在新集合上使用model_LSTM.predict()时,它只为每个时间步输出相同的值。

Can you provide more information such as accuracy and cost? 您能否提供更多信息,例如准确性和成本? Since, it is predicting only one class all the time, your model is not learning. 由于它一直只在预测一个班级,因此您的模型无法学习。 Perhaps, your dataset is skewed that results in high accuracy with no learning. 也许,您的数据集偏斜,导致无需学习即可获得高精度。 I would recommend tinkering hyperparameters and try different models such as SimpleRNN and GRU . 我建议修改超参数,并尝试使用其他模型,例如SimpleRNNGRU

Also, since you asked I would recommend using the fit_generator method as it would decrease the overhead of calling fit again and again. 另外,由于您提出了要求,我建议您使用fit_generator方法,因为它将减少一次又一次调用fit的开销。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM