使用Keras回调函数保存最佳验证损失

我基于https://blog.keras.io/building-autoencoders-in-keras.html中的讨论,构建了一个用于mnist数据集的去噪自编码器。

我试图观察输入图像重建随时间的变化;我注意到有时DAE的损失会突然增加(无论是训练还是验证),例如从约0.12的损失增加到约3.0。为了避免在训练过程中使用这些“失误”,我尝试使用Keras的回调函数,保存最佳权重(基于验证损失),并在每次“段”训练后加载它们(在我这里,每段训练为10个epoch)。

然而,我收到了一个错误消息:

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, Densefrom keras.models import Modelfrom keras import regularizersfrom keras.callbacks import ModelCheckpointinput_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 mnistimport 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.shapeprint x_test.shapenoise_factor = 0.5x_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需要的是列表而不是元组,试试这样:

callbacks=[checkpointer]

我还注意到你的checkpointer后面有一个逗号,你也应该删除它。

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注