Init

public final class Init

Constants

String DEFAULT_NAME

Public Methods

static void
add(Scope scope, Op initializer)
Register an op as an initializer of the graph.
static Init
create(Scope scope)
Factory method to create an operation executing all initializers of a graph.

Inherited Methods

org.tensorflow.op.RawOp
final boolean
equals(Object obj)
final int
Operation
op()
Return this unit of computation as a single Operation.
final String
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()
org.tensorflow.op.Op
abstract ExecutionEnvironment
env()
Return the execution environment this op was created in.
abstract Operation
op()
Return this unit of computation as a single Operation.

Constants

public static final String DEFAULT_NAME

Constant Value: "init"

Public Methods

public static void add (Scope scope, Op initializer)

Register an op as an initializer of the graph.

Registered initializers are then grouped as a single unit of computation by adding and executing an init operation from a graph session. This is a no-op if executed in an eager session.

See Also

public static Init create (Scope scope)

Factory method to create an operation executing all initializers of a graph.

All initializers added to a graph via tf.initAdd are grouped together as a single unit of computation in the graph. This operation must then be added to any graph using one or more variables and executed once before running the graph so the variable states are initialized properly.

When the graph is built by the same process that is running the session, the initializers can be invoked by executing this single endpoint. For example:

try (Graph g = new Graph()) {
   Variable<TInt32> x = tf.variable(tf.constant(10));  // initAdd is called implicitly
   Variable<TInt32> y = tf.variable(tf.constant(20));  // idem
   Add<TInt32> z = tf.math.add(x, y);

   try (Session s = new Session(g)) {
     s.run(tf.init());  // initialize all variables

     try (TInt32 t = (TInt32)s.runner().fetch(z).run().get(0)) {
       assertEquals(30, t.data().getInt());
     
   }
 }
 }

When the graph is built by a separate process, the initializers can be invoked by running the init op by its name, which defaults to DEFAULT_NAME. For example:

// Building the model
 try (Graph g = new Graph()) {
   Variable<TInt32> x = tf.variable(tf.constant(10));  // initAdd is called implicitly
   Variable<TInt32> y = tf.variable(tf.constant(20));  // idem
   Add<TInt32> z = tf.withName("z").math.add(x, y);

   tf.init();  // add variables initializers to the graph, as Init.DEFAULT_NAME
   // ...exporting graph as a saved model...
 

 ...

 // Running the model
 try (SavedModelBundle model = SavedModelBundle.load("/path/to/model", "train")) {
   model.session().run(Init.DEFAULT_NAME);

   try (TInt32 t = (TInt32)s.runner().fetch("z").run().get(0)) {
     assertEquals(30, t.data().getInt());
   }
 }
 }

Parameters
scope current scope
Returns
  • an op grouping all initializers added to the graph
Throws
IllegalArgumentException if the execution environment in scope is not a graph