I am Charmie

メモとログ

customize a model in Keras

Here's a note to customize a pre-defined model in Keras. This official information and stackoverflow post gave me how to do it.

A Model is a list of layers (or layer instances) such as

[code lang="python"] model = Sequential([ Dense(32, input_shape=(784,)), Activation('relu'), Dense(10), Activation('softmax'), ]) [/code] Each layer has its input and output as tensor object and they are accessible as [code lang="python"] for layer in model.layers: print(layer.input) print(layer.output) [/code]

  1. keep first N layers give N-th layer's output model.layers[N].output as an input of a new N+1-th layer. [code lang="python"] x = Conv2D(...)(model.layers[N].output) x = Conv2D(...)(x) x = MaxPool2D(...)(x) [/code]

  2. add a model to a middle layer of another model give model1's N-th layer's output as an input of model2's M-th layer. [code lang="python"] x = model2.layersM x = Conv2D(...)(x) x = Conv2D(...)(x) x = MaxPool2D(...)(x) [/code]