gpt4 book ai didi

python - 从经过训练的自动编码器中提取编码器和解码器

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:31 24 4
gpt4 key购买 nike

我想把autoencoder的学习和应用分为以下两部分https://blog.keras.io/building-autoencoders-in-keras.html并将 fashion-mnist 数据用于测试目的:

  1. 加载图像,进行可能需要数小时或数天的拟合,并使用回调来保存最佳自动编码器模型。该过程可能在接下来的部分之前几周。
  2. 使用这个最佳模型(通过文件名手动选择)并绘制原始图像、自动编码器的编码器进行的编码表示以及使用自动编码器的解码器进行的预测。我在从经过训练和保存的自动编码器中提取编码器和解码器层时遇到问题(参见第二步)。

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

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') 保存它。在我的真实示例中,我使用回调保存它,因此通过保存编码器和解码器的解决方法似乎不是真正的解决方案。稍后,我加载图像(未显示)并进行如下预测

# 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 步 我遇到问题的地方。我使用

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

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

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'

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

encoded_imgs = encoder.predict(x_test)

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

(784,) 的形状不正确。

因此,由于尺寸不正确,我对编码器的提取不起作用。我什至没有成功提取解码器(从保存的自动编码器),因为我不能使用 push() 并尝试了像 decoder = decoder.layers[-1:-2] 但它没有用。

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

最佳答案

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

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()的解决方案does not work since you need to update some of the internal attributes of the model .虽然,对于顺序模型,内置 pop()方法已经实现。

关于python - 从经过训练的自动编码器中提取编码器和解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com