Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically freeze weights after compiling model in Keras?

I would like to train a GAN in Keras. My final target is BEGAN, but I'm starting with the simplest one. Understanding how to freeze weights properly is necessary here and that's what I'm struggling with.

During the generator training time the discriminator weights might not be updated. I would like to freeze and unfreeze discriminator alternately for training generator and discriminator alternately. The problem is that setting trainable parameter to false on discriminator model or even on its' weights doesn't stop model to train (and weights to update). On the other hand when I compile the model after setting trainable to False the weights become unfreezable. I can't compile the model after each iteration because that negates the idea of whole training.

Because of that problem it seems that many Keras implementations are bugged or they work because of some non-intuitive trick in old version or something.

like image 940
Andrzej Pisarek Avatar asked Jul 17 '17 21:07

Andrzej Pisarek


1 Answers

I've tried this example code a couple months ago and it worked: https://github.com/fchollet/keras/blob/master/examples/mnist_acgan.py

It's not the simplest form of GAN, but as far as I remembered, it's not too difficult to remove the classification loss and turn the model into a GAN.

You don't need to turn on/off the discriminator's trainable property and recompile. Simply create and compile two model objects, one with trainable=True (discriminator in the code) and another one with trainable=False (combined in the code).

When you're updating the discriminator, call discriminator.train_on_batch(). When you're updating the generator, call combined.train_on_batch().

like image 134
Yu-Yang Avatar answered Oct 03 '22 04:10

Yu-Yang