Код: Выделить всё
import keras
class InnerModel(keras.Model):
def __init__(self, **kwargs):
super().__init__()
self.dense1 = keras.layers.Dense(64, activation='relu')
self.dense2 = keras.layers.Dense(32, activation='relu')
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
@keras.saving.register_keras_serializable()
class OuterModel(keras.Model):
def __init__(self, inner_model, **kwargs):
super().__init__(**kwargs)
self.inner_model = inner_model
self.dense3 = keras.layers.Dense(16, activation='relu')
self.output_layer = keras.layers.Dense(1)
def call(self, inputs):
x = self.inner_model(inputs)
x = self.dense3(x)
return self.output_layer(x)
def get_config(self):
config = super().get_config()
config.update({
"inner_model": self.inner_model,
})
return config
inner_model = InnerModel()
outer_model = OuterModel(inner_model)
outer_model.save('outer_model.keras')
loaded_model = keras.saving.load_model('outer_model.keras')
loaded_model.summary()
Код: Выделить всё
@classmethod
def from_config(cls, config):
inner_model = config["inner_model"]
return cls(InnerModel.from_config(inner_model))
Код: Выделить всё
"inner_model": self.inner_model.get_config()
Подробнее здесь: https://stackoverflow.com/questions/793 ... taining-an