Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a deep copy of a keras model in python

Tags:

python

keras

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).

like image 650
Jose Ramon Avatar asked Jan 25 '19 14:01

Jose Ramon


People also ask

Is it possible to copy a keras model with TensorFlow?

I want to replicate the DQN paper in which you freeze the target model and only after several step of iterations, you copy the temporal model to the target model. Thanks! Early January, I can tell you it was possible to deep copy a keras model with theano backend. However, it was not possible to deep copy one with tensorflow backend. #1466

What is Keras deep learning?

This Keras tutorial introduces you to deep learning in Python: learn to preprocess your data, model, evaluate and optimize neural networks. Deep Learning By now, you might already know machine learning, a branch in computer science that studies the design of algorithms that can learn.

What is the difference between copy () and deepcopy () in Python?

li2 = copy.copy (li1) li3 = copy.deepcopy (li1) In the above code, the copy () returns a shallow copy of list and deepcopy () return a deep copy of list.

How to do shallow and deep copy in Python?

We use the copy module of Python for shallow and deep copy operations. Suppose, you need to copy the compound list say x. For example: Here, the copy () return a shallow copy of x.


2 Answers

The issue is that model_copy is probably not compiled after cloning. There are in fact a few issues:

  1. Apparently cloning doesn't copy over the loss function, optimizer info, etc.

  2. Before compiling you need to also build the model.

  3. Moreover, cloning doesn't copy weight over

So you need a couple extra lines after cloning. For example, for 10 input variables:

model_copy= keras.models.clone_model(model1)
model_copy.build((None, 10)) # replace 10 with number of variables in input layer
model_copy.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model_copy.set_weights(model.get_weights())


Easier Method 1: Loading weights from file

If I understand your question correctly, there is an easier way to do this. You don't need to clone the model, just need to save the old_weights and set the weights at beginning of the loop. You can simply load weights from file as you are doing.

for _ in range(10):
    model1= create_Model()
    model1.compile(optimizer='rmsprop', loss='categorical_crossentropy')
    model1.load_weights('my_weights')

    for j in range(0, image_size):
          model1.fit(sample[j], sample_lbl[j])
          prediction= model1.predict(sample[j])

Easier Method 2: Loading weights from previous get_weights()

Or if you prefer not to load from file:

model1= create_Model()
model1.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model1.load_weights('my_weights')
old_weights = model1.get_weights()

for _ in range(10):
    model1.set_weights(old_weights)
    for j in range(0, image_size):
          model1.fit(sample[j], sample_lbl[j])
          prediction= model1.predict(sample[j])
like image 144
Tim Avatar answered Oct 29 '22 01:10

Tim


These days it's trivial:

model2 = tf.keras.models.clone_model(model1)

This will give you a new model, new layers, and new weights. If for some reason that doesn't work (I haven't tested it) this older solution will:

model1 = Model(...)
model1.compile(...)
model1.save(savepath) # saves compiled state
model2 = keras.models.load_model(savepath)
like image 31
markemus Avatar answered Oct 29 '22 01:10

markemus