保存和加载keras自动编码器

时间:2018-07-02 08:40:45

标签: keras autoencoder

看看这种奇怪的加载/保存模型情况。我保存了变分自动编码器模型及其编码器和解码器:

autoencoder.save("autoencoder_save", overwrite=True)
encoder.save("encoder_save", overwrite=True)
decoder.save("decoder_save", overwrite=T)

之后,我从磁盘上加载了所有文件:

autoencoder_disk = load_model("autoencoder_save", custom_objects={'KLDivergenceLayer': KLDivergenceLayer,
                                                       'nll': nll})
encoder_disk = load_model("encoder_save", custom_objects={'KLDivergenceLayer': KLDivergenceLayer,
                                                       'nll': nll})
decoder_disk = load_model("decoder_save", custom_objects={'KLDivergenceLayer': KLDivergenceLayer,
                                                       'nll': nll})

如果我尝试

x_test_encoded = encoder_disk.predict(x_test,batch_size=batch_size)
x_test_decoded = decoder_disk.predict(x_test_encoded)
print(np.round(x_test_decoded[3]))

一切正常,就好像我使用内存中的编码器/解码器一样,但是如果我尝试

vae = autoencoder_disk.predict(x_test_encoded) 

我知道了

ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s) but instead got the following list of 1 arrays:...

尽管我可以从内存中的变化型自动编码器进行预测。为什么从磁盘加载autoencoder时不起作用?

1 个答案:

答案 0 :(得分:0)

您要将编码的输出x_test_encoded作为输入传递给自动编码器autoencoder_disk.predict(x_test_encoded)。您应该传递编码器期望x_test的原始输入。