FunctionGraph

TODO: clean up/update the doc/check if complete WRITEME

class theano.gof.fg.FunctionGraph(inputs, outputs, features=None, clone=True, update_mapping=None)

WRITEME A FunctionGraph represents a subgraph bound by a set of input variables and a 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 a variable 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 the way the the Variables are used) and for annotating the Variables with a .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 using FunctionGraph.attach_feature(<toolbox.Feature instance>). See toolbox.Feature for event types and documentation. Extra features allow the FunctionGraph to verify new properties of a 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 mind while reading out-of-date documentation, e-mail support threads, etc.

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

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

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

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 constant cache problem.

Notes

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

Feature

class theano.gof.toolbox.Feature

Base class for FunctionGraph extensions.

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

See also

theano.gof.toolbox
for common extensions.
on_attach(function_graph)

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.

The on_attach method may raise the AlreadyThere exception to cancel the attach operation if it detects that another Feature instance implementing the same functionality is already atttached to the FunctionGraph.

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

on_change_input(function_graph, node, i, r, new_r, reason=None)

Called whenever node.inputs[i] is changed from r to new_r. At the moment the callback is done, the change has already taken place.

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

on_detach(function_graph)

Called by remove_feature(feature). Should remove any dynamically-added functionality that it installed into the function_graph.

on_import(function_graph, node, reason)

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

on_prune(function_graph, node, reason)

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

orderings(function_graph)

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

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