![](/img/trans.png)
[英]How to save a Keras Regressor model from Colab to Google Drive?
[英]How To Save Keras Regressor Model?
如何在训练后保存模型重量?
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()))
cross_val_score
克隆所提供的估算器,将它们放在训练折叠上,在测试折叠上得分。 基本上,你的实际模型尚未安装。
首先,您需要在数据上拟合模型:
model.fit(X, Y)
然后,您可以使用底层model
属性(实际存储keras模型)来调用save()
或save_weights()
方法。
model.model.save('saved_model.h5')
现在,当您想再次加载模型时,请执行以下操作:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.