问题
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