fg – Graph Container [doc TODO]

FunctionGraph

  • class theano.gof.FunctionGraph(inputs, outputs, features=None, clone=True, update_mapping=None)[source]
  • A FunctionGraph represents a subgraph bound by a set of input variables anda set of output variables, ie a subgraph that specifies a theano function.The inputs list should contain all the inputs on which the outputs depend.Variables of type Constant are not counted as inputs.

The FunctionGraph supports the replace operation which allows to replace avariable in the subgraph by another, e.g. replace (x + x).out by (2* x).out. This is the basis for optimization in theano.

This class is also reponsible for verifying that a graph is valid(ie, all the dtypes and broadcast patterns are compatible with theway the the Variables are used) and for annotating the Variables witha .clients field that specifies which Apply nodes use the variable.The .clients field combined with the .owner field and the Apply nodes’.inputs field allows the graph to be traversed in both directions.

It can also be extended with new features usingFunctionGraph.attach_feature().See toolbox.Feature for event types and documentation.Extra features allow the FunctionGraph to verify new properties ofa graph as it is optimized.# TODO: are there other things features can do to the fgraph?

Historically, the FunctionGraph was called an Env. Keep this in mindwhile reading out-of-date documentation, e-mail support threads, etc.

The constructor creates a FunctionGraph which operates on the subgraphbound by the inputs and outputs sets.

This class keeps a pointer to the inputs and outputs, and also modifiesthem.

TODO: document what variables are[not] set in the FunctionGraph when afeature is added via the constructor. How constructed is theFunctionGraph?

Parameters:

  • inputs – Inputs nodes of the graph, usually declared by the user.
  • outputs – Outputs nodes of the graph.
  • clone – If true, we will clone the graph. This is useful to remove the constantcache problem.

Notes

The intermediate nodes between ‘inputs’ and ‘outputs’ are not explicitelypassed.

TODO

Note

FunctionGraph(inputs, outputs) clones the inputs bydefault. To avoid this behavior, add the parameterclone=False. This is needed as we do not want cached constantsin fgraph.

  • attachfeature(_feature)[source]
  • Adds a gof.toolbox.Feature to this function_graph and triggers itson_attach callback.

  • changeinput(_node, i, new_r, reason=None)[source]

  • Changes node.inputs[i] to new_r.

new_r.type == old_r.type must be True, where old_r is thecurrent value of node.inputs[i] which we want to replace.

For each feature that has a ‘on_change_input’ method, calls:feature.on_change_input(function_graph, node, i, old_r, new_r, reason)

  • check_integrity()[source]
  • Call this for a diagnosis if things go awry.

  • clients(r)[source]

  • Set of all the (node, i) pairs such that node.inputs[i] is r.Told differently, a list of (node,i) such that each node haver as input at index i.

  • clone(check_integrity=True)[source]

  • Clone the graph and get a memo( a dict )that map old node to new node

  • cloneget_equiv(_check_integrity=True, attach_feature=True)[source]

  • Clone the graph and get a dict that maps old nodes to new ones

    • Parameters:
      • check_integrity: bool
      • Whether to check integrity. Default is True.
      • attach_feature: bool
      • Whether to attach feature of origin graph to cloned graph.Default is True.
    • Returns:
      • e: FunctionGraph
      • Cloned fgraph. Every node in cloned graph is cloned.
      • equiv: dict
      • A dict that map old node to new node.
  • collectcallbacks(_name, *args)[source]
  • Collects callbacks

Returns a dictionary d such that_d[feature] == getattr(feature, name)(*args)_For each feature which has a method called after name.

  • disown()[source]
  • Cleans up all of this FunctionGraph’s nodes and variables so they arenot associated with this FunctionGraph anymore.

The FunctionGraph should not be used anymore after disown is called.

  • executecallbacks(_name, *args, **kwargs)[source]
  • Execute callbacks

Calls getattr(feature, name)(*args) for each feature which hasa method called after name.

  • orderings()[source]
  • Return dict d s.t. d[node] is a list of nodes that must be evaluatedbefore node itself can be evaluated.

This is used primarily by the destroy_handler feature to ensure thatall clients of any destroyed inputs have already computed their outputs.

Notes

This only calls the orderings() fct on all features. It does nottake care of computing dependencies by itself.

  • removefeature(_feature)[source]
  • Removes the feature from the graph.

Calls feature.on_detach(function_graph) if an on_detach methodis defined.

  • replace(r, new_r, reason=None, verbose=None)[source]
  • This is the main interface to manipulate the subgraph in FunctionGraph.For every node that uses r as input, makes it use new_r instead.

  • replaceall(_pairs, reason=None)[source]

  • For every node that uses r as input, makes it use new_r instead

  • toposort()[source]

  • Toposort

Return an ordering of the graph’s Apply nodes such that

  1. - All the nodes of the inputs of a node are before that node.
  2. - Satisfies the orderings provided by each feature that hasan orderings method.

If a feature has an ‘orderings’ method, it will be called withthis FunctionGraph as sole argument. It should return a dictionary of{node: predecessors} where predecessors is a list of nodes thatshould be computed before the key node.

FunctionGraph Features

  • class theano.gof.toolbox.Feature[source]
  • Base class for FunctionGraph extensions.

A Feature is an object with several callbacks that are triggeredby various operations on FunctionGraphs. It can be used to enforcegraph properties at all stages of graph optimization.

See also

  • theano.gof.toolbox
  • for common extensions.

  • onattach(_function_graph)[source]

  • Called by FunctionGraph.attach_feature, the method that attachesthe feature to the FunctionGraph. Since this is called after theFunctionGraph is initially populated, this is where you shouldrun checks on the initial contents of the FunctionGraph.

The on_attach method may raise the AlreadyThere exception to cancelthe attach operation if it detects that another Feature instanceimplementing the same functionality is already atttached to theFunctionGraph.

The feature has great freedom in what it can do with thefunction_graph: it may, for example, add methods to it dynamically.

  • onchange_input(_function_graph, node, i, r, new_r, reason=None)[source]
  • Called whenever node.inputs[i] is changed from r to new_r.At the moment the callback is done, the change has alreadytaken place.

If you raise an exception in this function, the state of the graphmight be broken for all intents and purposes.

  • ondetach(_function_graph)[source]
  • Called by remove_feature(feature). Should remove any dynamically-addedfunctionality that it installed into the function_graph.

  • onimport(_function_graph, node, reason)[source]

  • Called whenever a node is imported into function_graph, which isjust before the node is actually connected to the graph.Note: on_import is not called when the graph is created. If youwant to detect the first nodes to be implemented to the graph,you should do this by implementing on_attach.

  • onprune(_function_graph, node, reason)[source]

  • Called whenever a node is pruned (removed) from the function_graph,after it is disconnected from the graph.

  • orderings(function_graph)[source]

  • Called by toposort. It should return a dictionary of{node: predecessors} where predecessors is a list ofnodes that should be computed before the key node.

If you raise an exception in this function, the state of the graphmight be broken for all intents and purposes.

FunctionGraph Feature List

  • ReplaceValidate
  • DestroyHandler