提交 08425470 authored 作者: Jeremiah Lowin's avatar Jeremiah Lowin

move tests into class

上级 da44df32
...@@ -1826,163 +1826,164 @@ def test_eye(): ...@@ -1826,163 +1826,164 @@ def test_eye():
yield check, dtype, 5, 3, -1 yield check, dtype, 5, 3, -1
def test_tri(): class test_triangle(unittest.TestCase):
def check(dtype, N, M_=None, k=0): def test_tri(self):
# Theano does not accept None as a tensor. def check(dtype, N, M_=None, k=0):
# So we must use a real value. # Theano does not accept None as a tensor.
M = M_ # So we must use a real value.
# Currently DebugMode does not support None as inputs even if this is M = M_
# allowed. # Currently DebugMode does not support None as inputs even if this is
if M is None and theano.config.mode in ['DebugMode', 'DEBUG_MODE']: # allowed.
M = N if M is None and theano.config.mode in ['DebugMode', 'DEBUG_MODE']:
N_symb = tensor.iscalar() M = N
M_symb = tensor.iscalar() N_symb = tensor.iscalar()
k_symb = tensor.iscalar() M_symb = tensor.iscalar()
f = function([N_symb, M_symb, k_symb], k_symb = tensor.iscalar()
tri(N_symb, M_symb, k_symb, dtype=dtype)) f = function([N_symb, M_symb, k_symb],
result = f(N, M, k) tri(N_symb, M_symb, k_symb, dtype=dtype))
assert numpy.allclose(result, numpy.tri(N, M_, k, dtype=dtype)) result = f(N, M, k)
assert result.dtype == numpy.dtype(dtype) self.assertTrue(
for dtype in ALL_DTYPES: numpy.allclose(result, numpy.tri(N, M_, k, dtype=dtype)))
yield check, dtype, 3 self.assertTrue(result.dtype == numpy.dtype(dtype))
# M != N, k = 0 for dtype in ALL_DTYPES:
yield check, dtype, 3, 5 yield check, dtype, 3
yield check, dtype, 5, 3 # M != N, k = 0
# N == M, k != 0 yield check, dtype, 3, 5
yield check, dtype, 3, 3, 1 yield check, dtype, 5, 3
yield check, dtype, 3, 3, -1 # N == M, k != 0
# N < M, k != 0 yield check, dtype, 3, 3, 1
yield check, dtype, 3, 5, 1 yield check, dtype, 3, 3, -1
yield check, dtype, 3, 5, -1 # N < M, k != 0
# N > M, k != 0 yield check, dtype, 3, 5, 1
yield check, dtype, 5, 3, 1 yield check, dtype, 3, 5, -1
yield check, dtype, 5, 3, -1 # N > M, k != 0
yield check, dtype, 5, 3, 1
yield check, dtype, 5, 3, -1
def test_tril_triu():
def check_l(m, k=0):
m_symb = matrix(dtype=m.dtype) def test_tril_triu(self):
k_symb = iscalar() def check_l(m, k=0):
f = function([m_symb, k_symb], tril(m_symb, k_symb)) m_symb = matrix(dtype=m.dtype)
result = f(m, k) k_symb = iscalar()
assert numpy.allclose(result, numpy.tril(m, k)) f = function([m_symb, k_symb], tril(m_symb, k_symb))
assert result.dtype == numpy.dtype(dtype) result = f(m, k)
self.assertTrue(numpy.allclose(result, numpy.tril(m, k)))
def check_u(m, k=0): self.assertTrue(result.dtype == numpy.dtype(dtype))
m_symb = matrix(dtype=m.dtype)
k_symb = iscalar() def check_u(m, k=0):
f = function([m_symb, k_symb], triu(m_symb, k_symb)) m_symb = matrix(dtype=m.dtype)
result = f(m, k) k_symb = iscalar()
assert numpy.allclose(result, numpy.triu(m, k)) f = function([m_symb, k_symb], triu(m_symb, k_symb))
assert result.dtype == numpy.dtype(dtype) result = f(m, k)
self.assertTrue(numpy.allclose(result, numpy.triu(m, k)))
for dtype in ALL_DTYPES: self.assertTrue(result.dtype == numpy.dtype(dtype))
m = rand_of_dtype((10, 10), dtype)
yield check_l, m, 0 for dtype in ALL_DTYPES:
yield check_l, m, 1 m = rand_of_dtype((10, 10), dtype)
yield check_l, m, -1 yield check_l, m, 0
yield check_l, m, 1
yield check_u, m, 0 yield check_l, m, -1
yield check_u, m, 1
yield check_u, m, -1 yield check_u, m, 0
yield check_u, m, 1
m = rand_of_dtype((10, 5), dtype) yield check_u, m, -1
yield check_l, m, 0
yield check_l, m, 1 m = rand_of_dtype((10, 5), dtype)
yield check_l, m, -1 yield check_l, m, 0
yield check_l, m, 1
yield check_u, m, 0 yield check_l, m, -1
yield check_u, m, 1
yield check_u, m, -1 yield check_u, m, 0
yield check_u, m, 1
yield check_u, m, -1
def test_nonzero():
def check(m):
m_symb = theano.tensor.tensor(dtype=m.dtype, class test_nonzero(unittest.TestCase):
broadcastable = (False,) * m.ndim) def test_nonzero(self):
def check(m):
f_tuple = function([m_symb], nonzero(m_symb, return_matrix=False)) m_symb = theano.tensor.tensor(dtype=m.dtype,
f_matrix = function([m_symb], nonzero(m_symb, return_matrix=True)) broadcastable = (False,) * m.ndim)
assert numpy.allclose(f_matrix(m), numpy.vstack(numpy.nonzero(m))) f_tuple = function([m_symb], nonzero(m_symb, return_matrix=False))
for i, j in zip(f_tuple(m), numpy.nonzero(m)): f_matrix = function([m_symb], nonzero(m_symb, return_matrix=True))
assert numpy.allclose(i, j)
self.assertTrue(numpy.allclose(f_matrix(m), numpy.vstack(numpy.nonzero(m))))
rand0d = numpy.array(rand()) for i, j in zip(f_tuple(m), numpy.nonzero(m)):
check(rand0d) self.assertTrue(numpy.allclose(i, j))
rand0d_0 = numpy.array(0, dtype = theano.config.floatX) rand0d = numpy.array(rand())
check(rand0d_0) self.assertRaises(ValueError, check, rand0d)
rand1d = rand(8) rand1d = rand(8)
rand1d[:4] = 0 rand1d[:4] = 0
check(rand1d) check(rand1d)
rand2d = rand(8, 9) rand2d = rand(8, 9)
rand2d[:4] = 0 rand2d[:4] = 0
check(rand2d) check(rand2d)
rand3d = rand(8, 9, 10) rand3d = rand(8, 9, 10)
rand3d[:4] = 0 rand3d[:4] = 0
check(rand3d) check(rand3d)
rand4d = rand(8, 9, 10, 11) rand4d = rand(8, 9, 10, 11)
rand4d[:4] = 0 rand4d[:4] = 0
check(rand4d) check(rand4d)
def test_flatnonzero():
def check(m): def test_flatnonzero(self):
m_symb = theano.tensor.tensor(dtype=m.dtype, def check(m):
broadcastable = (False,) * m.ndim) m_symb = theano.tensor.tensor(dtype=m.dtype,
f = function([m_symb], flatnonzero(m_symb)) broadcastable = (False,) * m.ndim)
result = f(m) f = function([m_symb], flatnonzero(m_symb))
assert numpy.allclose(result, numpy.flatnonzero(m)) result = f(m)
assert numpy.allclose(result, numpy.flatnonzero(m))
rand0d = numpy.array(rand())
check(rand0d) rand0d = numpy.array(rand())
self.assertRaises(ValueError, check, rand0d)
rand0d_0 = numpy.array(0, dtype = theano.config.floatX)
check(rand0d_0) rand1d = rand(8)
rand1d[:4] = 0
rand1d = rand(8) check(rand1d)
rand1d[:4] = 0
check(rand1d) rand2d = rand(8, 9)
rand2d[:4] = 0
rand2d = rand(8, 9) check(rand2d)
rand2d[:4] = 0
check(rand2d) rand3d = rand(8, 9, 10)
rand3d[:4] = 0
rand3d = rand(8, 9, 10) check(rand3d)
rand3d[:4] = 0
check(rand3d) rand4d = rand(8, 9, 10, 11)
rand4d[:4] = 0
rand4d = rand(8, 9, 10, 11) check(rand4d)
rand4d[:4] = 0
check(rand4d) def test_nonzero_values(self):
def check(m):
def test_nonzero_values(): m_symb = theano.tensor.tensor(dtype=m.dtype,
def check(m): broadcastable = (False,) * m.ndim)
m_symb = theano.tensor.tensor(dtype=m.dtype, f = function([m_symb], nonzero_values(m_symb))
broadcastable = (False,) * m.ndim) result = f(m)
f = function([m_symb], nonzero_values(m_symb)) assert numpy.allclose(result, m[numpy.nonzero(m)])
result = f(m)
assert numpy.allclose(result, m[numpy.nonzero(m)]) rand0d = rand()
self.assertRaises(ValueError, check, rand0d)
rand1d = rand(8)
rand1d[:4] = 0 rand1d = rand(8)
check(rand1d) rand1d[:4] = 0
check(rand1d)
rand2d = rand(8, 9)
rand2d[:4] = 0 rand2d = rand(8, 9)
check(rand2d) rand2d[:4] = 0
check(rand2d)
rand3d = rand(8, 9, 10)
rand3d[:4] = 0 rand3d = rand(8, 9, 10)
check(rand3d) rand3d[:4] = 0
check(rand3d)
rand4d = rand(8, 9, 10, 11)
rand4d[:4] = 0 rand4d = rand(8, 9, 10, 11)
check(rand4d) rand4d[:4] = 0
check(rand4d)
def test_identity(): def test_identity():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论