# 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 `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#close()` method then the Graph object is no longer needed.

### Nested Classes

|---|---|---|---|
| interface | [Graph.WhileSubgraphBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#Graph())() Create an empty Graph. |

### Public Methods

|---|---|
| [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> | [addGradients](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#addGradients(java.lang.String, org.tensorflow.Output<?>[], org.tensorflow.Output<?>[], org.tensorflow.Output<?>[]))(String prefix, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> y, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> x, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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. |
| [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> | [addGradients](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#addGradients(org.tensorflow.Output<?>, org.tensorflow.Output<?>[]))([Output](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> y, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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(Output[], Output[], Output[])` where `y` is a single output, `dx` is null and `prefix` is null. |
| void | [close](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#close())() Release resources associated with the Graph. |
| void | [importGraphDef](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[], java.lang.String))(byte\[\] graphDef, String prefix) Import a serialized representation of a TensorFlow graph. |
| void | [importGraphDef](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[]))(byte\[\] graphDef) Import a serialized representation of a TensorFlow graph. |
| [GraphOperationBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/GraphOperationBuilder) | [opBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#opBuilder(java.lang.String, java.lang.String))(String type, String name) Returns a builder to add `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation`s to the Graph. |
| [GraphOperation](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/GraphOperation) | [operation](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#operation(java.lang.String))(String name) Returns the operation (node in the Graph) with the provided name. |
| Iterator\<[Operation](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation)\> | [operations](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#operations())() Iterator over all the `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation`s in the graph. |
| byte\[\] | [toGraphDef](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#toGraphDef())() Generate a serialized representation of the Graph. |
| [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> | [whileLoop](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#whileLoop(org.tensorflow.Output<?>[], org.tensorflow.Graph.WhileSubgraphBuilder, org.tensorflow.Graph.WhileSubgraphBuilder, java.lang.String))([Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> inputs, [Graph.WhileSubgraphBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph.WhileSubgraphBuilder) cgBuilder, [Graph.WhileSubgraphBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph.WhileSubgraphBuilder) bgBuilder, String name) Builds a while loop. |

### Inherited Methods

From class java.lang.Object

|---|---|
| boolean | equals(Object arg0) |
| final Class\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| String | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

From interface [org.tensorflow.ExecutionEnvironment](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/ExecutionEnvironment)

|---|---|
| abstract [OperationBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/OperationBuilder) | [opBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/ExecutionEnvironment#opBuilder(java.lang.String, java.lang.String))(String type, String name) Returns a builder to create a new `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation`. |

From interface java.lang.AutoCloseable

|---|---|
| abstract void | close() |

## Public Constructors

#### public
**Graph**
()

Create an empty Graph.

## Public Methods

#### public [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\>
**addGradients**
(String prefix, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> y, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> x, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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 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 [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\>
**addGradients**
([Output](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> y, [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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(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 void
**close**
()

Release resources associated with the Graph.

Blocks until there are no active `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Session` instances referring to this Graph. A Graph
is not usable after close returns.

#### public void
**importGraphDef**
(byte\[\] graphDef, String prefix)

Import a serialized representation of a TensorFlow graph.

##### Parameters

| graphDef | the serialized 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. |
|---|---|

##### See Also

- `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[])`

#### public void
**importGraphDef**
(byte\[\] graphDef)

Import a serialized representation of a TensorFlow graph.

The serialized representation of the graph, often referred to as a *GraphDef*, can be
generated by `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#toGraphDef()` and equivalents in other language APIs.

##### Parameters

| graphDef |   |
|---|---|

##### Throws

| IllegalArgumentException | if graphDef is not a recognized serialization of a graph. |
|---|---|

##### See Also

- `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[], java.lang.String)`

#### public [GraphOperationBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/GraphOperationBuilder)
**opBuilder**
(String type, String name)

Returns a builder to add `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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

- an `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/OperationBuilder`, which will add the Operation to the graph when `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/OperationBuilder#build()` is invoked. If `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/OperationBuilder#build()` is not invoked, then some resources may leak.

#### public [GraphOperation](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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 |   |
|---|---|

#### public Iterator\<[Operation](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation)\>
**operations**
()

Iterator over all the `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/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 byte\[\]
**toGraphDef**
()

Generate a serialized representation of the Graph.

##### See Also

- `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[])`
- `https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph#importGraphDef(byte[], java.lang.String)`

#### public [Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\>
**whileLoop**
([Output\[\]](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output)\<?\> inputs, [Graph.WhileSubgraphBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph.WhileSubgraphBuilder) cgBuilder, [Graph.WhileSubgraphBuilder](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Graph.WhileSubgraphBuilder) bgBuilder, String name)

Builds a while loop.

##### Parameters

| inputs | the loop inputs |
| cgBuilder | WhileSubgraphBuilder to build the conditional subgraph |
| bgBuilder | WhileSubgraphBuilder to build the body subgraph |
| name | name for the loop |
|---|---|

##### Returns

- list of loop outputs, of the same length as `inputs`