Save and load keras autoencoder

左心房为你撑大大i 提交于 2020-01-05 05:26:09

问题


Look at this strange load/save model situation. I saved variational autoencoder model and its encoder and decoder:

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

After that I loaded all of it from the disk:

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})

If I try

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]))

Everything works just fine as if I use encoder/decoder from the memory, but if I try

vae = autoencoder_disk.predict(x_test_encoded) 

I got

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:...

although I can predict from the variational autoencoder from the memory. Why autoencoder does not work when it is loaded from the disk?


回答1:


You are passing the encoded output x_test_encoded as the input to the autoencoder autoencoder_disk.predict(x_test_encoded). You should pass the original input the encoder expects x_test instead.



来源:https://stackoverflow.com/questions/51132099/save-and-load-keras-autoencoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!