本文整理汇总了Python中theano.gof.graph.io_toposort函数的典型用法代码示例。如果您正苦于以下问题:Python io_toposort函数的具体用法?Python io_toposort怎么用?Python io_toposort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了io_toposort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_2
def test_2(self):
"""Test a graph where the inputs have owners"""
r1, r5 = MyVariable(1), MyVariable(5)
o = MyOp.make_node(r1, r1)
r2b = o.outputs[0]
o2 = MyOp.make_node(r2b, r2b)
all = io_toposort([r2b], o2.outputs)
assert all == [o2]
o2 = MyOp.make_node(r2b, r5)
all = io_toposort([r2b], o2.outputs)
assert all == [o2]
示例2: __import__
def __import__(self, node, check = True):
# We import the nodes in topological order. We only are interested
# in new nodes, so we use all variables we know of as if they were the input set.
# (the functions in the graph module only use the input set to
# know where to stop going down)
new_nodes = graph.io_toposort(self.variables, node.outputs)
if check:
for node in new_nodes:
if hasattr(node, 'env') and node.env is not self:
raise Exception("%s is already owned by another env" % node)
for r in node.inputs:
if hasattr(r, 'env') and r.env is not self:
raise Exception("%s is already owned by another env" % r)
for node in new_nodes:
assert node not in self.nodes
self.__setup_node__(node)
for output in node.outputs:
self.__setup_r__(output)
for i, input in enumerate(node.inputs):
if input not in self.variables:
self.__setup_r__(input)
self.__add_clients__(input, [(node, i)])
assert node.env is self
self.execute_callbacks('on_import', node)
示例3: toposort
def toposort(self):
"""WRITEME
Returns an ordering of the graph's Apply nodes such that:
- All the nodes of the inputs of a node are before that node.
- Satisfies the orderings provided by each feature that has
an 'orderings' method.
If a feature has an 'orderings' method, it will be called with
this FunctionGraph as sole argument. It should return a dictionary of
{node: predecessors} where predecessors is a list of nodes
that should be computed before the key node.
"""
if len(self.apply_nodes) < 2:
# optimization
# when there are 0 or 1 nodes, no sorting is necessary
# This special case happens a lot because the OpWiseCLinker
# produces 1-element graphs.
return list(self.apply_nodes)
fg = self
ords = self.orderings()
order = graph.io_toposort(fg.inputs, fg.outputs, ords)
return order
示例4: _get_variables
def _get_variables(self):
"""Collect variables, updates and auxiliary variables.
In addition collects all :class:`.Scan` ops and recurses in the
respective inner Theano graphs.
"""
updates = OrderedDict()
shared_outputs = [o for o in self.outputs if is_shared_variable(o)]
usual_outputs = [o for o in self.outputs if not is_shared_variable(o)]
variables = shared_outputs
if usual_outputs:
# Sort apply nodes topologically, get variables and remove
# duplicates
inputs = graph.inputs(self.outputs)
sorted_apply_nodes = graph.io_toposort(inputs, usual_outputs)
self.scans = list(unique([node.op for node in sorted_apply_nodes
if isinstance(node.op, Scan)],
key=lambda op: id(op)))
self._scan_graphs = [ComputationGraph(scan.outputs)
for scan in self.scans]
seen = set()
main_vars = (
[var for var in list(chain(
*[apply_node.inputs for apply_node in sorted_apply_nodes]))
if not (var in seen or seen.add(var))] +
[var for var in self.outputs if var not in seen])
# While preserving order add auxiliary variables, and collect
# updates
seen = set()
# Intermediate variables could be auxiliary
seen_avs = set(main_vars)
variables = []
for var in main_vars:
variables.append(var)
for annotation in getattr(var.tag, 'annotations', []):
if annotation not in seen:
seen.add(annotation)
new_avs = [
av for av in annotation.auxiliary_variables
if not (av in seen_avs or seen_avs.add(av))]
variables.extend(new_avs)
updates = dict_union(updates, annotation.updates)
# If shared_variables is assigned default_update (cloned), we cannot eval()
# it to get the real numpy array value, hence, try to trace back
# original shared variable
def shared_variable_filter(var):
if is_shared_variable(var) and hasattr(var, 'default_update'):
for annotation in var.tag.annotations:
if hasattr(annotation, var.name) and \
is_shared_variable(getattr(annotation, var.name)):
return getattr(annotation, var.name)
return var
self.variables = map(shared_variable_filter, variables)
self.updates = updates
示例5: test_5
def test_5(self):
"""Test when outputs have clients"""
r1, r2, r4 = MyVariable(1), MyVariable(2), MyVariable(4)
o0 = MyOp.make_node(r1, r2)
MyOp.make_node(o0.outputs[0], r4)
all = io_toposort([], o0.outputs)
assert all == [o0]
示例6: test_3
def test_3(self):
"""Test a graph which is not connected"""
r1, r2, r3, r4 = MyVariable(1), MyVariable(2), MyVariable(3), MyVariable(4)
o0 = MyOp.make_node(r1, r2)
o1 = MyOp.make_node(r3, r4)
all = io_toposort([r1, r2, r3, r4], o0.outputs + o1.outputs)
assert all == [o1, o0]
示例7: test_4
def test_4(self):
"""Test inputs and outputs mixed together in a chain graph"""
r1, r2 = MyVariable(1), MyVariable(2)
o0 = MyOp.make_node(r1, r2)
o1 = MyOp.make_node(o0.outputs[0], r1)
all = io_toposort([r1, o0.outputs[0]], [o0.outputs[0], o1.outputs[0]])
assert all == [o1]
示例8: on_detach
def on_detach(self, fgraph):
"""
Should remove any dynamically added functionality
that it installed into the function_graph
"""
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
self.on_prune(fgraph, node, 'Bookkeeper.detach')
示例9: test_dependence
def test_dependence():
dependence = make_dependence_cmp()
x = tensor.matrix('x')
y = tensor.dot(x * 2, x + 1)
nodes = io_toposort([x], [y])
for a, b in zip(nodes[:-1], nodes[1:]):
assert dependence(a, b) <= 0
示例10: on_attach
def on_attach(self, fgraph):
"""
Called by FunctionGraph.attach_feature, the method that attaches
the feature to the FunctionGraph. Since this is called after the
FunctionGraph is initially populated, this is where you should
run checks on the initial contents of the FunctionGraph.
"""
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
self.on_import(fgraph, node, "on_attach")
示例11: test_0
def test_0(self):
"""Test a simple graph"""
r1, r2, r5 = MyVariable(1), MyVariable(2), MyVariable(5)
o = MyOp.make_node(r1, r2)
o2 = MyOp.make_node(o.outputs[0], r5)
all = general_toposort(o2.outputs, prenode)
assert all == [r5, r2, r1, o, o.outputs[0], o2, o2.outputs[0]]
all = io_toposort([r5], o2.outputs)
assert all == [o, o2]
示例12: _get_variables
def _get_variables(self):
"""Collect variables, updates and auxiliary variables.
In addition collects all :class:`.Scan` ops and recurses in the
respective inner Theano graphs.
"""
updates = OrderedDict()
shared_outputs = [o for o in self.outputs if is_shared_variable(o)]
usual_outputs = [o for o in self.outputs if not is_shared_variable(o)]
variables = shared_outputs
if usual_outputs:
# Sort apply nodes topologically, get variables and remove
# duplicates
inputs = graph.inputs(self.outputs)
self.sorted_apply_nodes = graph.io_toposort(inputs, usual_outputs)
self.scans = list(unique([node.op for node in self.sorted_apply_nodes
if isinstance(node.op, Scan)]))
self.sorted_scan_nodes = [node for node in self.sorted_apply_nodes
if isinstance(node.op, Scan)]
self._scan_graphs = [ComputationGraph(scan.outputs)
for scan in self.scans]
seen = set()
main_vars = (
[var for var in list(chain(
*[apply_node.inputs for apply_node in self.sorted_apply_nodes]))
if not (var in seen or seen.add(var))] +
[var for var in self.outputs if var not in seen])
# While preserving order add auxiliary variables, and collect
# updates
seen = set()
# Intermediate variables could be auxiliary
seen_avs = set(main_vars)
variables = []
for var in main_vars:
variables.append(var)
for annotation in getattr(var.tag, 'annotations', []):
if annotation not in seen:
seen.add(annotation)
new_avs = [
av for av in annotation.auxiliary_variables
if not (av in seen_avs or seen_avs.add(av))]
variables.extend(new_avs)
updates = dict_union(updates, annotation.updates)
self.variables = variables
self.updates = updates
示例13: __import__
def __import__(self, apply_node, check=True, reason=None):
"""
Given an apply_node, recursively search from this node to know graph,
and then add all unknown variables and apply_nodes to this graph.
"""
node = apply_node
# We import the nodes in topological order. We only are interested
# in new nodes, so we use all variables we know of as if they were the input set.
# (the functions in the graph module only use the input set to
# know where to stop going down)
new_nodes = graph.io_toposort(self.variables, apply_node.outputs)
if check:
for node in new_nodes:
if hasattr(node, 'fgraph') and node.fgraph is not self:
raise Exception("%s is already owned by another fgraph" % node)
for r in node.inputs:
if hasattr(r, 'fgraph') and r.fgraph is not self:
raise Exception("%s is already owned by another fgraph" % r)
if (r.owner is None and
not isinstance(r, graph.Constant) and
r not in self.inputs):
# Standard error message
error_msg = ("Input %d of the graph (indices start "
"from 0), used to compute %s, was not "
"provided and not given a value. Use the "
"Theano flag exception_verbosity='high', "
"for more information on this error."
% (node.inputs.index(r), str(node)))
error_msg += get_variable_trace_string(r)
raise MissingInputError(error_msg, variable=r)
for node in new_nodes:
assert node not in self.apply_nodes
self.__setup_node__(node)
self.apply_nodes.add(node)
if not hasattr(node.tag, 'imported_by'):
node.tag.imported_by = []
node.tag.imported_by.append(str(reason))
for output in node.outputs:
self.__setup_r__(output)
self.variables.add(output)
for i, input in enumerate(node.inputs):
if input not in self.variables:
self.__setup_r__(input)
self.variables.add(input)
self.__add_client__(input, (node, i))
assert node.fgraph is self
self.execute_callbacks('on_import', node, reason)
示例14: clone_get_equiv
def clone_get_equiv(i, o, replacements=None):
"""Duplicate nodes from `i` to `o` inclusive.
Returns replacements dictionary, mapping each old node to its new one.
i - sequence of variables
o - sequence of variables
replacements - initial value for return value, modified in place.
"""
if replacements is None:
d = {}
else:
d = replacements
for old, new in replacements.items():
if new in replacements:
# I think we want to do something recursive here, but
# it feels like it might get tricky? This reminds me of the
# 'sorted_givens' branch on github/jaberg/Theano
raise NotImplementedError('think before implementing')
replacements[new] = new
for input in i:
if input not in d:
d[input] = input
for apply in graph.io_toposort(i, o):
for input in apply.inputs:
if input not in d:
d[input] = input
new_apply = apply.clone_with_new_inputs([d[i] for i in apply.inputs])
if apply not in d:
d[apply] = new_apply
for output, new_output in zip(apply.outputs, new_apply.outputs):
if output not in d:
d[output] = new_output
for output in o:
if output not in d:
d[output] = output.clone()
return d
示例15: on_attach
def on_attach(self, fgraph):
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
self.on_import(fgraph, node, "on_attach")