LSTMKeras是否考虑了时间序列之间的依赖关系?

2024-03-29 09:52:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有:

  • 多时间序列作为输入
  • 预测输出时间序列点

如何确保模型通过使用输入中所有时间序列之间的依赖关系来预测数据?在

编辑1
我的当前型号:

model = Sequential()
model.add(keras.layers.LSTM(hidden_nodes, input_dim=num_features, input_length=window, consume_less="mem"))
model.add(keras.layers.Dense(num_features, activation='sigmoid'))
optimizer = keras.optimizers.SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)

Tags: 数据模型add编辑inputmodel关系layers
1条回答
网友
1楼 · 发布于 2024-03-29 09:52:24

默认情况下,keras中的LSTM层(以及任何其他类型的递归层)不是有状态的,因此每次向网络输入新的输入时,状态都会重置。您的代码使用此默认版本。如果需要,可以通过在LSTM层内指定stateful=True使其有状态,然后状态将不会被重置。您可以阅读有关语法here的更多信息,this blog post提供了有关有状态模式的更多信息。在

下面是来自here的相应语法示例:

trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(100):
    model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
    model.reset_states()
# make predictions
trainPredict = model.predict(trainX, batch_size=batch_size)
model.reset_states()
testPredict = model.predict(testX, batch_size=batch_size)

相关问题 更多 >