Joh*_*her 3 python machine-learning scikit-learn keras tensorflow
如何在训练后保存模型重量?
Keras提供:
model.save( 'weights.h5')`
模型对象由build_fn属性函数初始化,如何进行保存?
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()))
Run Code Online (Sandbox Code Playgroud)
Viv*_*mar 10
cross_val_score克隆提供的估算器,将它们放在训练折叠上,在测试折叠上得分.基本上,你的实际模型尚未安装.
首先,您需要在数据上拟合模型:
model.fit(X, Y)
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用基础model属性(实际存储keras模型)来调用save()or save_weights()方法.
model.model.save('saved_model.h5')
Run Code Online (Sandbox Code Playgroud)
现在,当您想再次加载模型时,请执行以下操作:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1751 次 |
| 最近记录: |