我使用ModelCheckpoint来保存最佳的自动编码器模型,如下所示:
checkpoint = ModelCheckpoint("ae_model", monitor='val_loss', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
# encoder layer
encoded = Dense(128, activation='relu')(input)
encoder_output = Dense(10)(encoded)
# decoder layer
decoded = Dense(128, activation='relu')(decoded)
# construct the autoencoder model
autoencoder = Model(input=input_img, output=decoded)
# construct the encoder model
encoder = Model(input=input_img, output=encoder_output)
autoencoder.compile(loss='mse', optimizer='adam')
autoencoder.fit(x_train, x_train, epochs=100, batch_size=10,
shuffle=True, validation_split=0.33,
callbacks=callbacks_list)
但是,当保存最佳的最佳自动编码器模型时,如何保存编码器模型呢?因此,我可以像下面那样重用编码器模型。
from keras.models import load_model
encoder = load_model('encoder_model')
还是有其他方法可以将编码器与自动编码器模型分开?
from keras.models import load_model
autoencoder = load_model('autoencoder_model')
encoder = autoencoder.???
谢谢,
最佳答案
您可以编写小的自定义ModelCheckpoint类,以替换应保存的模型:
class EncoderCheckpoint(ModelCheckpoint):
def __init__(self, filepath, **kwargs):
super().__init__(filepath, **kwargs)
self.model = encoder # we manually set encoder model
def set_model(self, model):
pass # ignore when Keras tries to set autoencoder model
关于python - 保存自动编码器的编码器模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50711764/