Search code examples
pythontensorflowkeras

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


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.


Solution

  • 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')