How do we create a block (reusable set of functions) in Keras?

Viewed 2964

I am using Keras, actually tensorflow.keras to be specific and want to know if it is possible to create reusable blocks of inbuilt Keras layers. For example I would like to repeatedly use the following block at different times in my model.

conv1a = Conv3D(filters=32, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(inputs)
bn1a = BatchNormalization()(conv1a)
relu1a = ReLU()(bn1a)
conv1b = Conv3D(filters=32, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu1a)
bn1b = BatchNormalization()(conv1b)
relu1b = ReLU()(bn1b)

I have read about creating custom layers in Keras but I did not find the documentation to be clear enough.

Any help would be appreciated.

2 Answers

You could simply put it inside a function then use like:

relu1a = my_block(inputs)
relu1b = my_block(relu1a)

Also consider adding something such as with K.name_scope('MyBlock'): in the beginning of your function, so that things get wrapped in the graph as well.

So you'd have something like:

def my_block(inputs, block_name='MyBlock'):
  with K.name_scope(block_name):
    conv = Conv3D(filters=32, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(inputs)
    bn = BatchNormalization()(conv)
    relu = ReLU()(bn)

  return relu

If you specify block names:

relu1a = my_block(inputs, 'Block1')
relu1b = my_block(relu1a, 'Block2')

Define a method that receives your model and returns it with the added layers. Here's an example:

def get_modified_model(model):
    model.add(Dense(512, activation='relu', input_shape=(NUM_ROWS * NUM_COLS,)))
    model.add(Dropout(0.5))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.25))
    model.add(Dense(10, activation='softmax'))
    return model

such that the argument model passed to get_model is obtained from keras.models.Sequential()

Related