提交 4b8b01c6 authored 作者: Matthew Rocklin's avatar Matthew Rocklin

remove new_io_toposort from active code.

Add docstrings Remove dependence function by default. Can now sort by anything. Does not respect dependence by default.
上级 479614ec
......@@ -554,14 +554,9 @@ class FunctionGraph(utils.object2):
return list(self.apply_nodes)
fg = self
# TODO - This is a hack.
# Should change deeper in to just move around functions
ords = self.orderings()
ords = [lambda a,b: (1 if b in d.get(a, ())
else -1 if a in d.get(b, ())
else 0) for d in ords]
order = graph.new_io_toposort(fg.inputs, fg.outputs, ords)
order = graph.io_toposort(fg.inputs, fg.outputs, ords)
return order
def orderings(self):
......
......@@ -1028,7 +1028,6 @@ def memodict(f):
return memodict().__getitem__
## end of http://code.activestate.com/recipes/578231/ }}}
@memodict
def depends((a, b)):
return (not set(a.inputs).isdisjoint(set(b.outputs))
......@@ -1046,11 +1045,27 @@ def dependence(a, b):
if depends((b, a)): return -1
return 0
def new_io_toposort(inputs, outputs, cmps=[]):
""" Same as io_toposort """
cmps = [dependence] + cmps # enforce that dependence is the strongest cmp fn
def sort_apply_nodes(inputs, outputs, cmps):
""" Order a graph of apply nodes according to a list of comparators
The following example sorts first by dependence of nodes (this is a
topological sort) and then by lexicographical ordering (nodes that start
with 'E' come before nodes that start with 'I' if there is no dependence.
>>> from theano.gof.graph import sort_apply_nodes, dependence
>>> from theano.tensor import matrix, dot
>>> x = matrix('x')
>>> y = dot(x*2, x+1)
>>> str_cmp = lambda a, b: cmp(str(a), str(b)) # lexicographical sort
>>> sort_apply_nodes([x], [y], cmps=[dependence, str_cmp])
[Elemwise{add,no_inplace}(x, InplaceDimShuffle{x,x}.0),
InplaceDimShuffle{x,x}(TensorConstant{2}),
Elemwise{mul,no_inplace}(x, InplaceDimShuffle{x,x}.0),
InplaceDimShuffle{x,x}(TensorConstant{1}),
dot(Elemwise{mul,no_inplace}.0, Elemwise{add,no_inplace}.0)]
"""
# An aggregate comparator - looks at each cmp in order
# An aggregate comparator - looks at each cmp function in order
def cmp(a,b, fns=cmps):
if not fns: return 0
head, tail = fns[0], fns[1:]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论