如何在.h5和json中保存整体机器学习模型?

时间:2019-11-22 08:52:01

标签: python keras

保存Ensemble模型时出现问题。就我而言,我已经通过keras库制作了LSTM模型,然后使用scikit的BaggingRegressor学习了如何制作健壮的模型。然后需要保存整体包装,但是找不到方法将{h}和json文件保存为https://machinelearningmastery.com/save-load-keras-deep-learning-models/

上的教程

我的代码如下

from tensorflow.keras.layers import Dense, Dropout, Flatten, LSTM, BatchNormalization
from sklearn.ensemble import BaggingRegressor

def model_base_LSTM(inputshape): # inputshape normally x_train.shape[2] or x_train.shape[1]

    model_ = Sequential([

    # Make layers
    LSTM(100, return_sequences=True,input_shape=(1,inputshape)),
    BatchNormalization(),
    Dropout(0.15),
    LSTM(100, return_sequences=True),
    BatchNormalization(),
    Dropout(0.15),
    BatchNormalization(),
    Flatten(),
    # Output layer
    Dense(1)
    ])
    # Compile
    model_.compile(optimizer = 'adam', loss = 'mean_squared_error')

    return model_

model = MyKerasRegressor(build_fn = model_base_LSTM, epochs=20, batch_size =50, validation_split=0.3, 
                         inputshape = x_train.shape[1])

bag_mod = BaggingRegressor(base_estimator=model, n_estimators=5)
bag_mod_train = bag_mod.fit(x_train, y_train.ravel())

训练模型之后,我想保存'bag_mod'模型,但是scikit学习保存模型的方式与keras不同(如链接所示)。希望同时保存在.h5和.json文件中。那么,如何保存此训练后的模型以供下次使用更新数据进行训练?

非常感谢!

0 个答案:

没有答案