python-kerasHow do I save a Keras model as an H5 file in Python?
To save a Keras model as an H5 file in Python, you need to use the model.save()
method. This method takes the path to the file as an argument and saves the model as an HDF5 file. The following example code saves a Keras model to an H5 file:
from keras.models import Sequential
from keras.layers import Dense
# Create the model
model = Sequential()
model.add(Dense(2, input_dim=3, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Save the model as an H5 file
model.save('model.h5')
The above code will save the model as an H5 file named model.h5
in the current working directory.
Code explanation
from keras.models import Sequential
: imports theSequential
model from thekeras.models
module.from keras.layers import Dense
: imports theDense
layer from thekeras.layers
module.model = Sequential()
: creates aSequential
model object.model.add(Dense(2, input_dim=3, activation='relu'))
: adds aDense
layer to the model with 2 nodes, 3 input dimensions and a ReLU activation function.model.add(Dense(1, activation='sigmoid'))
: adds aDense
layer to the model with 1 node and a sigmoid activation function.model.save('model.h5')
: saves the model as an H5 file namedmodel.h5
in the current working directory.
Helpful links
More of Python Keras
- How can I use XGBoost, Python and Keras together to build a machine learning model?
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How do I use validation_data when creating a Keras model in Python?
- How do I use Python and Keras to create a tutorial?
- How do I check which version of Keras I am using in Python?
- How do I use Python Keras to zip a file?
- How do I set the input shape when using Keras with Python?
- How do I use Python's tf.keras.utils.get_file to retrieve a file?
- How do I use TensorFlow, Python, Keras, and utils to_categorical?
- How do I use keras.utils.to_categorical in Python?
See more codes...