Skip to content

Instantly share code, notes, and snippets.

@L-Lewis
Last active October 2, 2019 12:19
Show Gist options
  • Save L-Lewis/0299796cfaf23621aaf111470a02f22e to your computer and use it in GitHub Desktop.
Save L-Lewis/0299796cfaf23621aaf111470a02f22e to your computer and use it in GitHub Desktop.
from keras.models import Sequential
from keras.optimizers import Adam # Other optimisers are available
# Create the MLP and CNN models
mlp = create_mlp(trainAttrX.shape[1])
cnn = create_cnn(128, 128, 3)
# Create the input to the final set of layers as the output of both the MLP and CNN
combinedInput = concatenate([mlp.output, cnn.output])
# The final fully-connected layer head will have two dense layers (one relu and one sigmoid)
x = Dense(4, activation="relu")(combinedInput)
x = Dense(1, activation="sigmoid")(x)
# The final model accepts numerical data on the MLP input and images on the CNN input, outputting a single value
model1 = Model(inputs=[mlp.input, cnn.input], outputs=x)
# Compile the model
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model1.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=opt)
# Train the model
model1_history = model1.fit(
[trainAttrX, trainImagesX],
trainY,
validation_data=([testAttrX, testImagesX], testY),
epochs=5,
batch_size=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment