1

I'm dealing with a stacked denoising autoencoder in keras. So for after generating the noised input using random tool, I noticed that even if I use a checkpointer to save the weights of the model, I got different encoded data each time I run the script, while when I'm dealing with normal auotencoder, I don't have such issue.

So I think that the problem comes from the generated noised input data. How can I fix the values of this noised data input once for all.

Here is how I generated the noised input:

mu, sigma = 2, 3
noise_noy = np.random.normal(mu, sigma, [7412,48])
noise_test = np.random.normal(mu, sigma, [3600,48])
noised_noyau_y = df_noyau_yes + noise_noy
noised_test_y = df_test_yes + noise_test
print(noised_noyau_y)

And this the denoising autoencoder

checkpointer = ModelCheckpoint(filepath="modelyes.h5",
                               verbose=0,
                               save_best_only=True,
                               save_weights_only=True)
tensorboard = TensorBoard(log_dir='/tmp/autoencoder',
                          histogram_freq=0,
                          write_graph=True,
                          write_images=True)
input_enc = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size1, activation='relu')(input_enc)
hidden_11 = Dense(hidden_size2, activation='relu')(hidden_1)
code = Dense(code_size, activation='relu')(hidden_11)
hidden_22 = Dense(hidden_size2, activation='relu')(code)
hidden_2 = Dense(hidden_size1, activation='relu')(hidden_22)
output_enc = Dense(input_size, activation='tanh')(hidden_2)
D_autoencoder_yes = Model(input_enc, output_enc)
D_autoencoder_yes.compile(optimizer='adam',
                         loss='mean_squared_error', 
                         metrics=['accuracy'])
history_D_yes = D_autoencoder_yes.fit(df_noised_noy_norm_y, df_noyau_norm_y,
                               epochs=200,
                                batch_size=batch_size,
                                shuffle = True,
                                validation_data=(df_noised_test_norm_y, df_test_norm_y),
                                verbose=1, 
                                callbacks=[checkpointer, tensorboard]).history
D_autoencoder_yes.save_weights("modelyes.h5")
D_autoencoder_yes.load_weights("modelyes.h5")

generating the encoded data

encoder_yes = Model (inputs = input_enc,outputs = code)
encoded_input = Input(shape=(code_size, ))
encoded_data_yes = encoder_yes.predict(df_noised_noy_norm_y)
print("Encoded representations of samples belonging to class YES",encoded_data_yes)
Mari
  • 69
  • 1
  • 8
  • If you want to get a "constant" random input, you might want to use `numpy.random.seed` with a constant, see for instance: https://stackoverflow.com/a/21494630/4121573 – Adonis Apr 05 '18 at 12:37

1 Answers1

0

So you want to have the same "random" input each time?

Lock the random generator by calling np.random.seed(x) with an integer x before generating your random arrays. Note that x can be any integer as long as it's the same each time

np.random.seed(42)
noise_noy_1 = np.random.normal(mu, sigma, [7412,48])

np.random.seed(42)
noise_noy_2 = np.random.normal(mu, sigma, [7412,48])

if (noise_noy_1 == noise_noy_2).all():
    print('Equal')
NLindros
  • 1,683
  • 15
  • 15