在Keras中编译模型后如何动态冻结权重? [英] How to dynamically freeze weights after compiling model in Keras?

查看:105
本文介绍了在Keras中编译模型后如何动态冻结权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Keras训练GAN.我的最终目标是开始,但是我从最简单的目标开始.在这里,有必要正确了解如何冻结体重,这就是我正在努力解决的问题.

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.

在生成器训练期间,鉴别器权重可能不会更新.我想交替 freeze unfreeze 鉴别器,以交替训练生成器和鉴别器.问题在于在 discriminator 模型上甚至在其权重上将 trainable 参数设置为false不会停止模型的训练(以及权重的更新).另一方面,当我将 trainable 设置为False后编译模型时,权重变为 unfreezable .每次迭代后我都无法编译模型,因为这否定了整个训练的想法.

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.

由于这个问题,似乎许多Keras的实现存在错误或由于旧版本中的某些非直观技巧而起作用.

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.

推荐答案

几个月前,我已经尝试过该示例代码,并且可以正常工作: https://github.com/fchollet/keras/blob/master/examples /mnist_acgan.py

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

这不是GAN的最简单形式,但是据我所记得,消除分类损失并将模型转变为GAN并不是太困难.

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.

您不需要打开/关闭鉴别器的可训练属性并重新编译.只需创建并编译两个模型对象,一个使用trainable=True(代码中的discriminator),另一个使用trainable=False(代码中的combined).

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

在更新鉴别器时,请调用discriminator.train_on_batch().在更新生成器时,请调用combined.train_on_batch().

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

这篇关于在Keras中编译模型后如何动态冻结权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆