Keras ML库:梯度更新后如何进行权重裁剪? TensorFlow后端 [英] Keras ML library: how to do weight clipping after gradient updates? TensorFlow backend

查看:891
本文介绍了Keras ML库:梯度更新后如何进行权重裁剪? TensorFlow后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras来实现需要权重裁剪的算法的一部分,即在梯度更新后限制权重值.到目前为止,我还没有通过网络搜索找到任何解决方案.

I'm trying to use Keras to implement part of an algorithm that requires weight clipping, i.e. limiting the weight values after a gradient update. I haven't found any solutions through web searches so far.

对于背景,这与WGANs算法有关:

For background, this has to do with the WGANs algorithm:

https://arxiv.org/pdf/1701.07875.pdf

如果您查看第8页的算法1,则会看到以下内容:

If you look at algorithm 1 on page 8, you'll see the following:

我突出了我要在Keras中实现的行:在计算了用于更新网络中权重的渐变之后,我要确保所有权重都被限制在某些值之间[-c ,c]我可以设定.

I've highlighted the lines that I'm trying to implement in Keras: after computing a gradient to use to update the weights in the network, I want to make sure that all the weights are clipped between some values [-c, c] that I can set.

我该如何在Keras中进行此操作?

How could I go about doing this in Keras?

作为参考,我正在使用TensorFlow后端.我现在不介意深入研究并添加凌乱的快速修复程序.

For reference I am using the TensorFlow backend. I don't mind digging into things and adding messy quick-fixes for now.

推荐答案

在创建优化程序对象集参数clipvalue时. 它将精确地完成您想要的.

While creating the optimizer object set param clipvalue. It will do precisely what you want.

# all parameter gradients will be clipped to
# a maximum value of 0.5 and
# a minimum value of -0.5.
rsmprop = RMSprop(clipvalue=0.5)

然后使用该对象进行模型编译

and then use this object to for model compiling

model.compile(loss='mse', optimizer=rsmprop)

有关更多参考信息,请检查:此处.

For more reference check: here.

此外,我更喜欢使用clipnorm而不是clipvalue,因为使用clipnorm时,优化保持稳定.例如,假设您有2个参数,并且渐变为[0.1, 3].通过使用clipvalue,梯度将变为[0.1,0.5],即最陡的体面方向可能会急剧改变.尽管clipnorm没有类似的问题,因为将适当缩放所有梯度并保留方向,并且始终确保对梯度大小的约束.

Also, I prefer to use clipnorm over clipvalue because with clipnorm the optimization remains stable. For example say you have 2 parameters and the gradients came out to be [0.1, 3]. By using clipvalue the gradients will become [0.1, 0.5] ie there are chances that the direction of steepest decent can get changed drastically. While clipnorm don't have similar problem as all the gradients will be appropriately scaled and the direction will be preserved and all the while ensuring the constraint on the magnitude of the gradient.

问题是权重裁剪而不是梯度裁剪:

渐变权重裁剪不是keras代码的一部分.但是maxnorm在权重约束上是.在此处.

Gradiant clipping on weights is not part of keras code. But maxnorm on weights constraints is. Check here.

话虽如此,它可以轻松实现.这是一个非常小的示例:

Having said that it can be easily implemented. Here is a very small example:

from keras.constraints import Constraint
from keras import backend as K

class WeightClip(Constraint):
    '''Clips the weights incident to each hidden unit to be inside a range
    '''
    def __init__(self, c=2):
        self.c = c

    def __call__(self, p):
        return K.clip(p, -self.c, self.c)

    def get_config(self):
        return {'name': self.__class__.__name__,
                'c': self.c}

import numpy as np
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(30, input_dim=100, W_constraint = WeightClip(2)))
model.add(Dense(1))

model.compile(loss='mse', optimizer='rmsprop')

X = np.random.random((1000,100))
Y = np.random.random((1000,1))

model.fit(X,Y)

我已经测试了上面代码的运行,但没有测试约束的有效性.您可以通过在使用model.get_weights()model.layers[idx].get_weights()进行训练后获得模型权重并检查其是否遵守约束来做到这一点.

I have tested the running of the above code, but not the validity of the constraints. You can do so by getting the model weights after training using model.get_weights() or model.layers[idx].get_weights() and checking whether its abiding the constraints.

注意:约束未添加到所有模型权重..而是仅添加到其使用的特定层的权重,并且W_constraint还将约束添加到W参数,将b_constraint添加到b(偏差)param

Note: The constrain is not added to all the model weights .. but just to the weights of the specific layer its used and also W_constraint adds constrain to W param and b_constraint to b (bias) param

这篇关于Keras ML库:梯度更新后如何进行权重裁剪? TensorFlow后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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