Skip to content

Instantly share code, notes, and snippets.

@ericbhanson
Created October 30, 2018 12:24
Show Gist options
  • Save ericbhanson/f8f275f666cb31b3656d8572c35177f5 to your computer and use it in GitHub Desktop.
Save ericbhanson/f8f275f666cb31b3656d8572c35177f5 to your computer and use it in GitHub Desktop.
import keras
def add_lstm_layer(batch_size, stateful, units, X_shape):
lstm = keras.layers.LSTM(units, batch_input_shape=(batch_size, X_shape[1], X_shape[2]),
stateful=stateful)
return lstm
def compile_model(model, optimizer, loss='mean_squared_error'):
model.compile(loss=loss, optimizer=optimizer)
return model
def evaluate_model(data, batch_size, model):
evaluate_model = model.evaluate(data['X_test'], data['y_test'], batch_size=batch_size, verbose=2)
return evaluate_model
def fit_model(data, batch_size, epochs, model, shuffle):
fit_model = model.fit(data['X_train'], data['y_train'], batch_size=batch_size, epochs=epochs, shuffle=shuffle,
validation_data=(data['X_test'], data['y_test']), verbose=2)
return fit_model
def make_model(activation, batch_size, data, layers, stateful, units):
model = keras.models.Sequential()
model.add(add_lstm_layer(batch_size, stateful, units, data['X_train'].shape))
# TODO: Test whether return_sequences=True or False makes a difference.
for layer in layers:
model.add(layer)
model.add(activation)
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment