How can I save an object containing keras models?

情到浓时终转凉″ 提交于 2021-02-19 03:33:07

问题


Here is my code skeleton:

def build_model(x, y):
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(1, activation='relu'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(x, y)
    return model

class MultiModel(object):
    def __init__(self, x1, y1, x2, y2):
        self.model1 = build_model(x1, y1)
        self.model2 = build_model(x2, y2)

if __name__ == '__main__':
    # code that builds x1, x2, y1, y2
    mm = MultiModel(x1, y1, x2, y2)  # How to save mm ?

The issue is I don't know how to save the mm object that contains several Keras models.

The Keras built-in save method enables only to save Keras model, so it is unusable in that case. The pickle module can not save _thread.RLock objects, so it is also unusable.

It is maybe possible to save each model independently with the Keras save method, then to regroup them and to save them as a whole. But I do not know how to proceed.

Any ideas ?

来源:https://stackoverflow.com/questions/52591862/how-can-i-save-an-object-containing-keras-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!