使用Keras LSTM模型时,我遇到了一个奇怪的问题。我构建了一个单层LSTM并尝试使用它。我发现模型的输出在一个输入和多个输入之间是不同的,如下面的代码所示。
def lstmTest(training, latent_dim=10):
_, time_dim, input_dim = training.shape
# Define an input sequence and process it.
encoder_inputs = Input(shape=(time_dim, input_dim), name='input')
encoder = LSTM(latent_dim, return_state=False, name='lstm')
encoder_outputs = encoder(encoder_inputs)
model = Model(encoder_inputs, encoder_outputs)
return model
def trainingTest(model, training, nb_epoch=10, batch_size=300):
model.compile(optimizer='adam', loss='mse', metrics=['acc'])
history = model.fit(training, training[:, -1, :10],
epochs=nb_epoch,
batch_size=batch_size,
shuffle=True,
verbose=1,
).history
return history
myVector = [[[i]*20]*8 for i in range(100)]
myVector = np.array(myVector)
lstmTest = lstmTest(myVector)
history = trainingTest(lstmTest, myVector)
vector = myVector[:2]
res1 = lstmTest.predict(vector)
vector = myVector[:1]
res2 = lstmTest.predict(vector)
res2[0] - res1[0]
我得到了以下结果
array([0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 5.8207661e-11,
0.0000000e+00, 2.3283064e-10, 0.0000000e+00, 0.0000000e+00,
0.0000000e+00, 0.0000000e+00], dtype=float32)
但是如果我将res2更改为与res1相同,则会得到预期的结果
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
有人来过这个问题吗?
答案 0 :(得分:2)