自动编码器的正规化太强(Keras自动编码器教程代码)

ahs*_*tat 9 python regularized autoencoder keras

我正在使用本教程关于autoencoders:https://blog.keras.io/building-autoencoders-in-keras.html

所有代码都正常工作,但是当我为正则化参数设置10e-5时性能非常糟糕(结果很模糊),这是教程代码中定义的参数.实际上,我需要将正则化减少到10e-8以获得正确的输出.

我的问题如下:为什么结果与教程有如此不同?数据是相同的,参数是相同的,我没想到会有很大的差异.

我怀疑从2016年5月14日起Keras功能的默认行为已经改变(在所有情况下都执行自动批量标准化?).

输出:

使用10e-5正则化链接到模糊图像输出

用10e-5正则化(模糊); 50个时期后val_loss为0.2967,100个时期后为0.2774

使用10e-8正则化:50个历元后的val_loss为0.1080,100个历元后为0.1009.

没有正则化:50个时期后val_loss为0.1018,100个时期后为0.0944.

完整代码(供参考):

# Source: https://blog.keras.io/building-autoencoders-in-keras.html
import numpy as np
np.random.seed(2713)

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

encoding_dim = 32

input_img = Input(shape=(784,))
# add a Dense layer with a L1 activity regularizer
encoded = Dense(encoding_dim, activation='relu',
                activity_regularizer=regularizers.l1(10e-5))(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)

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')

from keras.datasets import mnist
(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((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)

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

# encode and decode some digits
# note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

# use Matplotlib (don't ask)
import matplotlib.pyplot as plt

n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
Run Code Online (Sandbox Code Playgroud)

小智 1

我有同样的问题。它位于 GitHub 上https://github.com/keras-team/keras/issues/5414 看起来你只是改变常量是正确的。