提交 161a3682 authored 作者: Razvan Pascanu's avatar Razvan Pascanu

add more tests

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