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

Added norm function.

上级 334081ee
...@@ -1215,8 +1215,93 @@ def eigvalsh(a, b, lower=True): ...@@ -1215,8 +1215,93 @@ def eigvalsh(a, b, lower=True):
return Eigvalsh(lower)(a, b) return Eigvalsh(lower)(a, b)
<<<<<<< HEAD
def matrix_power(M, n): def matrix_power(M, n):
result = 1 result = 1
for i in xrange(n): for i in xrange(n):
result = theano.dot(result, M) result = theano.dot(result, M)
return result return result
=======
def norm(x,ord,axis):
x = as_tensor_variable(x)
ndim = x.ndim
if ndim == 0:
raise TypeError
elif ndim == 1:
if ord == None:
z = (tensor.sum(x**2))**(0.5)
return z
elif ord == 'inf':
z = tensor.max(abs(x))
return z
elif ord == '-inf':
z = tensor.min(abs(x))
return z
elif ord == 0:
z = (x[x.nonzero()]).shape[0]
return z
else:
try:
z = (tensor.sum(abs(x**ord)))**(1./ord)
except TypeError:
raise ValueError
return z
elif ndim >= 2:
if axis == None:
if ord == None or ord == 'fro':
z = (tensor.sum(abs(x**2)))**(0.5)
return z
elif ord == 'inf':
z = tensor.sum(abs(x),1)
return tensor.max(z)
elif ord == '-inf':
z = tensor.sum(abs(x),1)
return tensor.min(z)
elif ord == 1:
z = tensor.sum(abs(x),0)
return tensor.max(z)
elif ord == -1:
z = tensor.sum(abs(x),0)
return tensor.min(z)
else:
raise ValueError(0)
else:
try:
axis + 1
if ord == None:
z = (tensor.sum(x**2,axis))**(0.5)
return z
elif ord == 'inf':
z = tensor.max(abs(x),axis)
return z
elif ord == '-inf':
z = tensor.min(abs(x),axis)
return z
elif ord == 0:
z = tensor.sum(tensor.neq(x,0),1)
return z
else:
try:
z = (tensor.sum(abs(x**ord),axis))**(1./ord)
except TypeError:
raise ValueError
return z
except TypeError:
if ord == None or ord == 'fro':
z = (tensor.sum(abs(x**2),axis))**(0.5)
return z
elif ord == 'inf':
z = tensor.sum(abs(x),1)
return tensor.max(z,axis)
elif ord == '-inf':
z = tensor.sum(abs(x),1)
return tensor.min(z,axis)
elif ord == 1:
z = tensor.sum(abs(x),0)
return tensor.max(z,axis)
elif ord == -1:
z = tensor.sum(abs(x),0)
return tensor.min(z,axis)
else:
raise ValueError(0)
>>>>>>> Added norm function.
...@@ -31,7 +31,9 @@ from theano.sandbox.linalg.ops import (cholesky, ...@@ -31,7 +31,9 @@ from theano.sandbox.linalg.ops import (cholesky,
imported_scipy, imported_scipy,
Eig, Eig,
inv_as_solve inv_as_solve
norm
) )
from theano.sandbox.linalg import eig, eigh, eigvalsh from theano.sandbox.linalg import eig, eigh, eigvalsh
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
...@@ -611,6 +613,7 @@ def test_eigvalsh_grad(): ...@@ -611,6 +613,7 @@ def test_eigvalsh_grad():
[a, b], rng=numpy.random) [a, b], rng=numpy.random)
<<<<<<< HEAD
class Matrix_power(): class Matrix_power():
def test_numpy_compare(self): def test_numpy_compare(self):
...@@ -631,3 +634,19 @@ class Matrix_power(): ...@@ -631,3 +634,19 @@ class Matrix_power():
f = function([A], [Q]) f = function([A], [Q])
a = rng.rand(4, 3).astype(theano.config.floatX) a = rng.rand(4, 3).astype(theano.config.floatX)
self.assertRaises(ValueError, f, a) 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',0)
def test_wrong_type_of_ord_for_vector_in_matrix(self):
self.assertRaises(ValueError, norm, [[2,1],[3,4]],'fro',0)
def test_wrong_type_of_ord_for_vector_in_tensor(self):
self.assertRaises(ValueError, norm, [[[2,1],[3,4]],[[6,5],[7,8]]],'fro',0)
def test_wrong_type_of_ord_for_matrix(self):
self.assertRaises(ValueError, norm, [[2,1],[3,4]],0,None)
def test_wrong_type_of_ord_for_matrix_in_tensor(self):
self.assertRaises(ValueError, norm, [[[2,1],[3,4]],[[6,5],[7,8]]],0,None)
def test_non_tensorial_input(self):
self.assertRaises(TypeError, norm, 3, None, None)
def test_no_enough_dimensions(self):
self.assertRaises(ValueError, norm, [[2,1],[3,4]], None, 3)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论