如何将编码器的编码输出提供给分类器的输入?

时间:2018-11-19 17:44:52

标签: python keras neural-network classification autoencoder

我正在尝试使用AutoEncoder来增强分类器的性能。

这是我使用kerist文档编写的MNIST数据的AutoEncoder

from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5, assuming the input is 784 floats
encoding_dim = 32

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, 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(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))

现在,我正在尝试将其连接到分类器。我使用this question写道:

x = encoder.output
# h = Dense(3, activation='relu', name='hidden')(x)
y = Dense(1, activation='sigmoid', name='predictions')(x)

classifier = Model(inputs=autoencoder.inputs, outputs=y)


# Compile model
classifier.compile(loss='binary_crossentropy', optimizer='adam',
                   metrics=['accuracy'])

# Fit the model
history = classifier.fit(x_train, y_train, 
                         epochs=10, 
                         batch_size=10,
                         validation_split=.1)

首先,我不了解此代码。其次,我猜y的输出必须是10的10位数字,但是由于出现错误,我不能将其设置为10。

无论如何,以上分类器的准确性非常低(10%)!我的方法有什么问题吗?

0 个答案:

没有答案