有没有办法减少RMSProp的代码量

时间:2016-09-07 16:31:39

标签: python

我有一些简单的递归神经网络的代码,想知道是否有办法减少更新阶段所需的代码量。我有这样的代码:

class RNN(object):
    def__init___(self, data, hidden_size, eps=0.0001):
        self.data = data
        self.hidden_size = hidden_size
        self.weights_hidden = np.random.rand(hidden_size, hidden_size) * 0.1 # W
        self.weights_input = np.random.rand(hidden_size, len(data[0])) * 0.1 # U
        self.weights_output = np.random.rand(len(data[0]), hidden_size) * 0.1 # V 
        self.bias_hidden = np.array([np.random.rand(hidden_size)]).T  # b
        self.bias_output = np.array([np.random.rand(len(data[0]))]).T # c

        self.cache_w_hid, self.cache_w_in, self.cache_w_out = 0, 0, 0
        self.cache_b_hid, self.cache_b_out = 0, 0
        self.eps = eps

    def train(self, seq_length, epochs, eta, decay_rate=0.9, learning_decay=0.0):
         # Other stuff
         self.update(seq, epoch, eta, decay_rate, learning_decay)
         # Other Stuff

    def update(self, seq, epoch, eta, decay_rate, learning_decay):
        """Updates the network's weights and biases by applying gradient
        descent using backpropagation through time and RMSPROP. 
        """
        delta_nabla_c, delta_nabla_b,\
        delta_nabla_V, delta_nabla_W, delta_nabla_U = self.backward_pass(seq)

        eta = eta*np.exp(-epoch*learning_decay)

        self.cache_w_hid += decay_rate * self.cache_w_hid \
                           + (1 - decay_rate) * delta_nabla_W**2
        self.weights_hidden -= eta * delta_nabla_W / (np.sqrt(self.cache_w_hid) + self.eps)

        self.cache_w_in += decay_rate * self.cache_w_in \
                          + (1 - decay_rate) * delta_nabla_U**2         
        self.weights_input -= eta * delta_nabla_U / (np.sqrt(self.cache_w_in) + self.eps)

        self.cache_w_out += decay_rate * self.cache_w_out \
                           + (1 - decay_rate) * delta_nabla_V**2
        self.weights_output -= eta * delta_nabla_V / (np.sqrt(self.cache_w_out) + self.eps)

        self.cache_b_hid += decay_rate * self.cache_b_hid \
                          + (1 - decay_rate) * delta_nabla_b**2
        self.bias_hidden -= eta * delta_nabla_b / (np.sqrt(self.cache_b_hid) + self.eps)

        self.cache_b_out += decay_rate * self.cache_b_out \
                          + (1 - decay_rate) * delta_nabla_c**2
        self.bias_output -= eta * delta_nabla_c / (np.sqrt(self.cache_b_out) + self.eps)

对于#RMSProp下的每个变量,遵循更新规则,即:

cache = decay_rate * cache + (1 - decay_rate) * dx**2
x += - learning_rate * dx / (np.sqrt(cache) + eps)

我已cache_所有声明后跟self.weight_self.bias_,并希望更紧凑地编写。我正在考虑使用zip(),但我不知道如何去做。

1 个答案:

答案 0 :(得分:1)

从你的问题来看,我猜你在这里试图提高可读性/优雅性。

您可以引入一个函数来实现更新规则,然后为每个变量调用一次。这里的技巧是Python允许您按名称访问属性,因此您可以传入缓存和权重属性的名称而不是值。这将允许您更新未来通行证的值:

def update_rule(self, cache_attr, x_attr, decay_rate, learning_rate, dx):
    cache = getattr(self, cache_attr)
    cache = decay_rate * cache + (1 - decay_rate) * dx**2
    setattr(self, cache_attr, cache)

    x = getattr(self, x_attr)
    x += - learning_rate * dx / (np.sqrt(cache) + self.eps)
    setattr(self, x_attr, x)

def update(self, seq, epoch, eta, decay_rate, learning_decay):
    """Updates the network's weights and biases by applying gradient
    descent using backpropagation through time and RMSPROP. 
    """
    delta_nabla_c, delta_nabla_b,\
    delta_nabla_V, delta_nabla_W, delta_nabla_U = self.backward_pass(seq)

    eta = eta*np.exp(-epoch*learning_decay)

    self.update_rule('cache_w_hid', 'weights_hidden', decay_rate, eta, delta_nabla_W)
    self.update_rule('cache_w_in', 'weights_input', decay_rate, eta, delta_nabla_U)
    self.update_rule('cache_w_out', 'weights_output', decay_rate, eta, delta_nabla_V)
    self.update_rule('cache_b_hid', 'bias_hidden', decay_rate, eta, delta_nabla_b)
    self.update_rule('cache_b_out', 'bias_output', decay_rate, eta, delta_nabla_c)

事实上,您可以保存其他参数,并通过将update_rule放入update来避免暴露基本上属于私有方法的内容。这会在调用时将update的命名空间公开为update_rule,因此您无需传入decay_ratelearning_rate

def update(self, seq, epoch, eta, decay_rate, learning_decay):
    """Updates the network's weights and biases by applying gradient
    descent using backpropagation through time and RMSPROP. 
    """

    def update_rule(cache_attr, x_attr, dx):
        cache = getattr(self, cache_attr)
        cache = decay_rate * cache + (1 - decay_rate) * dx**2
        setattr(self, cache_attr, cache)

        x = getattr(self, x_attr)
        x += - eta * dx / (np.sqrt(cache) + self.eps)
        setattr(self, x_attr, x)

    delta_nabla_c, delta_nabla_b,\
    delta_nabla_V, delta_nabla_W, delta_nabla_U = self.backward_pass(seq)

    eta = eta*np.exp(-epoch*learning_decay)

    update_rule('cache_w_hid', 'weights_hidden', delta_nabla_W)
    update_rule('cache_w_in', 'weights_input', delta_nabla_U)
    update_rule('cache_w_out', 'weights_output', delta_nabla_V)
    update_rule('cache_b_hid', 'bias_hidden', delta_nabla_b)
    update_rule('cache_b_out', 'bias_output', delta_nabla_c)

最后,如果您真的想要,可以使用zip将对update_rule的调用放入循环中。请注意,对于此版本,调用的顺序已更改为与self.backward_pass返回的值的顺序相匹配。我个人不会使用这个最后版本,除非你真的有很多更新要做,因为除了backward_pass的结果非常敏感之外,它开始看起来模糊不清。

def update(self, seq, epoch, eta, decay_rate, learning_decay):
    """Updates the network's weights and biases by applying gradient
    descent using backpropagation through time and RMSPROP. 
    """

    def update_rule(cache_attr, x_attr, dx):
        cache = getattr(self, cache_attr)
        cache = decay_rate * cache + (1 - decay_rate) * dx**2
        setattr(self, cache_attr, cache)

        x = getattr(self, x_attr)
        x += - eta * dx / (np.sqrt(cache) + self.eps)
        setattr(self, x_attr, x)

    dx = self.backward_pass(seq)

    eta = eta*np.exp(-epoch*learning_decay)

    cache_attrs = ('cache_b_out', 'cache_b_hid', 'cache_w_out', 'cache_w_hid', 'cache_w_in')
    x_attrs = ('bias_output', 'bias_hidden', 'weights_output', 'weights_hidden', 'weights_input')

    for args in zip(cache_attrs, x_attrs, dx):
        update_rule(*args)