提交 94148810 authored 作者: Tanjay94's avatar Tanjay94

Fixed tests transfer for numpy based function.

上级 65908b64
...@@ -36,7 +36,9 @@ from theano.tensor.nlinalg import ( MatrixInverse, ...@@ -36,7 +36,9 @@ from theano.tensor.nlinalg import ( MatrixInverse,
_zero_disconnected, _zero_disconnected,
qr, qr,
svd, svd,
lstsq lstsq,
matrix_power,
norm
) )
from theano.tensor.slinalg import ( Cholesky, from theano.tensor.slinalg import ( Cholesky,
...@@ -378,54 +380,3 @@ def spectral_radius_bound(X, log2_exponent): ...@@ -378,54 +380,3 @@ def spectral_radius_bound(X, log2_exponent):
return tensor.pow( return tensor.pow(
trace(XX), trace(XX),
2 ** (-log2_exponent)) 2 ** (-log2_exponent))
def matrix_power(M, n):
result = 1
for i in xrange(n):
result = theano.dot(result, M)
return result
def norm(x,ord):
x = as_tensor_variable(x)
ndim = x.ndim
if ndim == 0:
raise ValueError("'axis' entry is out of bounds.")
elif ndim == 1:
if ord == None:
return tensor.sum(x**2)**0.5
elif ord == 'inf':
return tensor.max(abs(x))
elif ord == '-inf':
return tensor.min(abs(x))
elif ord == 0:
return x[x.nonzero()].shape[0]
else:
try:
z = tensor.sum(abs(x**ord))**(1./ord)
except TypeError:
raise ValueError("Invalid norm order for vectors.")
return z
elif ndim == 2:
if ord == None or ord == 'fro':
return tensor.sum(abs(x**2))**(0.5)
elif ord == 'inf':
return tensor.max(tensor.sum(abs(x), 1))
elif ord == '-inf':
return tensor.min(tensor.sum(abs(x), 1))
elif ord == 1:
return tensor.max(tensor.sum(abs(x), 0))
elif ord == -1:
return tensor.min(tensor.sum(abs(x),0))
else:
raise ValueError()
elif ndim > 2:
raise NotImplementedError("We don't support norm witn ndim > 2")
...@@ -137,59 +137,3 @@ def test_spectral_radius_bound(): ...@@ -137,59 +137,3 @@ def test_spectral_radius_bound():
except ValueError: except ValueError:
ok = True ok = True
assert ok assert ok
class Matrix_power():
def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3)
fn = function([A], [Q])
a = rng.rand(4, 4).astype(theano.config.floatX)
n_p = numpy.linalg.matrix_power(a, 3)
t_p = fn(a)
assert numpy.allclose(n_p, t_p)
def test_non_square_matrix(self):
rng = numpy.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3)
f = function([A], [Q])
a = rng.rand(4, 3).astype(theano.config.floatX)
self.assertRaises(ValueError, f, a)
class T_NormTests(unittest.TestCase):
def test_wrong_type_of_ord_for_vector(self):
self.assertRaises(ValueError, norm, [2, 1], 'fro')
def test_wrong_type_of_ord_for_matrix(self):
self.assertRaises(ValueError, norm, [[2, 1], [3, 4]], 0)
def test_non_tensorial_input(self):
self.assertRaises(ValueError, norm, 3, None)
def test_tensor_input(self):
self.assertRaises(NotImplementedError, norm, numpy.random.rand(3, 4, 5), None)
def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed())
M = tensor.matrix("A", dtype=theano.config.floatX)
V = tensor.vector("V", dtype=theano.config.floatX)
a = rng.rand(4, 4).astype(theano.config.floatX)
b = rng.rand(4).astype(theano.config.floatX)
A = ( [None, 'fro', 'inf', '-inf', 1, -1, None, 'inf', '-inf', 0, 1, -1, 2, -2],
[M, M, M, M, M, M, V, V, V, V, V, V, V, V],
[a, a, a, a, a, a, b, b, b, b, b, b, b, b],
[None, 'fro', inf, -inf, 1, -1, None, inf, -inf, 0, 1, -1, 2, -2])
for i in range(0, 14):
f = function([A[1][i]], norm(A[1][i], A[0][i]))
t_n = f(A[2][i])
n_n = numpy.linalg.norm(A[2][i], A[3][i])
assert _allclose(n_n, t_n)
...@@ -727,4 +727,49 @@ class lstsq(theano.Op): ...@@ -727,4 +727,49 @@ class lstsq(theano.Op):
outputs[0][0] = zz[0] outputs[0][0] = zz[0]
outputs[1][0] = zz[1] outputs[1][0] = zz[1]
outputs[2][0] = zz[2] outputs[2][0] = zz[2]
outputs[3][0] = zz[3] outputs[3][0] = zz[3]
\ No newline at end of file
def matrix_power(M, n):
result = 1
for i in xrange(n):
result = theano.dot(result, M)
return result
def norm(x,ord):
x = as_tensor_variable(x)
ndim = x.ndim
if ndim == 0:
raise ValueError("'axis' entry is out of bounds.")
elif ndim == 1:
if ord == None:
return tensor.sum(x**2)**0.5
elif ord == 'inf':
return tensor.max(abs(x))
elif ord == '-inf':
return tensor.min(abs(x))
elif ord == 0:
return x[x.nonzero()].shape[0]
else:
try:
z = tensor.sum(abs(x**ord))**(1./ord)
except TypeError:
raise ValueError("Invalid norm order for vectors.")
return z
elif ndim == 2:
if ord == None or ord == 'fro':
return tensor.sum(abs(x**2))**(0.5)
elif ord == 'inf':
return tensor.max(tensor.sum(abs(x), 1))
elif ord == '-inf':
return tensor.min(tensor.sum(abs(x), 1))
elif ord == 1:
return tensor.max(tensor.sum(abs(x), 0))
elif ord == -1:
return tensor.min(tensor.sum(abs(x),0))
else:
raise ValueError(0)
elif ndim > 2:
raise NotImplementedError("We don't support norm witn ndim > 2")
...@@ -32,7 +32,9 @@ from theano.tensor.nlinalg import ( MatrixInverse, ...@@ -32,7 +32,9 @@ from theano.tensor.nlinalg import ( MatrixInverse,
eigh, eigh,
matrix_dot, matrix_dot,
_zero_disconnected, _zero_disconnected,
qr qr,
matrix_power,
norm
) )
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
...@@ -432,3 +434,59 @@ class T_lstsq(unittest.TestCase): ...@@ -432,3 +434,59 @@ class T_lstsq(unittest.TestCase):
f = function([x, y, z], b) f = function([x, y, z], b)
self.assertRaises(numpy.linalg.LinAlgError, f, [2, 1], [2, 1], [2, 1]) self.assertRaises(numpy.linalg.LinAlgError, f, [2, 1], [2, 1], [2, 1])
class Matrix_power():
def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3)
fn = function([A], [Q])
a = rng.rand(4, 4).astype(theano.config.floatX)
n_p = numpy.linalg.matrix_power(a, 3)
t_p = fn(a)
assert numpy.allclose(n_p, t_p)
def test_non_square_matrix(self):
rng = numpy.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3)
f = function([A], [Q])
a = rng.rand(4, 3).astype(theano.config.floatX)
self.assertRaises(ValueError, f, a)
class T_NormTests(unittest.TestCase):
def test_wrong_type_of_ord_for_vector(self):
self.assertRaises(ValueError, norm, [2, 1], 'fro')
def test_wrong_type_of_ord_for_matrix(self):
self.assertRaises(ValueError, norm, [[2, 1], [3, 4]], 0)
def test_non_tensorial_input(self):
self.assertRaises(ValueError, norm, 3, None)
def test_tensor_input(self):
self.assertRaises(NotImplementedError, norm, numpy.random.rand(3, 4, 5), None)
def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed())
M = tensor.matrix("A", dtype=theano.config.floatX)
V = tensor.vector("V", dtype=theano.config.floatX)
a = rng.rand(4, 4).astype(theano.config.floatX)
b = rng.rand(4).astype(theano.config.floatX)
A = ( [None, 'fro', 'inf', '-inf', 1, -1, None, 'inf', '-inf', 0, 1, -1, 2, -2],
[M, M, M, M, M, M, V, V, V, V, V, V, V, V],
[a, a, a, a, a, a, b, b, b, b, b, b, b, b],
[None, 'fro', inf, -inf, 1, -1, None, inf, -inf, 0, 1, -1, 2, -2])
for i in range(0, 14):
f = function([A[1][i]], norm(A[1][i], A[0][i]))
t_n = f(A[2][i])
n_n = numpy.linalg.norm(A[2][i], A[3][i])
assert _allclose(n_n, t_n)
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论