提交 d63a2744 authored 作者: Razvan Pascanu's avatar Razvan Pascanu

PEP8

上级 7f0fcc44
...@@ -10,6 +10,7 @@ import numpy ...@@ -10,6 +10,7 @@ import numpy
utt.seed_rng() utt.seed_rng()
def test001_jacobian_vector(): def test001_jacobian_vector():
x = tensor.vector() x = tensor.vector()
y = x * 2 y = x * 2
...@@ -42,11 +43,11 @@ def test001_jacobian_vector(): ...@@ -42,11 +43,11 @@ def test001_jacobian_vector():
vx = rng.uniform(size=(10,)).astype(theano.config.floatX) vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
vz = rng.uniform(size=(10,)).astype(theano.config.floatX) vz = rng.uniform(size=(10,)).astype(theano.config.floatX)
vJs = f(vx, vz) vJs = f(vx, vz)
evx = numpy.zeros((10,10)) evx = numpy.zeros((10, 10))
evz = numpy.zeros((10,10)) evz = numpy.zeros((10, 10))
for dx in xrange(10): for dx in xrange(10):
evx[dx,dx] = vx[dx] evx[dx, dx] = vx[dx]
evz[dx,dx] = vz[dx] evz[dx, dx] = vz[dx]
assert numpy.allclose(vJs[0], evz) assert numpy.allclose(vJs[0], evz)
assert numpy.allclose(vJs[1], evx) assert numpy.allclose(vJs[1], evx)
...@@ -55,27 +56,27 @@ def test002_jacobian_matrix(): ...@@ -55,27 +56,27 @@ def test002_jacobian_matrix():
x = tensor.matrix() x = tensor.matrix()
y = 2 * x.sum(axis=0) y = 2 * x.sum(axis=0)
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = numpy.random.RandomState(seed=utt.fetch_seed())
ev = numpy.zeros((10,10,10)) ev = numpy.zeros((10, 10, 10))
for dx in xrange(10): for dx in xrange(10):
ev[dx,:,dx] = 2. ev[dx, :, dx] = 2.
# test when the jacobian is called with a tensor as wrt # test when the jacobian is called with a tensor as wrt
Jx = tensor.jacobian(y, x) Jx = tensor.jacobian(y, x)
f = theano.function([x], Jx, allow_input_downcast=True) f = theano.function([x], Jx, allow_input_downcast=True)
vx = rng.uniform(size=(10,10)).astype(theano.config.floatX) vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), ev) assert numpy.allclose(f(vx), ev)
# test when the jacobian is called with a tuple as wrt # test when the jacobian is called with a tuple as wrt
Jx = tensor.jacobian(y, (x,)) Jx = tensor.jacobian(y, (x,))
assert isinstance(Jx, tuple) assert isinstance(Jx, tuple)
f = theano.function([x], Jx[0], allow_input_downcast=True) f = theano.function([x], Jx[0], allow_input_downcast=True)
vx = rng.uniform(size=(10,10)).astype(theano.config.floatX) vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), ev) assert numpy.allclose(f(vx), ev)
# test when the jacobian is called with a list as wrt # test when the jacobian is called with a list as wrt
Jx = tensor.jacobian(y, [x]) Jx = tensor.jacobian(y, [x])
f = theano.function([x], Jx[0], allow_input_downcast=True) f = theano.function([x], Jx[0], allow_input_downcast=True)
vx = rng.uniform(size=(10,10)).astype(theano.config.floatX) vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), ev) assert numpy.allclose(f(vx), ev)
# test when the jacobian is called wit a list of two elements # test when the jacobian is called wit a list of two elements
...@@ -83,14 +84,14 @@ def test002_jacobian_matrix(): ...@@ -83,14 +84,14 @@ def test002_jacobian_matrix():
y = (x * z).sum(axis=1) y = (x * z).sum(axis=1)
Js = tensor.jacobian(y, [x, z]) Js = tensor.jacobian(y, [x, z])
f = theano.function([x, z], Js, allow_input_downcast=True) f = theano.function([x, z], Js, allow_input_downcast=True)
vx = rng.uniform(size=(10,10)).astype(theano.config.floatX) vx = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
vz = rng.uniform(size=(10,10)).astype(theano.config.floatX) vz = rng.uniform(size=(10, 10)).astype(theano.config.floatX)
vJs = f(vx, vz) vJs = f(vx, vz)
evx = numpy.zeros((10,10,10)) evx = numpy.zeros((10, 10, 10))
evz = numpy.zeros((10,10,10)) evz = numpy.zeros((10, 10, 10))
for dx in xrange(10): for dx in xrange(10):
evx[dx,dx,:] = vx[dx,:] evx[dx, dx, :] = vx[dx, :]
evz[dx,dx,:] = vz[dx,:] evz[dx, dx, :] = vz[dx, :]
assert numpy.allclose(vJs[0], evz) assert numpy.allclose(vJs[0], evz)
assert numpy.allclose(vJs[1], evx) assert numpy.allclose(vJs[1], evx)
...@@ -131,6 +132,7 @@ def test003_jacobian_scalar(): ...@@ -131,6 +132,7 @@ def test003_jacobian_scalar():
assert numpy.allclose(vJx[0], vz) assert numpy.allclose(vJx[0], vz)
assert numpy.allclose(vJx[1], vx) assert numpy.allclose(vJx[1], vx)
def test004_hessian(): def test004_hessian():
x = tensor.vector() x = tensor.vector()
y = tensor.sum(x ** 2) y = tensor.sum(x ** 2)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论