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

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