保存并加载keras自动编码器 [英] Save and load keras autoencoder

查看:295
本文介绍了保存并加载keras自动编码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果我尝试

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) 

我知道了

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时不起作用?

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

推荐答案

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

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.

这篇关于保存并加载keras自动编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆