在功能API中简化Keras LSTM模型

时间:2018-03-19 00:54:28

标签: functional-programming keras lstm

我有以下使用功能API的Keras LSTM模型:

model = Sequential()
model.add(Lambda(lambda x: x,input_shape=(timestep,n_feature)))
output = model.output
output = LSTM(8)(output)
output = Dense(2)(output)

inputTensor = model.input
myModel = Model([inputTensor], output)
myModel.compile(loss='mean_squared_error', optimizer='adam')

myModel.fit([trainX], trainY, epochs=100, batch_size=1, verbose=2, validation_split = 0.1)

该模型工作正常,但我认为我的架构中存在冗余语法。例如,Lambda层仅用于定义input_shape,也许它可以被删除?上面的代码可以简化/清理(我想继续使用功能API)?谢谢!

1 个答案:

答案 0 :(得分:3)

您可以使用功能API编写模型,如下所示 -

x=Input(shape=(timestep,n_feature))
model=LSTM(8)(x)
model=Dense(2)(model)

myModel=Model(x,model)