def test_1(self): x, y, z = map(MyVariable, 'xyz') e = op3(op4(x, y)) g = FunctionGraph([x, y, z], [e]) # print g opt = EquilibriumOptimizer([ PatternSub((op1, 'x', 'y'), (op2, 'x', 'y')), PatternSub((op4, 'x', 'y'), (op1, 'x', 'y')), PatternSub((op3, (op2, 'x', 'y')), (op4, 'x', 'y')) ], max_use_ratio=10) opt.optimize(g) # print g assert str(g) == '[Op2(x, y)]'
def test_1(self): x, y, z = map(MyVariable, "xyz") e = op3(op4(x, y)) g = FunctionGraph([x, y, z], [e]) # print g opt = EquilibriumOptimizer( [ PatternSub((op1, "x", "y"), (op2, "x", "y")), PatternSub((op4, "x", "y"), (op1, "x", "y")), PatternSub((op3, (op2, "x", "y")), (op4, "x", "y")), ], max_use_ratio=10, ) opt.optimize(g) # print g assert str(g) == "FunctionGraph(Op2(x, y))"
def test_low_use_ratio(self): x, y, z = map(MyVariable, "xyz") e = op3(op4(x, y)) g = FunctionGraph([x, y, z], [e]) # print 'before', g # display pesky warnings along with stdout # also silence logger for 'theano.gof.opt' _logger = logging.getLogger("theano.gof.opt") oldlevel = _logger.level _logger.setLevel(logging.CRITICAL) try: opt = EquilibriumOptimizer( [ PatternSub((op1, "x", "y"), (op2, "x", "y")), PatternSub((op4, "x", "y"), (op1, "x", "y")), PatternSub((op3, (op2, "x", "y")), (op4, "x", "y")), ], max_use_ratio=1.0 / len(g.apply_nodes), ) # each opt can only be applied once opt.optimize(g) finally: _logger.setLevel(oldlevel) # print 'after', g assert str(g) == "FunctionGraph(Op1(x, y))"
def PatternOptimizer(p1, p2, ign=True): return OpKeyOptimizer(PatternSub(p1, p2), ignore_newtrees=ign)
def TopoPatternOptimizer(p1, p2, ign=True): return TopoOptimizer(PatternSub(p1, p2), ignore_newtrees=ign)