提交 fc5569ad authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 12fe7c30
...@@ -6,7 +6,7 @@ from six.moves import xrange ...@@ -6,7 +6,7 @@ from six.moves import xrange
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
import theano import theano
from theano import tensor from theano import tensor
import numpy import numpy as np
utt.seed_rng() utt.seed_rng()
...@@ -14,27 +14,27 @@ utt.seed_rng() ...@@ -14,27 +14,27 @@ utt.seed_rng()
def test001_jacobian_vector(): def test001_jacobian_vector():
x = tensor.vector() x = tensor.vector()
y = x * 2 y = x * 2
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = np.random.RandomState(seed=utt.fetch_seed())
# 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) f = theano.function([x], Jx)
vx = rng.uniform(size=(10,)).astype(theano.config.floatX) vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), numpy.eye(10) * 2) assert np.allclose(f(vx), np.eye(10) * 2)
# 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]) f = theano.function([x], Jx[0])
vx = rng.uniform(size=(10,)).astype(theano.config.floatX) vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), numpy.eye(10) * 2) assert np.allclose(f(vx), np.eye(10) * 2)
# 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])
assert isinstance(Jx, list) assert isinstance(Jx, list)
f = theano.function([x], Jx[0]) f = theano.function([x], Jx[0])
vx = rng.uniform(size=(10,)).astype(theano.config.floatX) vx = rng.uniform(size=(10,)).astype(theano.config.floatX)
assert numpy.allclose(f(vx), numpy.eye(10) * 2) assert np.allclose(f(vx), np.eye(10) * 2)
# test when the jacobian is called with a list of two elements # test when the jacobian is called with a list of two elements
z = tensor.vector() z = tensor.vector()
...@@ -44,19 +44,19 @@ def test001_jacobian_vector(): ...@@ -44,19 +44,19 @@ 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 = np.zeros((10, 10))
evz = numpy.zeros((10, 10)) evz = np.zeros((10, 10))
numpy.fill_diagonal(evx, vx) np.fill_diagonal(evx, vx)
numpy.fill_diagonal(evz, vz) np.fill_diagonal(evz, vz)
assert numpy.allclose(vJs[0], evz) assert np.allclose(vJs[0], evz)
assert numpy.allclose(vJs[1], evx) assert np.allclose(vJs[1], evx)
def test002_jacobian_matrix(): 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 = np.random.RandomState(seed=utt.fetch_seed())
ev = numpy.zeros((10, 10, 10)) ev = np.zeros((10, 10, 10))
for dx in xrange(10): for dx in xrange(10):
ev[dx, :, dx] = 2. ev[dx, :, dx] = 2.
...@@ -64,21 +64,21 @@ def test002_jacobian_matrix(): ...@@ -64,21 +64,21 @@ def test002_jacobian_matrix():
Jx = tensor.jacobian(y, x) Jx = tensor.jacobian(y, x)
f = theano.function([x], Jx) f = theano.function([x], Jx)
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 np.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]) f = theano.function([x], Jx[0])
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 np.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])
assert isinstance(Jx, list) assert isinstance(Jx, list)
f = theano.function([x], Jx[0]) f = theano.function([x], Jx[0])
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 np.allclose(f(vx), ev)
# test when the jacobian is called with a list of two elements # test when the jacobian is called with a list of two elements
z = tensor.matrix() z = tensor.matrix()
...@@ -88,51 +88,51 @@ def test002_jacobian_matrix(): ...@@ -88,51 +88,51 @@ def test002_jacobian_matrix():
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 = np.zeros((10, 10, 10))
evz = numpy.zeros((10, 10, 10)) evz = np.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 np.allclose(vJs[0], evz)
assert numpy.allclose(vJs[1], evx) assert np.allclose(vJs[1], evx)
def test003_jacobian_scalar(): def test003_jacobian_scalar():
x = tensor.scalar() x = tensor.scalar()
y = x * 2 y = x * 2
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = np.random.RandomState(seed=utt.fetch_seed())
# 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) f = theano.function([x], Jx)
vx = numpy.cast[theano.config.floatX](rng.uniform()) vx = np.cast[theano.config.floatX](rng.uniform())
assert numpy.allclose(f(vx), 2) assert np.allclose(f(vx), 2)
# 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]) f = theano.function([x], Jx[0])
vx = numpy.cast[theano.config.floatX](rng.uniform()) vx = np.cast[theano.config.floatX](rng.uniform())
assert numpy.allclose(f(vx), 2) assert np.allclose(f(vx), 2)
# 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])
assert isinstance(Jx, list) assert isinstance(Jx, list)
f = theano.function([x], Jx[0]) f = theano.function([x], Jx[0])
vx = numpy.cast[theano.config.floatX](rng.uniform()) vx = np.cast[theano.config.floatX](rng.uniform())
assert numpy.allclose(f(vx), 2) assert np.allclose(f(vx), 2)
# test when the jacobian is called with a list of two elements # test when the jacobian is called with a list of two elements
z = tensor.scalar() z = tensor.scalar()
y = x * z y = x * z
Jx = tensor.jacobian(y, [x, z]) Jx = tensor.jacobian(y, [x, z])
f = theano.function([x, z], Jx) f = theano.function([x, z], Jx)
vx = numpy.cast[theano.config.floatX](rng.uniform()) vx = np.cast[theano.config.floatX](rng.uniform())
vz = numpy.cast[theano.config.floatX](rng.uniform()) vz = np.cast[theano.config.floatX](rng.uniform())
vJx = f(vx, vz) vJx = f(vx, vz)
assert numpy.allclose(vJx[0], vz) assert np.allclose(vJx[0], vz)
assert numpy.allclose(vJx[1], vx) assert np.allclose(vJx[1], vx)
def test004_hessian(): def test004_hessian():
...@@ -140,8 +140,8 @@ def test004_hessian(): ...@@ -140,8 +140,8 @@ def test004_hessian():
y = tensor.sum(x ** 2) y = tensor.sum(x ** 2)
Hx = tensor.hessian(y, x) Hx = tensor.hessian(y, x)
f = theano.function([x], Hx) f = theano.function([x], Hx)
vx = numpy.arange(10).astype(theano.config.floatX) vx = np.arange(10).astype(theano.config.floatX)
assert numpy.allclose(f(vx), numpy.eye(10) * 2) assert np.allclose(f(vx), np.eye(10) * 2)
def test_jacobian_disconnected_inputs(): def test_jacobian_disconnected_inputs():
...@@ -152,12 +152,12 @@ def test_jacobian_disconnected_inputs(): ...@@ -152,12 +152,12 @@ def test_jacobian_disconnected_inputs():
v2 = tensor.vector() v2 = tensor.vector()
jacobian_v = theano.gradient.jacobian(1 + v1, v2, disconnected_inputs='ignore') jacobian_v = theano.gradient.jacobian(1 + v1, v2, disconnected_inputs='ignore')
func_v = theano.function([v1, v2], jacobian_v) func_v = theano.function([v1, v2], jacobian_v)
val = numpy.arange(4.0).astype(theano.config.floatX) val = np.arange(4.0).astype(theano.config.floatX)
assert numpy.allclose(func_v(val, val), numpy.zeros((4, 4))) assert np.allclose(func_v(val, val), np.zeros((4, 4)))
s1 = tensor.scalar() s1 = tensor.scalar()
s2 = tensor.scalar() s2 = tensor.scalar()
jacobian_s = theano.gradient.jacobian(1 + s1, s2, disconnected_inputs='ignore') jacobian_s = theano.gradient.jacobian(1 + s1, s2, disconnected_inputs='ignore')
func_s = theano.function([s2], jacobian_s) func_s = theano.function([s2], jacobian_s)
val = numpy.array(1.0).astype(theano.config.floatX) val = np.array(1.0).astype(theano.config.floatX)
assert numpy.allclose(func_s(val), numpy.zeros(1)) assert np.allclose(func_s(val), np.zeros(1))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论