keras中如何保存model的数据,以及如何利用保存的数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zjm750617105/article/details/51321879

本文利用的demo的是之前写好的: addition_lstm.py, 详细的源代码请看: http://blog.csdn.net/zjm750617105/article/details/51321889

json_string = model.to_json()  #等价于 json_string = model.get_config()
open('my_model_architecture.json','w').write(json_string)  
model.save_weights('my_model_weights.h5')  
#加载模型数据和weights
model = model_from_json(open('my_model_architecture.json').read())  
model.load_weights('my_model_weights.h5')  
a = model.get_config()
print("model config is ",a)
'''
('model config is ', [
{'class_name': 'LSTM', 'config': {'inner_activation': 'hard_sigmoid', 'trainable': True, 'inner_init': 'orthogonal', 'output_dim': 128, 'unroll': False, 'consume_less': 'cpu', 'init': 'glorot_uniform', 'dropout_U': 0.0, 'input_dtype': 'float32', 'batch_input_shape': (None, 7, 12), 'input_length': None, 'dropout_W': 0.0, 'activation': 'tanh', 'stateful': False, 'b_regularizer': None, 'U_regularizer': None, 'name': 'lstm_1', 'go_backwards': False, 'input_dim': 12, 'return_sequences': False, 'W_regularizer': None, 'forget_bias_init': 'one'}},
{'class_name': 'RepeatVector', 'config': {'trainable': True, 'name': 'repeatvector_1', 'n': 4}},
{'class_name': 'LSTM', 'config': {'U_regularizer': None, 'name': 'lstm_2', 'inner_activation': 'hard_sigmoid', 'go_backwards': False, 'activation': 'tanh', 'trainable': True, 'unroll': False, 'consume_less': 'cpu', 'stateful': False, 'init': 'glorot_uniform', 'inner_init': 'orthogonal', 'dropout_U': 0.0, 'dropout_W': 0.0, 'input_dim': 128, 'return_sequences': True, 'b_regularizer': None, 'W_regularizer': None, 'output_dim': 128, 'forget_bias_init': 'one', 'input_length': None}},
{'class_name': 'TimeDistributedDense', 'config': {'input_length': None, 'W_constraint': None, 'b_constraint': None, 'name': 'timedistributeddense_1', 'activity_regularizer': None, 'trainable': True, 'init': 'glorot_uniform', 'input_dim': None, 'b_regularizer': None, 'W_regularizer': None, 'activation': 'linear', 'output_dim': 12}},
{'class_name': 'Activation', 'config': {'activation': 'sigmoid', 'trainable': True, 'name': 'activation_1'}}
])
'''

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Note that you will first need to install HDF5 and the Python library h5py, which do not come bundled with Keras.</span>

需要安装HDF5 和  h5py , 没有安装成功





展开阅读全文

没有更多推荐了,返回首页