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

move tests into class

上级 da44df32
...@@ -1826,7 +1826,8 @@ def test_eye(): ...@@ -1826,7 +1826,8 @@ def test_eye():
yield check, dtype, 5, 3, -1 yield check, dtype, 5, 3, -1
def test_tri(): class test_triangle(unittest.TestCase):
def test_tri(self):
def check(dtype, N, M_=None, k=0): def check(dtype, N, M_=None, k=0):
# Theano does not accept None as a tensor. # Theano does not accept None as a tensor.
# So we must use a real value. # So we must use a real value.
...@@ -1841,8 +1842,9 @@ def test_tri(): ...@@ -1841,8 +1842,9 @@ def test_tri():
f = function([N_symb, M_symb, k_symb], f = function([N_symb, M_symb, k_symb],
tri(N_symb, M_symb, k_symb, dtype=dtype)) tri(N_symb, M_symb, k_symb, dtype=dtype))
result = f(N, M, k) result = f(N, M, k)
assert numpy.allclose(result, numpy.tri(N, M_, k, dtype=dtype)) self.assertTrue(
assert result.dtype == numpy.dtype(dtype) numpy.allclose(result, numpy.tri(N, M_, k, dtype=dtype)))
self.assertTrue(result.dtype == numpy.dtype(dtype))
for dtype in ALL_DTYPES: for dtype in ALL_DTYPES:
yield check, dtype, 3 yield check, dtype, 3
# M != N, k = 0 # M != N, k = 0
...@@ -1859,22 +1861,22 @@ def test_tri(): ...@@ -1859,22 +1861,22 @@ def test_tri():
yield check, dtype, 5, 3, -1 yield check, dtype, 5, 3, -1
def test_tril_triu(): def test_tril_triu(self):
def check_l(m, k=0): def check_l(m, k=0):
m_symb = matrix(dtype=m.dtype) m_symb = matrix(dtype=m.dtype)
k_symb = iscalar() k_symb = iscalar()
f = function([m_symb, k_symb], tril(m_symb, k_symb)) f = function([m_symb, k_symb], tril(m_symb, k_symb))
result = f(m, k) result = f(m, k)
assert numpy.allclose(result, numpy.tril(m, k)) self.assertTrue(numpy.allclose(result, numpy.tril(m, k)))
assert result.dtype == numpy.dtype(dtype) self.assertTrue(result.dtype == numpy.dtype(dtype))
def check_u(m, k=0): def check_u(m, k=0):
m_symb = matrix(dtype=m.dtype) m_symb = matrix(dtype=m.dtype)
k_symb = iscalar() k_symb = iscalar()
f = function([m_symb, k_symb], triu(m_symb, k_symb)) f = function([m_symb, k_symb], triu(m_symb, k_symb))
result = f(m, k) result = f(m, k)
assert numpy.allclose(result, numpy.triu(m, k)) self.assertTrue(numpy.allclose(result, numpy.triu(m, k)))
assert result.dtype == numpy.dtype(dtype) self.assertTrue(result.dtype == numpy.dtype(dtype))
for dtype in ALL_DTYPES: for dtype in ALL_DTYPES:
m = rand_of_dtype((10, 10), dtype) m = rand_of_dtype((10, 10), dtype)
...@@ -1896,7 +1898,8 @@ def test_tril_triu(): ...@@ -1896,7 +1898,8 @@ def test_tril_triu():
yield check_u, m, -1 yield check_u, m, -1
def test_nonzero(): class test_nonzero(unittest.TestCase):
def test_nonzero(self):
def check(m): def check(m):
m_symb = theano.tensor.tensor(dtype=m.dtype, m_symb = theano.tensor.tensor(dtype=m.dtype,
broadcastable = (False,) * m.ndim) broadcastable = (False,) * m.ndim)
...@@ -1904,15 +1907,12 @@ def test_nonzero(): ...@@ -1904,15 +1907,12 @@ def test_nonzero():
f_tuple = function([m_symb], nonzero(m_symb, return_matrix=False)) f_tuple = function([m_symb], nonzero(m_symb, return_matrix=False))
f_matrix = function([m_symb], nonzero(m_symb, return_matrix=True)) f_matrix = function([m_symb], nonzero(m_symb, return_matrix=True))
assert numpy.allclose(f_matrix(m), numpy.vstack(numpy.nonzero(m))) self.assertTrue(numpy.allclose(f_matrix(m), numpy.vstack(numpy.nonzero(m))))
for i, j in zip(f_tuple(m), numpy.nonzero(m)): for i, j in zip(f_tuple(m), numpy.nonzero(m)):
assert numpy.allclose(i, j) self.assertTrue(numpy.allclose(i, j))
rand0d = numpy.array(rand()) rand0d = numpy.array(rand())
check(rand0d) self.assertRaises(ValueError, check, rand0d)
rand0d_0 = numpy.array(0, dtype = theano.config.floatX)
check(rand0d_0)
rand1d = rand(8) rand1d = rand(8)
rand1d[:4] = 0 rand1d[:4] = 0
...@@ -1930,7 +1930,8 @@ def test_nonzero(): ...@@ -1930,7 +1930,8 @@ def test_nonzero():
rand4d[:4] = 0 rand4d[:4] = 0
check(rand4d) check(rand4d)
def test_flatnonzero():
def test_flatnonzero(self):
def check(m): def check(m):
m_symb = theano.tensor.tensor(dtype=m.dtype, m_symb = theano.tensor.tensor(dtype=m.dtype,
broadcastable = (False,) * m.ndim) broadcastable = (False,) * m.ndim)
...@@ -1939,10 +1940,7 @@ def test_flatnonzero(): ...@@ -1939,10 +1940,7 @@ def test_flatnonzero():
assert numpy.allclose(result, numpy.flatnonzero(m)) assert numpy.allclose(result, numpy.flatnonzero(m))
rand0d = numpy.array(rand()) rand0d = numpy.array(rand())
check(rand0d) self.assertRaises(ValueError, check, rand0d)
rand0d_0 = numpy.array(0, dtype = theano.config.floatX)
check(rand0d_0)
rand1d = rand(8) rand1d = rand(8)
rand1d[:4] = 0 rand1d[:4] = 0
...@@ -1960,7 +1958,7 @@ def test_flatnonzero(): ...@@ -1960,7 +1958,7 @@ def test_flatnonzero():
rand4d[:4] = 0 rand4d[:4] = 0
check(rand4d) check(rand4d)
def test_nonzero_values(): def test_nonzero_values(self):
def check(m): def check(m):
m_symb = theano.tensor.tensor(dtype=m.dtype, m_symb = theano.tensor.tensor(dtype=m.dtype,
broadcastable = (False,) * m.ndim) broadcastable = (False,) * m.ndim)
...@@ -1968,6 +1966,9 @@ def test_nonzero_values(): ...@@ -1968,6 +1966,9 @@ def test_nonzero_values():
result = f(m) result = f(m)
assert numpy.allclose(result, m[numpy.nonzero(m)]) assert numpy.allclose(result, m[numpy.nonzero(m)])
rand0d = rand()
self.assertRaises(ValueError, check, rand0d)
rand1d = rand(8) rand1d = rand(8)
rand1d[:4] = 0 rand1d[:4] = 0
check(rand1d) check(rand1d)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论