提交 3d9074ac authored 作者: Olivier Breuleux's avatar Olivier Breuleux

fixed stuff

上级 d92eb12b
......@@ -6,75 +6,8 @@ from compile import *
from scalar import *
import tensor
# class Double(gof.result.Result):
# def __init__(self, data, name = "oignon"):
# assert isinstance(data, float)
# gof.result.Result.__init__(self, role = None, name = name)
# self.data = data
# def __str__(self):
# return self.name
# def __repr__(self):
# return self.name
# def __copy__(self):
# return self.__class__(self.data, self.name)
# class MyOp(gof.op.Op):
# nin = -1
# def __init__(self, *inputs):
# assert len(inputs) == self.nin
# for input in inputs:
# if not isinstance(input, Double):
# raise Exception("Error 1")
# self.inputs = inputs
# self.outputs = [Double(0.0, self.__class__.__name__ + "_R")]
# def perform(self):
# self.outputs[0].data = self.impl(*[input.data for input in self.inputs])
# class Unary(MyOp):
# nin = 1
# class Binary(MyOp):
# nin = 2
# class Add(Binary):
# def impl(self, x, y):
# return x + y
# class Sub(Binary):
# def impl(self, x, y):
# return x - y
# class Mul(Binary):
# def impl(self, x, y):
# return x * y
# class Div(Binary):
# def impl(self, x, y):
# return x / y
# def env(inputs, outputs, validate = True, features = []):
# return gof.env.Env(inputs, outputs, features = features, consistency_check = validate)
# def perform_linker(env):
# lnk = gof.link.PerformLinker(env)
# return lnk
# def graph1(): # (x+y) * (x/z)
# x = gof.modes.build(Double(1.0, 'x'))
# y = gof.modes.build(Double(3.0, 'y'))
# z = gof.modes.build(Double(4.0, 'z'))
# o = Mul(Add(x, y).out, Div(x, z).out).out
# return [x,y,z], [o]
PatternOptimizer = lambda p1, p2, ign=True: gof.OpKeyOptimizer(gof.PatternSub(p1, p2), ignore_newtrees=ign)
def graph1(): # (x+y) * (x/z)
......@@ -90,23 +23,8 @@ class T_Function(unittest.TestCase):
p = function(gi, go, optimizer = None, linker = 'py')
self.failUnless(p(1.0,3.0,4.0) == 1.0)
# def test_link_noopt(self):
# gi, go = graph1()
# fn, i, o = perform_linker(env(gi, go)).make_thunk(True)
# fn()
# self.failUnless(go[0].data == 1.0)
# def test_link_opt(self):
# opt = gof.opt.PatternOptimizer((Div, '1', '2'), (Div, '2', '1'))
# gi, go = graph1()
# e = env(gi, go)
# opt.optimize(e)
# fn, i, o = perform_linker(e).make_thunk(True)
# fn()
# self.failUnless(go[0].data == 16.0)
def test_opt(self):
opt = gof.opt.PatternOptimizer((div, '1', '2'), (div, '2', '1'))
opt = PatternOptimizer((div, '1', '2'), (div, '2', '1'))
gi, go = graph1()
p = function(gi,go, optimizer=opt.optimize, linker = 'py')
self.failUnless(p(1.,3.,4.) == 16.0)
......@@ -116,7 +34,7 @@ class T_Function(unittest.TestCase):
x, y, z = floats('xyz')
o = mul(add(x, y), div(x, z))
return [x,y,z], [o, o.owner.inputs[1]]
opt = gof.opt.PatternOptimizer((div, '1', '2'), (div, '2', '1'))
opt = PatternOptimizer((div, '1', '2'), (div, '2', '1'))
gi, go = graph2()
p = function(gi,go, optimizer=opt.optimize)
a,b = p(1.,3.,4.)
......@@ -211,46 +129,6 @@ class T_fast_compute(unittest.TestCase):
assert compile._fcache[(e, )]() == 14.0
# def test_orphans(self):
# gi, go = graph1()
# opt = None
# p0 = function(gi[0:0], go, optimizer = None, linker = 'py')
# self.failUnless(p0() == 1.0)
# p3 = Function(gi,go)
# p2 = Function(gi[0:2], go)
# p1 = Function(gi[0:1], go)
# try:
# self.failUnless(p3() == 6.0)
# self.fail()
# except TypeError, e:
# self.failUnless(e[0].split()[0:3] == ['Function','call', 'takes'])
# self.failUnless(p3(1.,3.,4.) == 1.0)
# self.failUnless(p2(1.,3.) == 1.0)
# self.failUnless(p1(1.,) == 1.0)
# def test_some_constant_outputs(self):
# x = gof.modes.build(Double(1.0, 'x'))
# y = gof.modes.build(Double(3.0, 'y'))
# z = gof.modes.build(Double(4.0, 'z'))
# xy = Mul(x,y).out
# zz = Mul(z,z).out
# p0 = Function([x,y], [xy, zz])
# self.failUnless(p0(1.,3.) == [3.0,16.0])
# self.failUnless(p0(1.5,4.) == [6.0,16.0])
# self.failUnless(x.data == 1.0)
# self.failUnless(y.data == 3.0)
# self.failUnless(z.data == 4.0)
# p1 = Function([z], [xy, zz],unpack_single=False)
# self.failUnless(p1(4.) == [3.0,16.0]) #returns 6.0, 16.10
# self.failUnless(p1(5.) == [3.0,25.0])
if __name__ == '__main__':
......
......@@ -2,7 +2,7 @@
import time
import unittest
from gof import Result, Op, Env
from gof import Result, Op
import gof
from scalar import *
......@@ -11,6 +11,10 @@ import tensor
from elemwise import *
def Env(i, o):
e = gof.Env(i, o)
gof.ExpandMacros().optimize(e)
return e
class _test_DimShuffle(unittest.TestCase):
......
......@@ -174,7 +174,7 @@ class DimShuffle(Op):
def make_node(self, input):
ib = tuple(input.type.broadcastable)
if not ib == self.input_broadcastable:
raise TypeError("The number of dimensions and/or broadcastable pattern of the input is incorrect for this op. Expected %s, got %s." % (ib, self.input_broadcastable))
raise TypeError("The number of dimensions and/or broadcastable pattern of the input is incorrect for this op. Expected %s, got %s." % (self.input_broadcastable, ib))
ob = []
for value in self.new_order:
if value == 'x':
......@@ -327,10 +327,10 @@ class Elemwise(Op):
# args.append(DimShuffle(input.type.broadcastable, ['x']*difference + range(length))(input))
# inputs = args
try:
assert len(set([len(input.type.broadcastable) for input in inputs])) == 1
except (AssertionError, AttributeError):
raise TypeError("All inputs to a Broadcast subclass must be Tensor instances and their broadcastable fields must all have the same length.", inputs)
# try:
# assert len(set([len(input.type.broadcastable) for input in inputs])) == 1
# except (AssertionError, AttributeError):
# raise TypeError("All inputs to a Broadcast subclass must be Tensor instances and their broadcastable fields must all have the same length.", inputs)
out_broadcastables = [[all(bcast) for bcast in zip(*[input.type.broadcastable for input in inputs])]] * shadow.nout
inplace_pattern = self.inplace_pattern
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论