简体   繁体   中英

How To Save Keras Regressor Model?

How can I save models weights after training?

Keras provides:

model.save('weights.h5')`

The model object is initialized by the build_fn attribute function, how is the save done?

def model():
    model = Sequential()
    model.add(Dense(10, activation='relu', input_dim=5))
    model.add(Dense(5, activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


if __name__ == '__main__':
`

    X, Y = process_data()

    print('Dataset Samples: {}'.format(len(Y)))

    model = KerasRegressor(build_fn=model,
            epochs=10,
            batch_size=10,
            verbose=1)


    kfold = KFold(n_splits=2, random_state=seed)

    results = cross_val_score(model, X, Y, cv=kfold)

    print('Results: {0}.2f ({1}.2f MSE'.format(results.mean(), results.std()))

cross_val_score clones the supplied estimator, fits them on training fold, scores on test fold. So essentially, your actual model hasnt been fitted yet.

So first you need to fit the model on the data:

model.fit(X, Y)

And then you can use the underlying model attribute (which actually stores the keras model) to call the save() or save_weights() method.

model.model.save('saved_model.h5')

Now when you want to load the model again, do this:

from keras.models import load_model

# Instantiate the model as you please (we are not going to use this)
model2 = KerasRegressor(build_fn=model_build_fn, epochs=10, batch_size=10, verbose=1)

# This is where you load the actual saved model into new variable.
model2.model = load_model('hh.h5')

# Now you can use this to predict on new data (without fitting model2, because it uses the older saved model)
model2.predict(X)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM