提交 b34a04b9 authored 作者: James Bergstra's avatar James Bergstra

moving solve to sandbox

上级 0d8780b9
import numpy
from theano import gof, tensor
import unittest
class Solve(gof.Op):
"""
Find the solution to the linear equation Ax=b,
where A is a 2d matrix and b is a 1d or 2d matrix.
It use numpy.solve to find the solution.
"""
def make_node(self, A, b):
if not isinstance(A, gof.Variable) or not A.type==tensor.matrix().type:
raise TypeError("We expected that A had a matrix type")
if not isinstance(B, gof.Variable) or not B.type==tensor.matrix().type:
raise TypeError("We expected that B had a matrix type")
node = gof.Apply(op=self, inputs=[A, B], outputs=[tensor.matrix()])
return node
def perform(self, node, (A, B), (output, )):
ret=numpy.solve(A,B)
output[0]=ret
def grad(self, (theta, A, B), (gtheta,)):
raise NotImplementedError()
solve = Solve()
class T_solve(unittest.TestCase):
def setUp(self):
self.rng = numpy.random.RandomState(utt.fetch_seed(666))
def test0(self):
A=self.rng.randn(5,5)
b=numpy.array(range(5),dtype=float)
x=numpy.linalg.solve(A,b)
Ax = numpy.dot(A,x)
are = tensor.numeric_grad.abs_rel_err(Ax, b)
self.failUnless(numpy.all(are < 1.0e-5), (are, Ax, b))
#print A,b
#print numpy.dot(A,x)
...@@ -1272,32 +1272,6 @@ prepend_scalar_to_each_row = Prepend_scalar_to_each_row() ...@@ -1272,32 +1272,6 @@ prepend_scalar_to_each_row = Prepend_scalar_to_each_row()
prepend_0_to_each_row = Prepend_scalar_constant_to_each_row(0.) prepend_0_to_each_row = Prepend_scalar_constant_to_each_row(0.)
prepend_1_to_each_row = Prepend_scalar_constant_to_each_row(1.) prepend_1_to_each_row = Prepend_scalar_constant_to_each_row(1.)
class solve(gof.Op):
"""
Find the solution to the linear equation Ax=b,
where A is a 2d matrix and b is a 1d or 2d matrix.
It use numpy.solve to find the solution.
"""
def make_node(self, A, b):
if not isinstance(A, gof.Variable) or not A.type==tensor.matrix().type:
raise TypeError("We expected that A had a matrix type")
if not isinstance(B, gof.Variable) or not B.type==tensor.matrix().type:
raise TypeError("We expected that B had a matrix type")
node = gof.Apply(op=self, inputs=[A, B], outputs=[tensor.matrix()])
return node
def perform(self, node, (A, B), (output, )):
ret=numpy.solve(A,B)
output[0]=ret
def grad(self, (theta, A, B), (gtheta,)):
raise NotImplementedError()
logsigm_to_softplus = gof.PatternSub( logsigm_to_softplus = gof.PatternSub(
(tensor.log, (sigmoid, 'x')), (tensor.log, (sigmoid, 'x')),
(tensor.neg, (softplus, (tensor.neg, 'x'))), (tensor.neg, (softplus, (tensor.neg, 'x'))),
......
...@@ -108,21 +108,6 @@ class T_prepend(unittest.TestCase): ...@@ -108,21 +108,6 @@ class T_prepend(unittest.TestCase):
self.failUnless(my.shape == (3, 6)) self.failUnless(my.shape == (3, 6))
self.failUnless(numpy.all(my[:,0] == 5.0)) self.failUnless(numpy.all(my[:,0] == 5.0))
class T_solve(unittest.TestCase):
def setUp(self):
self.rng = numpy.random.RandomState(utt.fetch_seed(666))
def test0(self):
A=self.rng.randn(5,5)
b=numpy.array(range(5),dtype=float)
x=numpy.linalg.solve(A,b)
Ax = numpy.dot(A,x)
are = T.numeric_grad.abs_rel_err(Ax, b)
self.failUnless(numpy.all(are < 1.0e-5), (are, Ax, b))
#print A,b
#print numpy.dot(A,x)
class T_CrossentropyCategorical1Hot(unittest.TestCase): class T_CrossentropyCategorical1Hot(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论