Skip to content

Instantly share code, notes, and snippets.

@louismagowan
Created June 13, 2022 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louismagowan/6631f0cda54e9a3b6e2ef6c1e7ffe2b4 to your computer and use it in GitHub Desktop.
Save louismagowan/6631f0cda54e9a3b6e2ef6c1e7ffe2b4 to your computer and use it in GitHub Desktop.
from tensorflow.keras import optimizers
from keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Dense, BatchNormalization, Reshape, Activation
from tensorflow.keras.layers import Embedding, GRU, Bidirectional
from tensorflow.keras import Sequential
# Model compilation params
compile_hp = dict()
compile_hp["loss"] = "binary_crossentropy"
compile_hp["optimizer"] = optimizers.Adam(learning_rate = 0.001)
compile_hp["metrics"] = ["accuracy"]
compile_hp["maxlen"] = 500
# Model fitting params
fit_hp = dict()
fit_hp["batch_size"] = 64
fit_hp["epochs"] = 100
fit_hp["validation_split"] = 0.3
# Create callback to select the best model
fit_hp["callbacks"] = EarlyStopping(monitor = "val_loss",
mode = "min",
restore_best_weights = True,
patience = 10)
def bi_gru(loss = "binary_crossentropy",
optimizer = "adam",
metrics = ["accuracy"],
batch_normalize = False,
embedding = None,
maxlen = 500,
hidden_dense_units = 256,
dense_kernel_initializer = "glorot_uniform",
rnn_units = 32,
rnn_kernel_initializer = "glorot_uniform"):
# Build model
model = Sequential(name = "GRU")
# Add embedding if desired
if embedding:
# Embedding contains input shape
model.add(embedding)
else:
# Otherwise reshape data to work with GRU
model.add(Reshape((maxlen, 1), input_shape = (maxlen, ), name = "Reshaping"))
# Add GRU
model.add(Bidirectional(GRU(rnn_units,
kernel_initializer = rnn_kernel_initializer),
name = "Bidirectional_GRU"))
# Baseline model
model.add(Dense(hidden_dense_units, name = "Linear_Dense",
kernel_initializer = dense_kernel_initializer))
# Batch normalised model
if batch_normalize:
model.add(BatchNormalization(name = "Batch_Norm1"))
# Apply non-linear activation, specified in this way to be consistent
# with the original paper
model.add(Activation("relu", name = "ReLU_Activation"))
# Output layer
model.add(Dense(1, activation = "sigmoid", name = "Output",
kernel_initializer = dense_kernel_initializer))
# Compile model
model.compile(loss = loss, optimizer = optimizer,
metrics = metrics)
return model
# Set embedding
embedding_layer = "keras"
# Toggle batch normalization
batch_normalize = True
# Build and fit model with embedding
model = bi_gru(**compile_hp, batch_normalize=batch_normalize,
embedding = embed_dict[embedding_layer])
model.summary()
history = model.fit(X_train, y_train, **fit_hp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment