提交 43cfa11a authored 作者: Tanjay94's avatar Tanjay94

Fixed swapaxes function in var, indentation error in test_basic and added lstsq…

Fixed swapaxes function in var, indentation error in test_basic and added lstsq function in ops with tests.
上级 26f2112e
......@@ -1215,14 +1215,14 @@ def eigvalsh(a, b, lower=True):
return Eigvalsh(lower)(a, b)
<<<<<<< HEAD
def matrix_power(M, n):
result = 1
for i in xrange(n):
result = theano.dot(result, M)
return result
=======
def norm(x,ord,axis):
def norm(x,ord):
x = as_tensor_variable(x)
ndim = x.ndim
if ndim == 0:
......@@ -1246,62 +1246,50 @@ def norm(x,ord,axis):
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)
elif ndim == 2:
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:
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),axis)
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.
raise ValueError(0)
class lstsq(theano.Op):
def __eq__(self, other):
pass
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y, rcond):
x = theano.tensor.as_tensor_variable(x)
y = theano.tensor.as_tensor_variable(y)
rcond = theano.tensor.as_tensor_variable(rcond)
return theano.Apply(self, [x, y, rcond], [y.type(), theano.tensor.dvector(), theano.tensor.lscalar(), theano.tensor.dvector()])
def perform(self, node, inputs, outputs):
x = inputs[0]
y = inputs[1]
rcond = inputs[2]
zz = numpy.linalg.lstsq(inputs[0], inputs[1], inputs[2])
outputs[0][0] = zz[0]
outputs[1][0] = zz[1]
outputs[2][0] = zz[2]
outputs[3][0] = zz[3]
>>>>>>> Fixed swapaxes function in var, indentation error in test_basic and added lstsq function in ops with tests.
......@@ -651,6 +651,34 @@ class T_NormTests(unittest.TestCase):
def test_no_enough_dimensions(self):
self.assertRaises(ValueError, norm, [[2,1],[3,4]], None, 3)
def test(self):
x = theano.tensor.matrix()
x.swapaxes()
def test():
x = theano.tensor.matrix()
x.swapaxes(0,1)
class T_lstsq(unittest.TestCase):
def test_correct_solution(self):
x = tensor.lmatrix()
y = tensor.lmatrix()
z = tensor.lscalar()
b = theano.sandbox.linalg.ops.lstsq()(x,y,z)
f = function([x,y,z], b)
TestMatrix1 = numpy.asarray([[2,1],[3,4]])
TestMatrix2 = numpy.asarray([[17,20],[43,50]])
TestScalar = numpy.asarray(1)
f = function([x,y,z], b)
m = f(TestMatrix1, TestMatrix2, TestScalar)
self.assertTrue(numpy.allclose(TestMatrix2, numpy.dot(TestMatrix1, m[0])))
def test_wrong_coefficient_matrix(self):
x = tensor.vector()
y = tensor.vector()
z = tensor.scalar()
b = theano.sandbox.linalg.ops.lstsq()(x, y, z)
f = function([x, y, z], b)
self.assertRaises(numpy.linalg.linalg.LinAlgError, f, [2,1],[2,1],1)
def test_wrong_rcond_dimension(self):
x = tensor.vector()
y = tensor.vector()
z = tensor.vector()
b = theano.sandbox.linalg.ops.lstsq()(x, y, z)
f = function([x, y, z], b)
self.assertRaises(numpy.linalg.LinAlgError, f, [2,1],[2,1],[2,1])
......@@ -46,7 +46,9 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
itensor3, Tile, switch, Diagonal, Diag,
nonzero, flatnonzero, nonzero_values,
stacklists, DimShuffle, hessian, ptp, power,
swapaxes)
swapaxes
)
from theano.tests import unittest_tools as utt
......
......@@ -556,7 +556,13 @@ class _tensor_py_operators:
"""see 'theano.tensor.ptp'"""
return theano.tensor.ptp(self, axis)
def swapaxes(self, axis1, axis2):
"""Return 'tensor.swapaxes(self, axis1, axis2)
If a matrix is provided with the right axes, its transpose will be returned.
"""
return theano.tensor.basic.swapaxes(self, axis1, axis2)
class TensorVariable(_tensor_py_operators, Variable):
"""Subclass to add the tensor operators to the basic `Variable` class."""
......@@ -699,11 +705,4 @@ class TensorConstant(_tensor_py_operators, Constant):
copy.deepcopy(self.data, memo),
copy.deepcopy(self.name, memo))
def swapaxes(self, axis1, axis2):
"""Return 'tensor.swapaxes(self, axis1, axis2)
If a matrix is provided with the right axes, its transpose will be returned.
"""
return theano.tensor.basic.swapaxes(self, axis1, axis2)
TensorType.Constant = TensorConstant
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论