Last active
February 28, 2018 09:31
-
-
Save kittinan/86ff2c5d0d72514e42fee470cafea870 to your computer and use it in GitHub Desktop.
Keras add a variable into a convolutional neural network's dense layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Adding a variable into Keras/TensorFlow CNN dense layer | |
| https://stackoverflow.com/questions/42556919/adding-a-variable-into-keras-tensorflow-cnn-dense-layer | |
| ___________ _________ _________ _________ ________ ______ | |
| | Conv | | Max | | Conv | | Max | | | | | | |
| Image --> | Layer 1 | --> | Pool 1 | --> | Layer 2 | --> | Pool 2 | -->| | | | | |
| |_________| |________| |_________| |________| | Dense | | Out | | |
| | Layer |-->|_____| | |
| Other ------------------------------------------------------------>| | | |
| Data | | | |
| |_______| | |
| """ | |
| import keras | |
| from keras.models import Model | |
| from keras.layers import Dense, Merge, Flatten, Input, Conv2D, MaxPooling2D | |
| # this is your image input definition. You have to specify a shape. | |
| image_input = Input(shape=(32,32,3)) | |
| # Some more data input with 10 features (eg.) | |
| other_data_input = Input(shape=(10,)) | |
| # First convolution filled with random parameters for the example | |
| conv1 = Conv2D(32, (3, 3),activation = "tanh")(image_input) | |
| # MaxPool it | |
| conv1 = MaxPooling2D()(conv1) | |
| # Second Convolution | |
| conv2 = Conv2D(64, (3, 3),activation = "tanh")(conv1) | |
| # MaxPool it | |
| conv2 = MaxPooling2D()(conv2) | |
| # Flatten the output to enable the merge to happen with the other input | |
| first_part_output = Flatten()(conv2) | |
| # Merge the output of the convNet with your added features by concatenation | |
| merged_model = keras.layers.concatenate([first_part_output, other_data_input]) | |
| # Predict on the output | |
| predictions = Dense(4, activation='softmax')(merged_model) | |
| # Now create the model | |
| model = Model(inputs=[image_input, other_data_input], outputs=predictions) | |
| # see your model | |
| model.summary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment