Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras dosen't load the model and weights when using checkpoint

I'm using keras to build a deep autoencoder. I used its checkpointer to load the model and the weights but the result is always None which I think it means that the checkpoint dosen't work correctly and is not saving weights. Here is the code how I proceed:

checkpointer = ModelCheckpoint(filepath="weights.best.h5",
                               verbose=0,
                               save_best_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)
autoencoder_yes = Model(input_enc, output_enc)

autoencoder_yes.compile(optimizer='adam',
                         loss='mean_squared_error', 
                         metrics=['accuracy'])
history_yes = autoencoder_yes.fit(df_noyau_norm_y, df_noyau_norm_y,
                               epochs=200,
                                batch_size=batch_size,
                                shuffle = True,
                                validation_data=(df_test_norm_y, df_test_norm_y),
                                verbose=1, 
                                callbacks=[checkpointer, tensorboard]).history

autoencoder_yes.save_weights("weights.best.h5")
print(autoencoder_yes.load_weights("weights.best.h5"))

Can somebody help me find out a way to resolve the problem? Thanks

like image 383
Mari Avatar asked Nov 25 '25 12:11

Mari


2 Answers

No, your interpretation of load_weights returning None is not correct. Load weights is a procedure, it does not return anything, and if you assign the return value of a procedure to a variable, it will get the value of None.

So weight saving is probably working fine, its just your interpretation that is wrong.

like image 162
Dr. Snoopy Avatar answered Nov 28 '25 01:11

Dr. Snoopy


you should use save_weights_only=True. Without this the whole model is saved not just the weights. To be able to load weights you must save weights like this:

checkpointer = ModelCheckpoint(filepath="weights.best.h5",
                           verbose=0, save_weights_only=True,
                           save_best_only=True)
like image 20
Ioannis Nasios Avatar answered Nov 28 '25 03:11

Ioannis Nasios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!