关于“在Keras建立自动编码器”?

时间:2017-04-28 06:04:31

标签: keras

我看了Building Autoencoders in Keras,网址是https://blog.keras.io/building-autoencoders-in-keras.htmlAdding a sparsity constraint on the encoded representations的部分中,我根据他的描述尝试过,但损失不能低至0.11,而是大约0.26。

结果是模糊的: enter image description here

任何做过这个实验的人都可以告诉我它有什么问题吗?

这是我的代码:

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

encoding_dim = 32 # 压缩后维度

input_img = Input(shape = (784,))
# 编码
encoded = Dense(encoding_dim, activation = 'relu',
                activity_regularizer = regularizers.l1(1e-4)
                )(input_img)
# 解码
decoded = Dense(784, activation = 'sigmoid')(encoded)

# 创建自动编码器
autoencoder = Model(input_img, decoded)

# 编码器
encoder = Model(input_img, encoded)

encoded_input = Input(shape = (encoding_dim,))
# 最后一层全连接层作为解码器
decoder_layer = autoencoder.layers[-1]

# 解码器
decoder = Model(encoded_input, decoder_layer(encoded_input))

# 编译模块
autoencoder.compile(optimizer = 'adadelta', loss = 'binary_crossentropy')

from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape(x_train.shape[0], np.prod(x_train.shape[1:]))
x_test = x_test.reshape(x_test.shape[0], np.prod(x_test.shape[1:]))

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


encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

import matplotlib.pyplot as plt

n = 10
plt.figure(figsize = (20, 4))
for i in range(10):
    # 原图
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.set_axis_off()

    # 解码后的图
    ax = plt.subplot(2, n, n + i + 1)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.set_axis_off()

plt.savefig('simpleSparse.png')

from keras import backend as K
K.clear_session()

3 个答案:

答案 0 :(得分:0)

我逐字复制了你的代码,并重现了你得到的错误。

解决方案:将批量大小从256减少到16。即使在10个训练时期之后,您的输出也会产生巨大差异。

说明:可能发生的事情是,即使你的训练损失有所减少,你也可以通过太多的例子对梯度求平均值,使得你在渐变方向上采取的步骤取消它本身就出现在一些更高维度的空间中,你的学习算法被欺骗,认为它融合到局部最小值,而实际上它不能决定去哪里。最后一部分解释了为什么看起来所有输出看起来都很模糊且完全一样。

更新:将批量减少到4,即使在10个时期之后,您也会接近完美重建。

答案 1 :(得分:0)

您需要更改代码中的批量大小。

autoencoder.fit(x_train,x_train,epochs = 10,batch_size = 16,shuffle = True,validation_data =(x_test,x_test))

答案 2 :(得分:0)

已知错误。需要将正则化器设置为10e-7

activity_regularizer=regularizers.activity_l1(10e-7))(input_img)

50个纪元后,val_loss:0.1424

https://github.com/keras-team/keras/issues/5414

如果减小批次大小,则需要更长的时间