我正在尝试实现这篇论文中的LSTM优化器:https://arxiv.org/pdf/1606.04474v1.pdf
他们假设关于LSTM参数的梯度导数等于零:
查看我的代码,我认为在优化损失函数时,这个假设并未被使用,因为Theano可以计算这个梯度,并且确实这样做了。我怎样才能阻止它这样做呢?
这是代码:
def step_opt(cell_previous, hid_previous, theta_previous, *args): func = self.func(theta_previous) grad = theano.grad(func, theta_previous) input_n = grad.dimshuffle(0, 'x') cell, hid = step(input_n, cell_previous, hid_previous, *args) # 函数重新计算LSTM的隐藏状态和单元 theta = theta_previous + hid.dot(self.W_hidden_to_output).dimshuffle(0) return cell, hid, theta, funccell_out, hid_out, theta_out, loss_out = theano.scan( fn=step_opt, outputs_info=[cell_init, hid_init, theta_init, None], non_sequences=non_seqs, n_steps=self.n_steps, strict=True)[0]loss = loss_out.sum()
回答:
最终我找到了答案。这里有一个页面:http://deeplearning.net/software/theano/library/gradient.html
我们可以使用disconnected_grad(expr)
来使反向传播在expr
处停止。