提交 9f87e0c3 authored 作者: David Warde-Farley's avatar David Warde-Farley

Merge pull request #114 from jaberg/ancestors

Refactored graph.inputs + new method graph.ancestors.
...@@ -390,8 +390,6 @@ class Constant(Value): ...@@ -390,8 +390,6 @@ class Constant(Value):
return cp return cp
def stack_search(start, expand, mode='bfs', build_inv = False): def stack_search(start, expand, mode='bfs', build_inv = False):
"""Search through a graph, either breadth- or depth-first """Search through a graph, either breadth- or depth-first
...@@ -439,15 +437,15 @@ def stack_search(start, expand, mode='bfs', build_inv = False): ...@@ -439,15 +437,15 @@ def stack_search(start, expand, mode='bfs', build_inv = False):
return rval_list return rval_list
def inputs(variable_list, blockers = None): def ancestors(variable_list, blockers = None):
"""Return the inputs required to compute the given Variables. """Return the variables that contribute to those in variable_list (inclusive).
:type variable_list: list of `Variable` instances :type variable_list: list of `Variable` instances
:param variable_list: :param variable_list:
output `Variable` instances from which to search backward through owners output `Variable` instances from which to search backward through owners
:rtype: list of `Variable` instances :rtype: list of `Variable` instances
:returns: :returns:
input nodes with no owner, in the order found by a left-recursive depth-first search all input nodes, in the order found by a left-recursive depth-first search
started at the nodes in `variable_list`. started at the nodes in `variable_list`.
""" """
...@@ -457,8 +455,23 @@ def inputs(variable_list, blockers = None): ...@@ -457,8 +455,23 @@ def inputs(variable_list, blockers = None):
l.reverse() l.reverse()
return l return l
dfs_variables = stack_search(deque(variable_list), expand, 'dfs') dfs_variables = stack_search(deque(variable_list), expand, 'dfs')
rval = [r for r in dfs_variables if r.owner is None] return dfs_variables
#print rval, _orig_inputs(o)
def inputs(variable_list, blockers = None):
"""Return the inputs required to compute the given Variables.
:type variable_list: list of `Variable` instances
:param variable_list:
output `Variable` instances from which to search backward through owners
:rtype: list of `Variable` instances
:returns:
input nodes with no owner, in the order found by a left-recursive depth-first search
started at the nodes in `variable_list`.
"""
vlist = ancestors(variable_list, blockers)
rval = [r for r in vlist if r.owner is None]
return rval return rval
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论