'KerasClassifier' object has no attribute 'save'

时间:2019-04-23 15:06:11

标签: keras neural-network deep-learning

I have trained a model on colab using artifical neural network (keras) and in the end I want to save it but getting error. I have tried pydrive method also. I have all the required library previously when I run it on local computer it worked.

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json

 model = Sequential()                                      # create model
 model.add(Dense(6, input_dim = 8, activation = 'relu'))
 model.add(Dense(6, activation = 'relu'))                  # hidden layer
 model.add(Dense(1, activation = 'sigmoid'))               # output layer
 model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 model.fit(X_train, y_train, epochs=100, batch_size=10)

def build_classifier():

    model = Sequential()                                      # create model
    model.add(Dense(6, input_dim = 8, activation = 'relu'))
    model.add(Dense(6, activation = 'relu'))                  # hidden layer
    model.add(Dense(1, activation = 'sigmoid'))               # output layer
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model  

model = KerasClassifier(build_fn = build_classifier, epochs=100, batch_size=32)
accuracies = cross_val_score(estimator = model,X = X_train, y = y_train, cv = 10, n_jobs = -1)

model.save("model.h5")

but getting error 'KerasClassifier' object has no attribute 'save'. does google colab required different method to save model?

enter image description here

1 个答案:

答案 0 :(得分:-1)

'KerasClassifier'对象没有属性'save'

发生错误是因为我试图保存KerasClassifier(交叉验证功能),因为交叉验证的目的是模型检查,而不是模型构建。参数模型将被保存。感谢@MatiasValdenegro洞察力。

更好的解释可在

https://stats.stackexchange.com/questions/52274/how-to-choose-a-predictive-model-after-k-fold-cross-validation