keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 910 forks source link

Wrapping a model using Lambda layer corrupts performance #168

Closed stigersh closed 4 years ago

stigersh commented 4 years ago

Summary

In the debugging process I checked the following: I have a keras model which I wrapped in a lambda. From some reason doing it made the optimization start from a worse accuracy point, and get stuck on that value, whereas without the lambda wrapper all is good.

Works bad:

class mymodel(object):
    def __init__():
        def lambda_wrapper(x):
            model = Sequential()
            #some layers like:
            cell = LSTM(100, return_sequences=True)
            layer = Bidirectional(cell)
            model.add(layer)
            return model(x)

       lambda_model_layer = Lambda(lambda x: lambda_wrapper(x))
       self.model = Sequential()
       self.model.add(lambda_model_layer)

Works fine:

class mymodel(object):
    def __init__():
        model = Sequential()
        #some layers like:
        cell = LSTM(100, return_sequences=True)
        layer = Bidirectional(cell)
        model.add(layer)
        self.model = model

Environment

Logs or source codes for reproduction

stigersh commented 4 years ago

I think I found the problem. Lambda layers weren't designed to be used with trainable variables. Thus everything inside the lambda becomes untrainable. That's why the code with the lambda is stuck on the same accuracy value. For writing a custom layer with trainable variables this link should be used: writing-your-own-keras-layers