提交 2060b8f7 authored 作者: lamblin's avatar lamblin

Merge pull request #1102 from nouiz/py24

Fix tests
...@@ -879,6 +879,7 @@ class A_Xinv_b(Op): ...@@ -879,6 +879,7 @@ class A_Xinv_b(Op):
gb = matrix_dot(ix.T, a.T, gz) gb = matrix_dot(ix.T, a.T, gz)
return [ga, gX, gb] return [ga, gX, gb]
class Eig(Op): class Eig(Op):
"""Compute the eigenvalues and right eigenvectors of a square array. """Compute the eigenvalues and right eigenvectors of a square array.
...@@ -916,17 +917,23 @@ class Eig(Op): ...@@ -916,17 +917,23 @@ class Eig(Op):
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
n = shapes[0][0] n = shapes[0][0]
return [(n,), (n,n)] return [(n,), (n, n)]
def __str__(self): def __str__(self):
return self._numop.__name__.capitalize() return self._numop.__name__.capitalize()
eig = Eig() eig = Eig()
def _zero_disconnected(outputs, grads): def _zero_disconnected(outputs, grads):
return [o.zeros_like() l = []
if isinstance(g.type, DisconnectedType) else g for o, g in zip(outputs, grads):
for o, g in zip(outputs, grads)] if isinstance(g.type, DisconnectedType):
l.append(o.zeros_like())
else:
l.append(g)
return l
class Eigh(Eig): class Eigh(Eig):
""" """
...@@ -934,6 +941,7 @@ class Eigh(Eig): ...@@ -934,6 +941,7 @@ class Eigh(Eig):
""" """
_numop = staticmethod(numpy.linalg.eigh) _numop = staticmethod(numpy.linalg.eigh)
def __init__(self, UPLO='L'): def __init__(self, UPLO='L'):
self.UPLO = UPLO self.UPLO = UPLO
...@@ -962,7 +970,6 @@ class Eigh(Eig): ...@@ -962,7 +970,6 @@ class Eigh(Eig):
except numpy.linalg.LinAlgError: except numpy.linalg.LinAlgError:
logger.debug('Failed to find %s of %s' % (self._numop.__name__, logger.debug('Failed to find %s of %s' % (self._numop.__name__,
node.inputs[0])) node.inputs[0]))
raise raise
def grad(self, inputs, g_outputs): def grad(self, inputs, g_outputs):
...@@ -994,9 +1001,11 @@ class Eigh(Eig): ...@@ -994,9 +1001,11 @@ class Eigh(Eig):
gw, gv = _zero_disconnected([w, v], g_outputs) gw, gv = _zero_disconnected([w, v], g_outputs)
return [EighGrad(self.UPLO)(x, w, v, gw, gv)] return [EighGrad(self.UPLO)(x, w, v, gw, gv)]
def eigh(a, UPLO='L'): def eigh(a, UPLO='L'):
return Eigh(UPLO)(a) return Eigh(UPLO)(a)
class EighGrad(Op): class EighGrad(Op):
"""Gradient of an eigensystem of a Hermitian matrix. """Gradient of an eigensystem of a Hermitian matrix.
...@@ -1022,7 +1031,6 @@ class EighGrad(Op): ...@@ -1022,7 +1031,6 @@ class EighGrad(Op):
def __str__(self): def __str__(self):
return 'EighGrad{%s}' % self.UPLO return 'EighGrad{%s}' % self.UPLO
def make_node(self, x, w, v, gw, gv): def make_node(self, x, w, v, gw, gv):
x, w, v, gw, gv = map(as_tensor_variable, (x, w, v, gw, gv)) x, w, v, gw, gv = map(as_tensor_variable, (x, w, v, gw, gv))
return Apply(self, [x, w, v, gw, gv], [x.type()]) return Apply(self, [x, w, v, gw, gv], [x.type()])
...@@ -1036,9 +1044,9 @@ class EighGrad(Op): ...@@ -1036,9 +1044,9 @@ class EighGrad(Op):
N = x.shape[0] N = x.shape[0]
outer = numpy.outer outer = numpy.outer
G = lambda n: sum(v[:,m]*V.T[n].dot(v[:,m])/(w[n]-w[m]) G = lambda n: sum(v[:, m] * V.T[n].dot(v[:, m]) / (w[n] - w[m])
for m in xrange(N) if m != n) for m in xrange(N) if m != n)
g = sum(outer(v[:,n], v[:,n]*W[n] + G(n)) g = sum(outer(v[:, n], v[:, n] * W[n] + G(n))
for n in xrange(N)) for n in xrange(N))
# Numpy's eigh(a, 'L') (eigh(a, 'U')) is a function of tril(a) # Numpy's eigh(a, 'L') (eigh(a, 'U')) is a function of tril(a)
...@@ -1053,4 +1061,3 @@ class EighGrad(Op): ...@@ -1053,4 +1061,3 @@ class EighGrad(Op):
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
return [shapes[0]] return [shapes[0]]
...@@ -185,6 +185,7 @@ def test_inverse_grad(): ...@@ -185,6 +185,7 @@ def test_inverse_grad():
r = rng.randn(4, 4) r = rng.randn(4, 4)
tensor.verify_grad(matrix_inverse, [r], rng=numpy.random) tensor.verify_grad(matrix_inverse, [r], rng=numpy.random)
def test_rop_lop(): def test_rop_lop():
mx = tensor.matrix('mx') mx = tensor.matrix('mx')
mv = tensor.matrix('mv') mv = tensor.matrix('mv')
...@@ -442,6 +443,7 @@ def test_spectral_radius_bound(): ...@@ -442,6 +443,7 @@ def test_spectral_radius_bound():
ok = True ok = True
assert ok assert ok
class test_Solve(utt.InferShapeTester): class test_Solve(utt.InferShapeTester):
def setUp(self): def setUp(self):
super(test_Solve, self).setUp() super(test_Solve, self).setUp()
...@@ -474,10 +476,12 @@ class test_Solve(utt.InferShapeTester): ...@@ -474,10 +476,12 @@ class test_Solve(utt.InferShapeTester):
dtype=config.floatX)], dtype=config.floatX)],
self.op_class) self.op_class)
class test_Eig(utt.InferShapeTester): class test_Eig(utt.InferShapeTester):
op_class = Eig op_class = Eig
op = eig op = eig
dtype = 'float64' dtype = 'float64'
def setUp(self): def setUp(self):
super(test_Eig, self).setUp() super(test_Eig, self).setUp()
self.rng = numpy.random.RandomState(utt.fetch_seed()) self.rng = numpy.random.RandomState(utt.fetch_seed())
...@@ -494,6 +498,7 @@ class test_Eig(utt.InferShapeTester): ...@@ -494,6 +498,7 @@ class test_Eig(utt.InferShapeTester):
# S must be square # S must be square
[S], [S],
self.op_class) self.op_class)
def test_eval(self): def test_eval(self):
import math import math
A = theano.tensor.matrix(dtype=self.dtype) A = theano.tensor.matrix(dtype=self.dtype)
...@@ -501,18 +506,20 @@ class test_Eig(utt.InferShapeTester): ...@@ -501,18 +506,20 @@ class test_Eig(utt.InferShapeTester):
[[1.0], [[1.0]]]) [[1.0], [[1.0]]])
x = [[0, 1], [1, 0]] x = [[0, 1], [1, 0]]
w, v = [e.eval({A: x}) for e in self.op(A)] w, v = [e.eval({A: x}) for e in self.op(A)]
assert_array_almost_equal(numpy.dot(x,v), w * v) assert_array_almost_equal(numpy.dot(x, v), w * v)
class test_Eigh(test_Eig): class test_Eigh(test_Eig):
op = staticmethod(eigh) op = staticmethod(eigh)
def test_uplo(self): def test_uplo(self):
S = self.S S = self.S
a = theano.tensor.matrix() a = theano.tensor.matrix(dtype=self.dtype)
wu, vu = [out.eval({a: S}) for out in self.op(a, 'U')] wu, vu = [out.eval({a: S}) for out in self.op(a, 'U')]
wl, vl = [out.eval({a: S}) for out in self.op(a, 'L')] wl, vl = [out.eval({a: S}) for out in self.op(a, 'L')]
assert_array_almost_equal(wu, wl) assert_array_almost_equal(wu, wl)
assert_array_almost_equal(vu*numpy.sign(vu[0,:]), assert_array_almost_equal(vu * numpy.sign(vu[0, :]),
vl*numpy.sign(vl[0,:])) vl * numpy.sign(vl[0, :]))
def test_grad(self): def test_grad(self):
S = self.S S = self.S
...@@ -521,5 +528,6 @@ class test_Eigh(test_Eig): ...@@ -521,5 +528,6 @@ class test_Eigh(test_Eig):
utt.verify_grad(lambda x: self.op(x, 'U')[0], [S], rng=self.rng) utt.verify_grad(lambda x: self.op(x, 'U')[0], [S], rng=self.rng)
utt.verify_grad(lambda x: self.op(x, 'U')[1], [S], rng=self.rng) utt.verify_grad(lambda x: self.op(x, 'U')[1], [S], rng=self.rng)
class test_Eigh_float32(test_Eigh): class test_Eigh_float32(test_Eigh):
dtype = 'float32' dtype = 'float32'
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论