用keras回调保存最佳的val_loss



我基于此处讨论的内容,为MNIST数据集提供了一个DeNoising AutoCoder:https://blog.keras.io/building-autoencoders-in-keras.html

我正在尝试查看输入图像的重建如何随时间变化。我注意到有时DAE尖峰的损失(无论是用于训练还是用于验证),例如从〜0.12损失到〜3.0。为了避免在培训过程中使用这些"失误",我正在尝试使用Keras的回调,节省了最佳权重(val_loss Wise)并在每个训练的"段"之后加载它们(我的情况下为10个时代)。p>但是,我收到一条错误消息:

File "noise_e_mini.py", line 71, in <module> callbacks=([checkpointer])) File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1650, in fit validation_steps=validation_steps) File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1145, in _fit_loop callbacks.set_model(callback_model) File "/usr/local/lib/python2.7/dist-packages/keras/callbacks.py", line 48, in set_model callback.set_model(model) AttributeError: 'tuple' object has no attribute 'set_model'

我的代码是:

from keras.layers import Input, Dense
from keras.models import Model
from keras import regularizers
from keras.callbacks import ModelCheckpoint
input_img = Input(shape=(784,))
filepath_for_w='denoise_by_AE_weights_1.h5'

def autoencoder_block(input,l1_score_encode,l1_score_decode):

    #    encoder:
    encoded = Dense(256, activation='relu',activity_regularizer=regularizers.l1(l1_score_encode))(input_img)
    encoded = Dense(128, activation='relu',activity_regularizer=regularizers.l1(l1_score_encode))(encoded)
    encoded = Dense(64, activation='relu',activity_regularizer=regularizers.l1(l1_score_encode))(encoded)
    encoded = Dense(32, activation='relu',activity_regularizer=regularizers.l1(l1_score_encode))(encoded)
    encoder = Model (input=input_img, output=encoded)
    #    decoder:
    connection_layer= Input(shape=(32,))
    decoded = Dense(64, activation='relu',activity_regularizer=regularizers.l1(l1_score_decode))(connection_layer)
    decoded = Dense(128, activation='relu',activity_regularizer=regularizers.l1(l1_score_decode))(decoded)
    decoded = Dense(256, activation='relu',activity_regularizer=regularizers.l1(l1_score_decode))(decoded)
    decoded = Dense(784, activation='sigmoid',activity_regularizer=regularizers.l1(l1_score_decode))(decoded)
    decoder = Model (input=connection_layer , output=decoded)
    crunched = encoder(input_img)
    final = decoder(crunched)
    autoencoder = Model(input=input_img, output=final)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    return (autoencoder)

from keras.datasets import mnist
import numpy as np
(x_train, y_train), (x_test, y_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
noise_factor = 0.5
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 
x_test_noisy = np.clip(x_test_noisy, 0., 1.)

autoencoder=autoencoder_block(input_img,0,0)
for i in range (10):
    x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 
    x_train_noisy = np.clip(x_train_noisy, 0., 1.)
    checkpointer=ModelCheckpoint(filepath_for_w, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=1),
    autoencoder.fit(x_train_noisy, x_train,
                epochs=10,
                batch_size=256,            
                shuffle=True,
                validation_data=(x_test_noisy, x_test),
                callbacks=([checkpointer]))
    autoencoder.load_weights(filepath_for_w)  # load weights from the best in the run
    decoded_imgs = autoencoder.predict(x_test_noisy) # save results for this stage for presentation
    np.save('decoded'+str(i)+'.npy',decoded_imgs)    ####
np.save('tested.npy',x_test_noisy)
np.save ('true_catagories.npy',y_test)
np.save('original.npy',x_test)

autoencoder.save('denoise_by_AE_model_1.h5')

我在做什么错?非常感谢:)

您的问题可能在此行中

callbacks=([checkpointer]))

您需要删除括号作为回调需求列表,而不是元组,尝试:

callbacks=[checkpointer]

我还注意到您的检查台上以逗号结束,您也应该删除。

checkpointer=ModelCheckpoint(filepath_for_w, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=1),

相关内容

  • 没有找到相关文章

最新更新