提交 f3ae6acc authored 作者: Frederic Bastien's avatar Frederic Bastien

white space fix.

上级 8188f49f
...@@ -33,7 +33,7 @@ class T_extending(unittest.TestCase): ...@@ -33,7 +33,7 @@ class T_extending(unittest.TestCase):
return float(x) return float(x)
def values_eq_approx(x, y, tolerance=1e-4): def values_eq_approx(x, y, tolerance=1e-4):
return abs(x - y) / (abs(x) + abs(y)) < tolerance return abs(x - y) / (abs(x) + abs(y)) < tolerance
from theano import gof from theano import gof
...@@ -47,13 +47,13 @@ class T_extending(unittest.TestCase): ...@@ -47,13 +47,13 @@ class T_extending(unittest.TestCase):
class Double(gof.Type): class Double(gof.Type):
def filter(self, x, strict=False): def filter(self, x, strict=False):
if strict and not isinstance(x, float): if strict and not isinstance(x, float):
raise TypeError('Expected a float!') raise TypeError('Expected a float!')
return float(x) return float(x)
def values_eq_approx(self, x, y, tolerance=1e-4): def values_eq_approx(self, x, y, tolerance=1e-4):
return abs(x - y) / (abs(x) + abs(y)) < tolerance return abs(x - y) / (abs(x) + abs(y)) < tolerance
double = Double() double = Double()
...@@ -67,16 +67,16 @@ class T_extending(unittest.TestCase): ...@@ -67,16 +67,16 @@ class T_extending(unittest.TestCase):
class Double(gof.Type): class Double(gof.Type):
def filter(self, x, strict=False, allow_downcast=None): def filter(self, x, strict=False, allow_downcast=None):
if strict and not isinstance(x, float): if strict and not isinstance(x, float):
raise TypeError('Expected a float!') raise TypeError('Expected a float!')
return float(x) return float(x)
def values_eq_approx(self, x, y, tolerance=1e-4): def values_eq_approx(self, x, y, tolerance=1e-4):
return abs(x - y) / (abs(x) + abs(y)) < tolerance return abs(x - y) / (abs(x) + abs(y)) < tolerance
def __str__(self): def __str__(self):
return "double" return "double"
double = Double() double = Double()
...@@ -107,13 +107,13 @@ class T_extending(unittest.TestCase): ...@@ -107,13 +107,13 @@ class T_extending(unittest.TestCase):
def make_node(x, y): def make_node(x, y):
if isinstance(x, (int, float)): if isinstance(x, (int, float)):
x = gof.Constant(double, x) x = gof.Constant(double, x)
if isinstance(y, (int, float)): if isinstance(y, (int, float)):
y = gof.Constant(double, y) y = gof.Constant(double, y)
if x.type != double or y.type != double: if x.type != double or y.type != double:
raise TypeError('mul only works on doubles') raise TypeError('mul only works on doubles')
return gof.Apply(mul, [x, y], [double()]) return gof.Apply(mul, [x, y], [double()])
mul.make_node = make_node mul.make_node = make_node
...@@ -125,30 +125,30 @@ class T_extending(unittest.TestCase): ...@@ -125,30 +125,30 @@ class T_extending(unittest.TestCase):
from theano import gof from theano import gof
class BinaryDoubleOp(gof.Op): class BinaryDoubleOp(gof.Op):
def __init__(self, name, fn): def __init__(self, name, fn):
self.name = name self.name = name
self.fn = fn self.fn = fn
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and (self.name == other.name) and (self.fn == other.fn) return type(self) == type(other) and (self.name == other.name) and (self.fn == other.fn)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.name) ^ hash(self.fn) return hash(type(self)) ^ hash(self.name) ^ hash(self.fn)
def make_node(self, x, y): def make_node(self, x, y):
if isinstance(x, (int, float)): if isinstance(x, (int, float)):
x = gof.Constant(double, x) x = gof.Constant(double, x)
if isinstance(y, (int, float)): if isinstance(y, (int, float)):
y = gof.Constant(double, y) y = gof.Constant(double, y)
if x.type != double or y.type != double: if x.type != double or y.type != double:
raise TypeError('%s only works on doubles' % self.name) raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()]) return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )): def perform(self, node, (x, y), (z, )):
z[0] = self.fn(x, y) z[0] = self.fn(x, y)
def __str__(self): def __str__(self):
return self.name return self.name
add = BinaryDoubleOp(name = 'add', add = BinaryDoubleOp(name = 'add',
fn = lambda x, y: x + y) fn = lambda x, y: x + y)
...@@ -175,44 +175,44 @@ class T_extending(unittest.TestCase): ...@@ -175,44 +175,44 @@ class T_extending(unittest.TestCase):
class Double(gof.Type): class Double(gof.Type):
def filter(self, x, strict=False, allow_downcast=None): def filter(self, x, strict=False, allow_downcast=None):
if strict and not isinstance(x, float): if strict and not isinstance(x, float):
raise TypeError('Expected a float!') raise TypeError('Expected a float!')
return float(x) return float(x)
def values_eq_approx(self, x, y, tolerance=1e-4): def values_eq_approx(self, x, y, tolerance=1e-4):
return abs(x - y) / (abs(x) + abs(y)) < tolerance return abs(x - y) / (abs(x) + abs(y)) < tolerance
def __str__(self): def __str__(self):
return "double" return "double"
double = Double() double = Double()
class BinaryDoubleOp(gof.Op): class BinaryDoubleOp(gof.Op):
def __init__(self, name, fn): def __init__(self, name, fn):
self.name = name self.name = name
self.fn = fn self.fn = fn
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and (self.name == other.name) and (self.fn == other.fn) return type(self) == type(other) and (self.name == other.name) and (self.fn == other.fn)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.name) ^ hash(self.fn) return hash(type(self)) ^ hash(self.name) ^ hash(self.fn)
def make_node(self, x, y): def make_node(self, x, y):
if isinstance(x, (int, float)): if isinstance(x, (int, float)):
x = gof.Constant(double, x) x = gof.Constant(double, x)
if isinstance(y, (int, float)): if isinstance(y, (int, float)):
y = gof.Constant(double, y) y = gof.Constant(double, y)
if x.type != double or y.type != double: if x.type != double or y.type != double:
raise TypeError('%s only works on doubles' % self.name) raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()]) return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )): def perform(self, node, (x, y), (z, )):
z[0] = self.fn(x, y) z[0] = self.fn(x, y)
def __str__(self): def __str__(self):
return self.name return self.name
add = BinaryDoubleOp(name = 'add', add = BinaryDoubleOp(name = 'add',
fn = lambda x, y: x + y) fn = lambda x, y: x + y)
...@@ -281,87 +281,87 @@ class T_extending(unittest.TestCase): ...@@ -281,87 +281,87 @@ class T_extending(unittest.TestCase):
from theano import gof from theano import gof
class Double(gof.Type): class Double(gof.Type):
def filter(self, x, strict=False, allow_downcast=None): def filter(self, x, strict=False, allow_downcast=None):
if strict and not isinstance(x, float): if strict and not isinstance(x, float):
raise TypeError('Expected a float!') raise TypeError('Expected a float!')
return float(x) return float(x)
def values_eq_approx(self, x, y, tolerance=1e-4): def values_eq_approx(self, x, y, tolerance=1e-4):
return abs(x - y) / (x + y) < tolerance return abs(x - y) / (x + y) < tolerance
def __str__(self): def __str__(self):
return "double" return "double"
def c_declare(self, name, sub): def c_declare(self, name, sub):
return """ return """
double %(name)s; double %(name)s;
""" % dict(name = name) """ % dict(name = name)
def c_init(self, name, sub): def c_init(self, name, sub):
return """ return """
%(name)s = 0.0; %(name)s = 0.0;
""" % dict(name = name) """ % dict(name = name)
def c_extract(self, name, sub): def c_extract(self, name, sub):
return """ return """
if (!PyFloat_Check(py_%(name)s)) { if (!PyFloat_Check(py_%(name)s)) {
PyErr_SetString(PyExc_TypeError, "expected a float"); PyErr_SetString(PyExc_TypeError, "expected a float");
%(fail)s %(fail)s
} }
%(name)s = PyFloat_AsDouble(py_%(name)s); %(name)s = PyFloat_AsDouble(py_%(name)s);
""" % dict(sub, name = name) """ % dict(sub, name = name)
def c_sync(self, name, sub): def c_sync(self, name, sub):
return """ return """
Py_XDECREF(py_%(name)s); Py_XDECREF(py_%(name)s);
py_%(name)s = PyFloat_FromDouble(%(name)s); py_%(name)s = PyFloat_FromDouble(%(name)s);
if (!py_%(name)s) { if (!py_%(name)s) {
printf("PyFloat_FromDouble failed on: %%f\\n", %(name)s); printf("PyFloat_FromDouble failed on: %%f\\n", %(name)s);
Py_XINCREF(Py_None); Py_XINCREF(Py_None);
py_%(name)s = Py_None; py_%(name)s = Py_None;
} }
""" % dict(name = name) """ % dict(name = name)
def c_cleanup(self, name, sub): def c_cleanup(self, name, sub):
return "" return ""
double = Double() double = Double()
def c_code(node, name, input_names, output_names, sub): def c_code(node, name, input_names, output_names, sub):
x_name, y_name = input_names[0], input_names[1] x_name, y_name = input_names[0], input_names[1]
output_name = output_names[0] output_name = output_names[0]
return """ return """
%(output_name)s = %(x_name)s * %(y_name)s; %(output_name)s = %(x_name)s * %(y_name)s;
""" % locals() """ % locals()
mul.c_code = c_code mul.c_code = c_code
from theano import gof from theano import gof
class BinaryDoubleOp(gof.Op): class BinaryDoubleOp(gof.Op):
def __init__(self, name, fn, ccode): def __init__(self, name, fn, ccode):
self.name = name self.name = name
self.fn = fn self.fn = fn
self.ccode = ccode self.ccode = ccode
def make_node(self, x, y): def make_node(self, x, y):
if isinstance(x, (int, float)): if isinstance(x, (int, float)):
x = gof.Constant(double, x) x = gof.Constant(double, x)
if isinstance(y, (int, float)): if isinstance(y, (int, float)):
y = gof.Constant(double, y) y = gof.Constant(double, y)
if x.type != double or y.type != double: if x.type != double or y.type != double:
raise TypeError('%s only works on doubles' % self.name) raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()]) return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )): def perform(self, node, (x, y), (z, )):
z[0] = self.fn(x, y) z[0] = self.fn(x, y)
def __str__(self): def __str__(self):
return self.name return self.name
def c_code(self, node, name, (x, y), (z, ), sub): def c_code(self, node, name, (x, y), (z, ), sub):
return self.ccode % locals() return self.ccode % locals()
add = BinaryDoubleOp(name = 'add', add = BinaryDoubleOp(name = 'add',
...@@ -384,19 +384,19 @@ class T_extending(unittest.TestCase): ...@@ -384,19 +384,19 @@ class T_extending(unittest.TestCase):
from theano.gof import toolbox from theano.gof import toolbox
class Simplify(gof.Optimizer): class Simplify(gof.Optimizer):
def add_requirements(self, env): def add_requirements(self, env):
env.extend(toolbox.ReplaceValidate()) env.extend(toolbox.ReplaceValidate())
def apply(self, env): def apply(self, env):
for node in env.toposort(): for node in env.toposort():
if node.op == div: if node.op == div:
x, y = node.inputs x, y = node.inputs
z = node.outputs[0] z = node.outputs[0]
if x.owner and x.owner.op == mul: if x.owner and x.owner.op == mul:
a, b = x.owner.inputs a, b = x.owner.inputs
if y == a: if y == a:
env.replace_validate(z, b) env.replace_validate(z, b)
elif y == b: elif y == b:
env.replace_validate(z, a) env.replace_validate(z, a)
simplify = Simplify() simplify = Simplify()
x = double('x') x = double('x')
...@@ -407,21 +407,21 @@ class T_extending(unittest.TestCase): ...@@ -407,21 +407,21 @@ class T_extending(unittest.TestCase):
simplify.optimize(e) simplify.optimize(e)
class LocalSimplify(gof.LocalOptimizer): class LocalSimplify(gof.LocalOptimizer):
def transform(self, node): def transform(self, node):
if node.op == div: if node.op == div:
x, y = node.inputs x, y = node.inputs
if x.owner and x.owner.op == mul: if x.owner and x.owner.op == mul:
a, b = x.owner.inputs a, b = x.owner.inputs
if y == a: if y == a:
return [b] return [b]
elif y == b: elif y == b:
return [a] return [a]
return False return False
def tracks(self): def tracks(self):
# This should be needed for the EquilibriumOptimizer # This should be needed for the EquilibriumOptimizer
# but it isn't now # but it isn't now
# TODO: do this and explain it # TODO: do this and explain it
return [] # that's not what you should do return [] # that's not what you should do
local_simplify = LocalSimplify() local_simplify = LocalSimplify()
...@@ -595,7 +595,7 @@ class T_examples(unittest.TestCase): ...@@ -595,7 +595,7 @@ class T_examples(unittest.TestCase):
fn_of_state = state * 2 + inc fn_of_state = state * 2 + inc
foo = T.lscalar() # the type (lscalar) must match the shared variable we foo = T.lscalar() # the type (lscalar) must match the shared variable we
# are replacing with the ``givens`` list # are replacing with the ``givens`` list
skip_shared = function([inc, foo], fn_of_state, skip_shared = function([inc, foo], fn_of_state,
givens=[(state, foo)]) givens=[(state, foo)])
assert skip_shared(1, 3) == array(7) assert skip_shared(1, 3) == array(7)
...@@ -967,7 +967,3 @@ class T_graphstructures(unittest.TestCase): ...@@ -967,7 +967,3 @@ class T_graphstructures(unittest.TestCase):
assert e.owner.inputs[1] is mul_variable assert e.owner.inputs[1] is mul_variable
assert e.owner.inputs[1].owner.inputs[0] is y assert e.owner.inputs[1].owner.inputs[0] is y
assert e.owner.inputs[1].owner.inputs[1] is z assert e.owner.inputs[1].owner.inputs[1] is z
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论