I would like to make a deep copy of a keras model (called model1
) of mine in order to be able to use it in a for a loop and then re-initialize for each for-loop iteration and perform fit
with one additional sample to the model. I would like to be able to initialize the model after each iteration since after performing the fit
(my model is modified however, I want it keep it as it is when i am loading from the path using the load_weights).
My code looks like:
model1= create_Model()
model1.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model1.load_weights('my_weights')
model_copy= create_Model()
model_copy.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model_copy= keras.models.clone_model(model1)
for j in range(0, image_size):
model_copy.fit(sample[j], sample_lbl[j])
prediction= model_copy.predict(sample[j])
Also, it is not really efficient for me to load the model each time in the for-loop since that is time-consuming. How can I do properly the deep copy in my case? The code I posted give the following error that concerns the function .fit and my reference model model_copy:
RuntimeError: You must compile a model before training/testing. Use
model.compile(optimizer, loss)
.