LSTM-Keras 是否考虑了时间序列之间的依赖关系?
Does LSTM-Keras take into account dependencies between time series?
我有:
- 多个时间序列作为输入
- OUTPUT 中的预测时间序列点
如何确保模型通过使用输入中所有时间序列之间的依赖关系来预测数据?
编辑 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)
默认情况下,keras 中的 LSTM 层(以及任何其他类型的循环层)不是有状态的,因此每次将新输入馈入网络时都会重置状态。您的代码使用此默认版本。如果你愿意,你可以通过在 LSTM 层内指定 stateful=True
使其成为有状态的,这样状态就不会被重置。您可以阅读有关相关语法的更多信息 here, and 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)
我有:
- 多个时间序列作为输入
- OUTPUT 中的预测时间序列点
如何确保模型通过使用输入中所有时间序列之间的依赖关系来预测数据?
编辑 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)
默认情况下,keras 中的 LSTM 层(以及任何其他类型的循环层)不是有状态的,因此每次将新输入馈入网络时都会重置状态。您的代码使用此默认版本。如果你愿意,你可以通过在 LSTM 层内指定 stateful=True
使其成为有状态的,这样状态就不会被重置。您可以阅读有关相关语法的更多信息 here, and 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)