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()))
Call tf. keras. Model. save to save a model's architecture, weights, and training configuration in a single file/folder .
Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With