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

1. Updated numpy as np

2. Fixed indentation
上级 bcd5d52e
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import unittest import unittest
import numpy import numpy as np
import numpy.linalg import numpy.linalg
from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose from numpy.testing import dec, assert_array_equal, assert_allclose
...@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f): ...@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f):
ch = ch_f(pd) ch = ch_f(pd)
assert ch[0, pd.shape[1] - 1] == 0 assert ch[0, pd.shape[1] - 1] == 0
assert ch[pd.shape[0] - 1, 0] != 0 assert ch[pd.shape[0] - 1, 0] != 0
assert numpy.allclose(numpy.dot(ch, ch.T), pd) assert np.allclose(np.dot(ch, ch.T), pd)
assert not numpy.allclose(numpy.dot(ch.T, ch), pd) assert not np.allclose(np.dot(ch.T, ch), pd)
def check_upper_triangular(pd, ch_f): def check_upper_triangular(pd, ch_f):
ch = ch_f(pd) ch = ch_f(pd)
assert ch[4, 0] == 0 assert ch[4, 0] == 0
assert ch[0, 4] != 0 assert ch[0, 4] != 0
assert numpy.allclose(numpy.dot(ch.T, ch), pd) assert np.allclose(np.dot(ch.T, ch), pd)
assert not numpy.allclose(numpy.dot(ch, ch.T), pd) assert not np.allclose(np.dot(ch, ch.T), pd)
def test_cholesky(): def test_cholesky():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
pd = numpy.dot(r, r.T) pd = np.dot(r, r.T)
x = tensor.matrix() x = tensor.matrix()
chol = cholesky(x) chol = cholesky(x)
# Check the default. # Check the default.
...@@ -72,7 +72,7 @@ def test_cholesky(): ...@@ -72,7 +72,7 @@ def test_cholesky():
def test_cholesky_grad(): def test_cholesky_grad():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
# The dots are inside the graph since Cholesky needs separable matrices # The dots are inside the graph since Cholesky needs separable matrices
...@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape(): ...@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = tensor.matrix() x = tensor.matrix()
for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)): for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)):
f_chol = theano.function([x], l.shape) f_chol = theano.function([x], l.shape)
...@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape(): ...@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape():
assert sum([node.op.__class__ == CholeskyGrad assert sum([node.op.__class__ == CholeskyGrad
for node in topo_cholgrad]) == 0 for node in topo_cholgrad]) == 0
for shp in [2, 3, 5]: for shp in [2, 3, 5]:
m = numpy.cov(rng.randn(shp, shp + 10)).astype(config.floatX) m = np.cov(rng.randn(shp, shp + 10)).astype(config.floatX)
yield numpy.testing.assert_equal, f_chol(m), (shp, shp) yield np.testing.assert_equal, f_chol(m), (shp, shp)
yield numpy.testing.assert_equal, f_cholgrad(m), (shp, shp) yield np.testing.assert_equal, f_cholgrad(m), (shp, shp)
def test_eigvalsh(): def test_eigvalsh():
...@@ -121,13 +121,13 @@ def test_eigvalsh(): ...@@ -121,13 +121,13 @@ def test_eigvalsh():
B = theano.tensor.dmatrix('b') B = theano.tensor.dmatrix('b')
f = function([A, B], eigvalsh(A, B)) f = function([A, B], eigvalsh(A, B))
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5) a = rng.randn(5, 5)
a = a + a.T a = a + a.T
for b in [10 * numpy.eye(5, 5) + rng.randn(5, 5)]: for b in [10 * np.eye(5, 5) + rng.randn(5, 5)]:
w = f(a, b) w = f(a, b)
refw = scipy.linalg.eigvalsh(a, b) refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw) np.testing.assert_array_almost_equal(w, refw)
# We need to test None separatly, as otherwise DebugMode will # We need to test None separatly, as otherwise DebugMode will
# complain, as this isn't a valid ndarray. # complain, as this isn't a valid ndarray.
...@@ -136,7 +136,7 @@ def test_eigvalsh(): ...@@ -136,7 +136,7 @@ def test_eigvalsh():
f = function([A], eigvalsh(A, B)) f = function([A], eigvalsh(A, B))
w = f(a) w = f(a)
refw = scipy.linalg.eigvalsh(a, b) refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw) np.testing.assert_array_almost_equal(w, refw)
def test_eigvalsh_grad(): def test_eigvalsh_grad():
...@@ -144,12 +144,12 @@ def test_eigvalsh_grad(): ...@@ -144,12 +144,12 @@ def test_eigvalsh_grad():
raise SkipTest("Scipy needed for the geigvalsh op.") raise SkipTest("Scipy needed for the geigvalsh op.")
import scipy.linalg import scipy.linalg
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5) a = rng.randn(5, 5)
a = a + a.T a = a + a.T
b = 10 * numpy.eye(5, 5) + rng.randn(5, 5) b = 10 * np.eye(5, 5) + rng.randn(5, 5)
tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]), tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]),
[a, b], rng=numpy.random) [a, b], rng=np.random)
class test_Solve(utt.InferShapeTester): class test_Solve(utt.InferShapeTester):
...@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester): ...@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Solve op.") raise SkipTest("Scipy needed for the Solve op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.matrix() b = theano.tensor.matrix()
self._compile_and_check([A, b], # theano.function inputs self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs [self.op(A, b)], # theano.function outputs
# A must be square # A must be square
[numpy.asarray(rng.rand(5, 5), [np.asarray(rng.rand(5, 5),
dtype=config.floatX), dtype=config.floatX),
numpy.asarray(rng.rand(5, 1), np.asarray(rng.rand(5, 1),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class, self.op_class,
warn=False) warn=False)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.vector() b = theano.tensor.vector()
self._compile_and_check([A, b], # theano.function inputs self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs [self.op(A, b)], # theano.function outputs
# A must be square # A must be square
[numpy.asarray(rng.rand(5, 5), [np.asarray(rng.rand(5, 5),
dtype=config.floatX), dtype=config.floatX),
numpy.asarray(rng.rand(5), np.asarray(rng.rand(5),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class, self.op_class,
warn=False) warn=False)
...@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_correctness(self): def test_solve_correctness(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky and Solve ops.") raise SkipTest("Scipy needed for the Cholesky and Solve ops.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.matrix() b = theano.tensor.matrix()
y = self.op(A, b) y = self.op(A, b)
...@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester): ...@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester):
y_upper = self.op(U, b) y_upper = self.op(U, b)
upper_solve_func = theano.function([U, b], y_upper) upper_solve_func = theano.function([U, b], y_upper)
b_val = numpy.asarray(rng.rand(5, 1), dtype=config.floatX) b_val = np.asarray(rng.rand(5, 1), dtype=config.floatX)
# 1-test general case # 1-test general case
A_val = numpy.asarray(rng.rand(5, 5), dtype=config.floatX) A_val = np.asarray(rng.rand(5, 5), dtype=config.floatX)
# positive definite matrix: # positive definite matrix:
A_val = numpy.dot(A_val.transpose(), A_val) A_val = np.dot(A_val.transpose(), A_val)
assert numpy.allclose(scipy.linalg.solve(A_val, b_val), assert np.allclose(scipy.linalg.solve(A_val, b_val),
gen_solve_func(A_val, b_val)) gen_solve_func(A_val, b_val))
# 2-test lower traingular case # 2-test lower traingular case
L_val = scipy.linalg.cholesky(A_val, lower=True) L_val = scipy.linalg.cholesky(A_val, lower=True)
assert numpy.allclose(scipy.linalg.solve_triangular(L_val, b_val, lower=True), assert np.allclose(scipy.linalg.solve_triangular(L_val, b_val, lower=True),
lower_solve_func(L_val, b_val)) lower_solve_func(L_val, b_val))
# 3-test upper traingular case # 3-test upper traingular case
U_val = scipy.linalg.cholesky(A_val, lower=False) U_val = scipy.linalg.cholesky(A_val, lower=False)
assert numpy.allclose(scipy.linalg.solve_triangular(U_val, b_val, lower=False), assert np.allclose(scipy.linalg.solve_triangular(U_val, b_val, lower=False),
upper_solve_func(U_val, b_val)) upper_solve_func(U_val, b_val))
def test_solve_dtype(self): def test_solve_dtype(self):
...@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester): ...@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester):
'int8', 'int16', 'int32', 'int64', 'int8', 'int16', 'int32', 'int64',
'float16', 'float32', 'float64'] 'float16', 'float32', 'float64']
A_val = numpy.eye(2) A_val = np.eye(2)
b_val = numpy.ones((2, 1)) b_val = np.ones((2, 1))
# try all dtype combinations # try all dtype combinations
for A_dtype, b_dtype in itertools.product(dtypes, dtypes): for A_dtype, b_dtype in itertools.product(dtypes, dtypes):
...@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester): ...@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester):
# ensure diagonal elements of A relatively large to avoid numerical # ensure diagonal elements of A relatively large to avoid numerical
# precision issues # precision issues
A_val = (rng.normal(size=(m, m)) * 0.5 + A_val = (rng.normal(size=(m, m)) * 0.5 +
numpy.eye(m)).astype(config.floatX) np.eye(m)).astype(config.floatX)
if A_structure == 'lower_triangular': if A_structure == 'lower_triangular':
A_val = numpy.tril(A_val) A_val = np.tril(A_val)
elif A_structure == 'upper_triangular': elif A_structure == 'upper_triangular':
A_val = numpy.triu(A_val) A_val = np.triu(A_val)
if n is None: if n is None:
b_val = rng.normal(size=m).astype(config.floatX) b_val = rng.normal(size=m).astype(config.floatX)
else: else:
...@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_grad(self): def test_solve_grad(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Solve op.") raise SkipTest("Scipy needed for the Solve op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
structures = ['general', 'lower_triangular', 'upper_triangular'] structures = ['general', 'lower_triangular', 'upper_triangular']
for A_structure in structures: for A_structure in structures:
lower = (A_structure == 'lower_triangular') lower = (A_structure == 'lower_triangular')
...@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester):
def test_expm(): def test_expm():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = rng.randn(5, 5).astype(config.floatX) A = rng.randn(5, 5).astype(config.floatX)
ref = scipy.linalg.expm(A) ref = scipy.linalg.expm(A)
...@@ -292,14 +292,14 @@ def test_expm(): ...@@ -292,14 +292,14 @@ def test_expm():
expm_f = function([x], m) expm_f = function([x], m)
val = expm_f(A) val = expm_f(A)
numpy.testing.assert_array_almost_equal(val, ref) np.testing.assert_array_almost_equal(val, ref)
def test_expm_grad_1(): def test_expm_grad_1():
# with symmetric matrix (real eigenvectors) # with symmetric matrix (real eigenvectors)
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
A = A + A.T A = A + A.T
...@@ -311,12 +311,12 @@ def test_expm_grad_2(): ...@@ -311,12 +311,12 @@ def test_expm_grad_2():
# with non-symmetric matrix with real eigenspecta # with non-symmetric matrix with real eigenspecta
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
w = rng.randn(5)**2 w = rng.randn(5)**2
A = (numpy.diag(w**0.5)).dot(A + A.T).dot(numpy.diag(w**(-0.5))) A = (np.diag(w**0.5)).dot(A + A.T).dot(np.diag(w**(-0.5)))
assert not numpy.allclose(A, A.T) assert not np.allclose(A, A.T)
tensor.verify_grad(expm, [A], rng=rng) tensor.verify_grad(expm, [A], rng=rng)
...@@ -325,7 +325,7 @@ def test_expm_grad_3(): ...@@ -325,7 +325,7 @@ def test_expm_grad_3():
# with non-symmetric matrix (complex eigenvectors) # with non-symmetric matrix (complex eigenvectors)
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
...@@ -334,7 +334,7 @@ def test_expm_grad_3(): ...@@ -334,7 +334,7 @@ def test_expm_grad_3():
class TestKron(utt.InferShapeTester): class TestKron(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestKron, self).setUp() super(TestKron, self).setUp()
...@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester): ...@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]: for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]:
x = tensor.tensor(dtype='floatX', x = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp0)) broadcastable=(False,) * len(shp0))
a = numpy.asarray(self.rng.rand(*shp0)).astype(config.floatX) a = np.asarray(self.rng.rand(*shp0)).astype(config.floatX)
for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]: for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]:
if len(shp0) + len(shp1) == 2: if len(shp0) + len(shp1) == 2:
continue continue
...@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester): ...@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester):
# so we have to add a dimension to a and flatten the result. # so we have to add a dimension to a and flatten the result.
if len(shp0) + len(shp1) == 3: if len(shp0) + len(shp1) == 3:
scipy_val = scipy.linalg.kron( scipy_val = scipy.linalg.kron(
a[numpy.newaxis, :], b).flatten() a[np.newaxis, :], b).flatten()
else: else:
scipy_val = scipy.linalg.kron(a, b) scipy_val = scipy.linalg.kron(a, b)
utt.assert_allclose(out, scipy_val) utt.assert_allclose(out, scipy_val)
...@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester): ...@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2, 3)]: for shp0 in [(2, 3)]:
x = tensor.tensor(dtype='floatX', x = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp0)) broadcastable=(False,) * len(shp0))
a = numpy.asarray(self.rng.rand(*shp0)).astype(config.floatX) a = np.asarray(self.rng.rand(*shp0)).astype(config.floatX)
for shp1 in [(6, 7)]: for shp1 in [(6, 7)]:
if len(shp0) + len(shp1) == 2: if len(shp0) + len(shp1) == 2:
continue continue
...@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester): ...@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester):
f = function([x, y], kron(x, y)) f = function([x, y], kron(x, y))
b = self.rng.rand(*shp1).astype(config.floatX) b = self.rng.rand(*shp1).astype(config.floatX)
out = f(a, b) out = f(a, b)
assert numpy.allclose(out, numpy.kron(a, b)) assert np.allclose(out, np.kron(a, b))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论