LSTM Keras API预测多个输出 [英] LSTM Keras API predicting multiple outputs
问题描述
我正在训练一个LSTM模型,使用50个步骤的序列来输入3种不同功能,如下所示:
I'm training an LSTM model using as input a sequence of 50 steps of 3 different features laid out as below:
#x_train
[[[a0,b0,c0],.....[a49,b49,c49]],
[a1,b1,c1]......[a50,b50,c50]],
...
[a49,b49,c49]...[a99,b99,c99]]]
使用以下因变量
#y_train
[a50, a51, a52, ... a99]
下面的代码只能预测a,如何在给定的时间步长预测并返回[a,b,c]向量?
The code below works to predict just a, how do I get it to predict and return a vector of [a,b,c] at a given timestep?
def build_model():
model = Sequential()
model.add(LSTM(
input_shape=(50,3),
return_sequences=True, units=50))
model.add(Dropout(0.2))
model.add(LSTM(
250,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(1))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
推荐答案
每层的输出取决于它具有多少个单元/单元/过滤器.
The output of every layer is based on how many cells/units/filters it has.
您的输出具有1个功能,因为Dense(1...)
只有一个单元格.
Your output has 1 feature because Dense(1...)
has only one cell.
只需将其设置为Dense(3...)
即可解决您的问题.
Just making it a Dense(3...)
would solve your problem.
现在,如果希望输出与输入具有相同的时间步长,则需要在所有LSTM层中打开return_sequences = True
.
Now, if you want the output to have the same number of time steps as the input, then you need to turn on return_sequences = True
in all your LSTM layers.
LSTM的输出是:
- (批量大小,单位)-带
return_sequences=False
- (批量大小,时间步长,单位)-使用
return_sequences=True
- (Batch size, units) - with
return_sequences=False
- (Batch size, time steps, units) - with
return_sequences=True
然后,在随后的图层中使用TimeDistributed
图层包装,就像它们也有时间步长一样(它基本上会将尺寸保留在中间).
Then you use a TimeDistributed
layer wrapper in your following layers to work as if they also had time steps (it will basically preserve the dimension in the middle).
def build_model():
model = Sequential()
model.add(LSTM(
input_shape=(50,3),
return_sequences=True, units=50))
model.add(Dropout(0.2))
model.add(LSTM(
250,
return_sequences=True))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(3)))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
这篇关于LSTM Keras API预测多个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!