Graph

public final class Graph

A data flow graph representing a TensorFlow computation.

Instances of a Graph are thread-safe.

WARNING: Resources consumed by the Graph object must be explicitly freed by invoking the close() method then the Graph object is no longer needed.

Nested Classes

interface Graph.WhileSubgraphBuilder Used to instantiate an abstract class which overrides the buildSubgraph method to build a conditional or body subgraph for a while loop.

Public Constructors

Graph ()
Create an empty Graph.

Public Methods

Output[] <?>
addGradients ( Output <?> y, Output[] <?> x)
Adds operations to compute the partial derivatives of sum of y s w.r.t x s, i.e., dy/dx_1, dy/dx_2...

This is a simplified version of addGradients(String, Output[], Output[], Output[]) where y is a single output, dx is null and prefix is null.

Output[] <?>
addGradients (String prefix, Output[] <?> y, Output[] <?> x, Output[] <?> dx)
Adds operations to compute the partial derivatives of sum of y s w.r.t x s, i.e., d(y_1 + y_2 + ...)/dx_1, d(y_1 + y_2 + ...)/dx_2...

dx are used as initial gradients (which represent the symbolic partial derivatives of some loss function L w.r.t.

synchronized void
addInitializer ( Op initializer)
Adds an initializer to the graph initializer list.
void
checkInput ( Op input)
Checks that input is valid to use as an input in this execution environment.
void
close ()
Release resources associated with the Graph.
synchronized Set< GraphOperation >
completeSubgraph (Set< Operand <?>> inputs, Set< Operand <?>> outputs)
Finds the operations used to produce outputs , assuming inputs are provided.
ExecutionEnvironment.Types
environmentType ()
Get the type of this environment (from the `Environments` enumeration.
void
importGraphDef ( GraphDef graphDef)
Import a representation of a TensorFlow graph.
void
importGraphDef ( GraphDef graphDef, String prefix)
Import a representation of a TensorFlow graph.
List< Op >
initializers ()
Returns all initializers added to the graph via addInitializer(Op)
GraphOperationBuilder
opBuilder (String type, String name)
Returns a builder to add Operation s to the Graph.
GraphOperation
operation (String name)
Returns the operation (node in the Graph) with the provided name.
GraphOperation
operationOrThrow (String name)
Returns the operation (node in the Graph) with the provided name, or throws IllegalArgumentException if there isn't one.
Iterator< Operation >
operations ()
Iterator over all the Operation s in the graph.
Output <?>
output (String output)
Returns the output with the provided name, or null if there is no such output.
Output <?>
outputOrThrow (String output)
Returns the output with the provided name, or throws IllegalArgumentException if there isn't one.
synchronized Set< GraphOperation >
subgraphFrom (Set< Operand <?>> inputs)
Get all ops that use one of inputs directly or indirectly (not including inputs ), including control dependencies.
synchronized Set< GraphOperation >
subgraphFromOps (Set< GraphOperation > inputs)
Get all ops that use one of inputs directly or indirectly (not including inputs ), including control dependencies.
Set< GraphOperation >
subgraphTo (Set< Operand <?>> outputs)
Get all ops directly or indirectly required to calculate outputs (not including outputs ), including control dependencies.
Set< GraphOperation >
subgraphToOps (Set< GraphOperation > outputs)
Get all ops directly or indirectly required to calculate outputs (not including outputs ), including control dependencies.
GraphDef
toGraphDef ()
Generate a representation of the Graph.
Output[] <?>
whileLoop ( Output[] <?> inputs, Graph.WhileSubgraphBuilder cgBuilder, Graph.WhileSubgraphBuilder bgBuilder, String name)
Builds a while loop.

Inherited Methods

Public Constructors

public Graph ()

Create an empty Graph.

Public Methods

public Output[] <?> addGradients ( Output <?> y, Output[] <?> x)

Adds operations to compute the partial derivatives of sum of y s w.r.t x s, i.e., dy/dx_1, dy/dx_2...

This is a simplified version of addGradients(String, Output[], Output[], Output[]) where y is a single output, dx is null and prefix is null.

Parameters
y output of the function to derive
x inputs of the function for which partial derivatives are computed
Returns
  • the partial derivatives dy with the size of x

public Output[] <?> addGradients (String prefix, Output[] <?> y, Output[] <?> x, Output[] <?> dx)

Adds operations to compute the partial derivatives of sum of y s w.r.t x s, i.e., d(y_1 + y_2 + ...)/dx_1, d(y_1 + y_2 + ...)/dx_2...

dx are used as initial gradients (which represent the symbolic partial derivatives of some loss function L w.r.t. y ). dx must be null or have size of y .

If dx is null, the implementation will use dx of OnesLike for all shapes in y .

prefix is used as the name prefix applied to all nodes added to the graph to compute gradients. It must be unique within the provided graph or the operation will fail.

If prefix is null, then one will be chosen automatically.

Parameters
prefix unique string prefix applied before the names of nodes added to the graph to compute gradients. If null, a default one will be chosen.
y output of the function to derive
x inputs of the function for which partial derivatives are computed
dx if not null, the partial derivatives of some loss function L w.r.t. y
Returns
  • the partial derivatives dy with the size of x

public synchronized void addInitializer ( Op initializer)

Adds an initializer to the graph initializer list.

Parameters
initializer An initializer to add to the list.

public void checkInput ( Op input)

Checks that input is valid to use as an input in this execution environment. Throws IllegalArgumentException if not.

Parameters
input The op to check

public void close ()

Release resources associated with the Graph.

Blocks until there are no active Session instances referring to this Graph. A Graph is not usable after close returns.

public synchronized Set< GraphOperation > completeSubgraph (Set< Operand <?>> inputs, Set< Operand <?>> outputs)

Finds the operations used to produce outputs , assuming inputs are provided. Includes control dependencies.

Note that this function can easily return ops upstream of inputs as part of the body. Depending on your use, the returned body should probably be filtered for Placeholder s, at least.

Parameters
inputs the inputs of the subgraph. Must be from single output ops. May not be null.
outputs the outputs of the subgraph. May not be null.
Returns
  • the set of operations needed to calculate outputs from inputs, including outputs and inputs

public ExecutionEnvironment.Types environmentType ()

Get the type of this environment (from the `Environments` enumeration.

Returns
  • An `Environments` value indicating the type of execution environment.

public void importGraphDef ( GraphDef graphDef)

Import a representation of a TensorFlow graph.

The representation of the graph, referred to as a GraphDef , can be generated by toGraphDef() and equivalents in other language APIs.

Parameters
graphDef GraphDef proto to import
Throws
IllegalArgumentException if graphDef is not a recognized serialization of a graph.

public void importGraphDef ( GraphDef graphDef, String prefix)

Import a representation of a TensorFlow graph.

Parameters
graphDef the representation of a TensorFlow graph.
prefix a prefix that will be prepended to names in graphDef
Throws
IllegalArgumentException if graphDef is not a recognized serialization of a graph.

public List< Op > initializers ()

Returns all initializers added to the graph via addInitializer(Op)

public GraphOperationBuilder opBuilder (String type, String name)

Returns a builder to add Operation s to the Graph.

Parameters
type of the Operation (i.e., identifies the computation to be performed)
name to refer to the created Operation in the graph.
Returns

public GraphOperation operation (String name)

Returns the operation (node in the Graph) with the provided name.

Or null if no such operation exists in the Graph.

Parameters
name name of the operation to look for
Returns
  • operation in the graph with this name or null if not found

public GraphOperation operationOrThrow (String name)

Returns the operation (node in the Graph) with the provided name, or throws IllegalArgumentException if there isn't one.

Parameters
name name of the operation to look for
Returns
  • operation in the graph with this name
Throws
IllegalArgumentException if no such operation exists in the Graph

public Iterator< Operation > operations ()

Iterator over all the Operation s in the graph.

The order of iteration is unspecified. Consumers of the iterator will receive no notification should the underlying graph change during iteration.

public Output <?> output (String output)

Returns the output with the provided name, or null if there is no such output.

Names should be of the format /scope/op , with an optional index: /scope/op:1 . 0 is used if the index is not specified.

Parameters
output the output to get
Returns
  • the output with this name, or null if there isn't one