本文介绍了保存和加载 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:...

虽然我可以从记忆中的变分自编码器中进行预测.为什么自编码器从磁盘加载时不起作用?

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 自动编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:23