提交 809d0546 authored 作者: Razvan Pascanu's avatar Razvan Pascanu

test for grad of a cost in terms of grad of a scan

上级 c5f73396
...@@ -2585,6 +2585,26 @@ class T_Scan(unittest.TestCase): ...@@ -2585,6 +2585,26 @@ class T_Scan(unittest.TestCase):
tf = theano.function([c, x], dP) tf = theano.function([c, x], dP)
assert tf([1.0, 2.0, -3.0, 4.0], 2.0) == 38 assert tf([1.0, 2.0, -3.0, 4.0], 2.0) == 38
def test_grad_of_grad_of_state(self):
# Example provided Michael Forbes
# This tests ensures that we can compute gradients through cost
# defines in terms of gradients of scan
c = theano.tensor.vector('c')
x = theano.tensor.scalar('x')
_max_coefficients_supported = 1000
full_range = theano.tensor.arange(_max_coefficients_supported)
components, updates = theano.scan(
fn=lambda coeff, power, free_var: coeff * (free_var ** power),
outputs_info=None,
sequences=[c, full_range],
non_sequences=x)
P = components.sum()
dP = theano.tensor.grad(P, x).sum()
ddP = theano.tensor.grad(dP, x)
tf = theano.function([c, x], ddP)
assert tf([1.0, 2.0, -3.0, 4.0], 2.0) == 42
def test_return_steps(self): def test_return_steps(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
vW_in2 = asarrayX(rng.uniform(size = (2,), low = -5.,high = 5.)) vW_in2 = asarrayX(rng.uniform(size = (2,), low = -5.,high = 5.))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论