Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying a Keras LSTM model in functional API

I have the following Keras LSTM model using functional API:

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)

The model works fine but I think there are redundant syntax in my architecture. For example, the Lambda layer is only used to define the input_shape, maybe it can be removed? Can the above code be simplified/cleaned (I want to keep using functional API)? Thanks!

like image 623
Edamame Avatar asked Mar 19 '18 00:03

Edamame


1 Answers

You can write your model using functional API as follows-

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

myModel=Model(x,model)
like image 101
user239457 Avatar answered Nov 14 '22 22:11

user239457