Keras的Checkpoint深度学习模型

时间:2017-01-30 13:59:44

标签: python keras

我需要帮助在Keras中实现检查点功能。我将训练一个大型数据集,为了做到这一点,我首先使用虹膜花数据集训练了一个模型:http://machinelearningmastery.com/multi-class-classification-tutorial-keras-deep-learning-library/

因为我自己的数据集与它很相似,所以不同之处在于我的数据集更大。

对于检查点功能:http://machinelearningmastery.com/check-point-deep-learning-models-keras/

我理解使用pima-indians数据集的示例。 现在我试图在iris-flower脚本中实现相同的检查点功能。这是我到目前为止所尝试的内容。

import numpy
from pandas import *
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score, KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from keras.callbacks import ModelCheckpoint

seed = 7
numpy.random.seed(seed)

dataframe = read_csv("iris.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]

# encode class value as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
dummy_y = np_utils.to_categorical(encoded_Y)

def baseline_model():
    model = Sequential()
    model.add(Dense(4, input_dim=4, init='normal', activation='relu'))
    model.add(Dense(3, init='normal', activation='sigmoid'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

estimator = KerasClassifier(build_fn=baseline_model, validation_split=0.33, nb_epoch=200, batch_size=5, callbacks=callbacks_list, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

此脚本产生以下错误。我不知道如何解决它,或者我在脚本中的安排是错误的。

RuntimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x10e120fd0>, as the constructor does not seem to set parameter callbacks

我希望有人可以帮助我。谢谢。

3 个答案:

答案 0 :(得分:2)

我认为问题在于你的baseline_model()函数没有返回它正在创建的模型;它应该是这样的:

def baseline_model():
    model = Sequential()
    model.add(Dense(4, input_dim=4, init='normal', activation='relu'))
    model.add(Dense(3, init='normal', activation='sigmoid'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

答案 1 :(得分:0)

使用模型本身而不是KerasClassifier。

model = baseline_model()
#declare your callback methods here
model.fit(x,y, batch_size=32, verbose=0, epochs=10, shuffle=True, validation_split = 0.1, callbacks = <your list of callbacks>)

答案 2 :(得分:0)

我有同样的错误,但是在NN层中设置了'units'参数。

RuntimeError: Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier object at 0x1496b97f0>, as the constructor either does not set or modifies parameter units

将scikit-learn从0.23.1降级到0.21.2为我解决了这个问题。

在github上查看此问题:Link