Search code examples
deep-learningkeraskeras-layer

How to test the correctness of a Keras custom layer?


After creating a Keras custom layer with training weight, how can one test the correctness of the code? It does not seem to be described in Keras' manual.

For example, to test the expected behavior of a function, one can write a unit test. How can we do this for a Keras custom layer?


Solution

  • You can still do something like unit test by getting the output of the custom layer for the given input and verifying it against the manually calculated output,

    Let's say your custom layer Custom takes (None, 3, 200) as input shape and returns (None, 3)

    from keras.layers import Input
    from keras.models import Model
    
    inp = Input(shape=(3, 200))
    out = Custom()(inp)
    model = Model(inp, out)
    
    output = model.predict(your_input)
    

    You can verify the layer output output with your expected output for a known input your_input.