Regularization too strong for autoencoder (Kens code autencoder tutorial)

I am using this tutorial about autoencoders: https://blog.keras.io/building-autoencoders-in-keras.html

All code works, however performance is very poor (results are blurry) when I set 10e-5

the regularization parameter, which is the parameter defined in the tutorial code. Actually I need to reduce the regularization to 10e-8

to get the correct output.

My question is: why is the result so different from the lesson? The data is the same, the parameters are the same, I did not expect a big difference.

I suspect the default behavior of Keras functions has been changed (was automatic batch normalization done in all cases?) As of May 14, 2016.

Outputs

Link to the blurred image output with 10e-5 regularization

  • With regularization 10e-5

    (blurry); val_loss

    0.2967

    after 50 epochs and 0.2774

    after 100 epochs.
  • With 10e-8

    regularization: val_loss

    0.1080

    after 50 epochs and 0.1009

    after 100 epochs.
  • No regularization: val_loss

    0.1018

    after 50 epochs and 0.0944

    after 100 epochs.

Full code (for reference)

# 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()

      

+7


source to share


1 answer


I have the same problem. And it's on GitHub here https://github.com/keras-team/keras/issues/5414 Looks like you were right by just changing the constant.



0


source







All Articles