从训练有素的自动编码器中提取编码器和解码器 [英] Extract encoder and decoder from trained autoencoder

查看:147
本文介绍了从训练有素的自动编码器中提取编码器和解码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https:之后,我想将自动编码器的学习和应用分为两部分: //blog.keras.io/building-autoencoders-in-keras.html 并使用fashion-mnist数据进行测试:

I want to divide the autoencoder learning and applying into two parts following https://blog.keras.io/building-autoencoders-in-keras.html and using the fashion-mnist data for testing purposes:

  1. 加载图像,进行可能需要几个小时或几天的拟合,并使用回调函数来保存最佳的自动编码器模型.该过程可能比下一部分要早几个星期.
  2. 使用最佳模型(通过文件名手动选择)并绘制原始图像,自动编码器的编码器进行的编码表示以及使用自动编码器的解码器进行的预测. 我在从训练有素的自动编码器中提取编码器和解码器层时遇到问题(请参阅第二步).
  1. Load the images, do the fitting that may take some hours or days and use a callback to save the best autoencoder model. That process can be some weeks before the following part.
  2. Use this best model (manually selected by filename) and plot original image, the encoded representation made by the encoder of the autoencoder and the prediction using the decoder of the autoencoder. I have problems (see second step) to extract the encoder and decoder layers from the trained and saved autoencoder.

对于第一步,我有一个非常简单的网络,如下所示:

For step one I have the very simple network as follows:

input_img = Input(shape=(784,))
# encoded representation
encoded = Dense(encoding_dim, activation='relu')(input_img)
# lossy reconstruction
decoded = Dense(784, activation='sigmoid')(encoded)

# full AE model: map an input to its reconstruction
autoencoder = Model(input_img, decoded)

# encoder: map an input to its encoded representation
encoder = Model(input_img, encoded)
# placeholder for an encoded input
encoded_input = Input(shape=(encoding_dim,))
# last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# decoder
decoder = Model(encoded_input, decoder_layer(encoded_input))

网络是:

autoencoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
_________________________________________________________________
dense_6 (Dense)              (None, 784)               25872     
=================================================================

encoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
=================================================================

因此,我训练模型并通过autoencoder.save('fashion-autoencoder.hdf5')保存它.在我的真实示例中,我使用回调将其保存,因此通过保存编码器和解码器的解决方法似乎不是真正的解决方案.稍后,我加载图像(未显示)并进行类似的预测

So I train the model and save it by autoencoder.save('fashion-autoencoder.hdf5'). In my real example I save it with a callback so a workaround by saving the encoder and decoder do not seem a real solution. Later I load the images (not shown) and do the predictions like

# encode and decode some images from test set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# test the shape
print(encoded_imgs[0].shape)

并得到(32,0)的形状.

因此,我要解决两个第2步问题.我使用

So lets go two step 2 where I have my problems. I load the model using

encoder= K.models.load_model('fashion-autoencoder.hdf5')
# delete the last layers to get the encoder
encoder.layers.pop()
encoder.summary() # show model data

并且编码器看起来与第一步中的原始编码器相同,这让我认为提取效果很好:

and the encoder looks the same as the original in step one what make me think the the extraction has worked well:

Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
=================================================================
Total params: 50,992
Trainable params: 50,992
Non-trainable params: 0

但我也收到警告

training.py:478: UserWarning: Discrepancy between trainable weights and collected trainable weights, did you set `model.trainable` without calling `model.compile` after ?
'Discrepancy between trainable weights and collected trainable'

我以某种方式理解但不知道它有多重要.然后我再次加载图像(未显示)并使用编码器

that I understand in a kind of way but do not know how important it is. Then I load images again (not shown) and use the encoder

encoded_imgs = encoder.predict(x_test)

# test the shape
print(encoded_imgs[0].shape)

但是形状与(784,)不匹配.

因此,由于尺寸不正确,我无法提取编码器. 我什至没有成功提取解码器(形成保存自动编码器),因为我无法使用push()并尝试了decoder = decoder.layers[-1:-2]之类的东西,但是没有用.

So, my extraction for the encoder did not work since the dimensions are not correct. I even have less success extracting the decoder (form the save autoencoder) since I cannot use push() and tried stuff like decoder = decoder.layers[-1:-2] but it did not work.

所以,我的一般问题是如何提取部分已加载的模型.

So, my general question is how to extract parts of loaded models.

推荐答案

由于您使用功能性API创建自动编码器,因此重构编码器和解码器的最佳方法是再次使用功能性API和Model类:

Since you are using functional API for creating the autoencoder, the best way to reconstruct the encoder and decoder is to use the functional API and the Model class again:

autoencoder= K.models.load_model('fashion-autoencoder.hdf5')

encoder = Model(autoencoder.input, autoencoder.layers[-2].output)

decoder_input = Input(shape=(encoding_dim,))
decoder = Model(decoder_input, autoencoder.layers[-1](decoder_input))

encoder.summary()
decoder.summary()

模型摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 32)                25120     
=================================================================
Total params: 25,120
Trainable params: 25,120
Non-trainable params: 0
_________________________________________________________________


Layer (type)                 Output Shape              Param #   
=================================================================
input_6 (InputLayer)         (None, 32)                0         
_________________________________________________________________
dense_4 (Dense)              (None, 784)               25872     
=================================================================
Total params: 25,872
Trainable params: 25,872
Non-trainable params: 0
_________________________________________________________________

涉及layers属性pop()的解决方案不起作用因为您需要更新模型的一些内部属性.虽然,对于顺序模型,内置的 方法已实现.

The solution involving pop() on layers attribute does not work since you need to update some of the internal attributes of the model. Although, for sequential models a built-in pop() method has been implemented.

这篇关于从训练有素的自动编码器中提取编码器和解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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