深度自动编码器的 Python/Keras/Theano 错误维度 [英] Python/Keras/Theano wrong dimensions for Deep Autoencoder
问题描述
我正在尝试遵循 Deep Autoencoder Keras 示例.我遇到了尺寸不匹配异常,但对于我的生活,我不知道为什么.当我只使用一个编码维度时它有效,但当我堆叠它们时无效.
I'm trying to follow the Deep Autoencoder Keras example. I'm getting a dimension mismatch exception, but for the life of me, I can't figure out why. It works when I use only one encoded dimension, but not when I stack them.
异常:输入0与dense_18层不兼容:
预期形状=(无,128),发现形状=(无,32)*
Exception: Input 0 is incompatible with layer dense_18:
expected shape=(None, 128), found shape=(None, 32)*
错误在一行 decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
from keras.layers import Dense,Input
from keras.models import Model
import numpy as np
# this is the size of the encoded representations
encoding_dim = 32
#NPUT LAYER
input_img = Input(shape=(784,))
#ENCODE LAYER
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim*4, activation='relu')(input_img)
encoded = Dense(encoding_dim*2, activation='relu')(encoded)
encoded = Dense(encoding_dim, activation='relu')(encoded)
#DECODED LAYER
# "decoded" is the lossy reconstruction of the input
decoded = Dense(encoding_dim*2, activation='relu')(encoded)
decoded = Dense(encoding_dim*4, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)
#MODEL
autoencoder = Model(input=input_img, output=decoded)
#SEPERATE ENCODER MODEL
encoder = Model(input=input_img, output=encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
#COMPILER
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
推荐答案
感谢 Marcin 的提示.事实证明,所有解码器层都需要展开才能使其工作.
Thanks for the hint from Marcin. Turns out all the decoder layers need to be unrolled in order to get it to work.
# retrieve the last layer of the autoencoder model
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))
这篇关于深度自动编码器的 Python/Keras/Theano 错误维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!