openfl icon indicating copy to clipboard operation
openfl copied to clipboard

Keras submodels are not supported

Open katerina-merkulova opened this issue 4 years ago • 2 comments

As far as I know, problem occurs at serialization and deserialization steps. User should define get_config and from_config methods (docs). Then config requires an InputLayer, but submodel doesn't have it. It seems that the InputLayer is only created if user wraps model with functional API (stackoverflow).

katerina-merkulova avatar Sep 17 '21 11:09 katerina-merkulova

@katerina-merkulova could you please write down a simple example of such network definition which is not supported.

alexey-gruzdev avatar Sep 17 '21 12:09 alexey-gruzdev

Example of model:

class Model(tf.keras.Model):
    def __init__(self, **kwargs):
        super().__init__()

        self.lstm = LSTM(1000)
        self.d1 = Dense(1000, activation='tanh')
        self.d2 = Dense(10719, activation='softmax')

    def call(self, x):
        x = self.lstm
        x = self.d1(x)
        x = self.d2(x)
        return x

And these are my attempts to define required methods:

    def get_config(self):
        config = dict({'name': self.name})
        config['layers'] = [
            {'class_name': layer.__class__.__name__,
             'config': layer.get_config()}
            for layer in self._self_tracked_trackables
        ]
        return config
    
    @classmethod
    def from_config(cls, config):
        return cls(**config)

katerina-merkulova avatar Sep 17 '21 13:09 katerina-merkulova