stay Keras in , Sometimes models need to be serialized and deserialized . When serializing a model , The model results and model weights are saved in different files , Model weights are usually saved in the HDF5 In the file , The structure of the model can be saved in the JSON perhaps YAML In the file . The latter two methods are similar , Here is the JSON Let's take an example Keras Saving and loading model .
from sklearn import datasets import numpy as np from keras.models import
Sequential from keras.layers import Dense from keras.utils import
to_categorical from keras.models import model_from_json # Import data dataset =
datasets.load_iris() x = Y = # Convert label data to classification code Y_labels
= to_categorical(Y, num_classes=3) # Set random seed seed = 7 np.random.seed(seed)
# Building model functions def create_model(optimizer = 'rmsprop', init = 'glorot_uniform'): # Building models
model = Sequential() model.add(Dense(units=4, activation='relu', input_dim=4,
kernel_initializer=init)) model.add(Dense(units=6, activation='relu',
kernel_initializer=init)) model.add(Dense(units=3, activation='softmax',
kernel_initializer=init)) # Compiling model model.compile(loss='categorical_crossentropy',
optimizer=optimizer, metrics=['accuracy']) return model # Building models model =
create_model(),Y_labels, epochs=200, batch_size=5, verbose=0)
scores = model.evaluate(x,Y_labels, verbose=0) print('%s: %.2f%%' %
(model.metrics_names[1], scores[1]*100)) # Model preservation JSON file model_json =
model.to_json() with
open(r'F:\Python\pycharm\keras_deeplearning\model\modle.json', 'w') as file:
file.write(model_json) # Save model weight values model.save_weights('model.json.h5')
# from JSON Load model in file with
open(r'F:\Python\pycharm\keras_deeplearning\model\modle.json', 'r') as file:
model_json1 = # Load model new_model = model_from_json(model_json1)
new_model.load_weights('model.json.h5') # Compiling model
optimizer='rmsprop',metrics=['accuracy']) # Evaluate the loaded model scores1 =
new_model.evaluate(x,Y_labels,verbose=0) print('%s: %.2f%%' %
(model.metrics_names[1], scores1[1]*100))
After the new model is built by loading the model , The model must be compiled first , Then, the new data is predicted by the loaded model .

there JSON The contents of the document are as follows :
{"class_name": "Sequential", "config": [{"class_name": "Dense", "config":
{"name": "dense_1", "trainable": true, "batch_input_shape": [null, 4], "dtype":
"float32", "units": 4, "activation": "relu", "use_bias": true,
"kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale":
1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
"bias_initializer": {"class_name": "Zeros", "config": {}},
"kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer":
null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name":
"Dense", "config": {"name": "dense_2", "trainable": true, "units": 6,
"activation": "relu", "use_bias": true, "kernel_initializer": {"class_name":
"VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution":
"uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros",
"config": {}}, "kernel_regularizer": null, "bias_regularizer": null,
"activity_regularizer": null, "kernel_constraint": null, "bias_constraint":
null}}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable":
true, "units": 3, "activation": "softmax", "use_bias": true,
"kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale":
1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
"bias_initializer": {"class_name": "Zeros", "config": {}},
"kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer":
null, "kernel_constraint": null, "bias_constraint": null}}], "keras_version":
"2.2.2", "backend": "tensorflow"}




©2019-2020 Toolsou All rights reserved,
c Language to achieve mine clearance Games and mine source code Node.js Middle template engine 3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! The situation of receiving and receiving multi-path git It's set correctly ssh, But it still prompts Permission denied (publickey) new iPhone I won't support it 5G Will lead to further decline in shipment C# Sorting method of Chinese Dictionaries 10909 rice ! Chinese fighter successfully landed in Mariana Trench MySQL8.0MGR Single owner / Multi master installation and switching Java Poor reflection performance ?