Skip to content

Commit 0fc54c8

Browse files
authored
Initialization imprvements (#178)
* No-op on initAdd in eager mode Signed-off-by: Ryan Nett <[email protected]> * runInit() method in session Signed-off-by: Ryan Nett <[email protected]> * add doInitialization() to Runner Signed-off-by: Ryan Nett <[email protected]> * fix javadoc Signed-off-by: Ryan Nett <[email protected]> * assume only graph or eager environments Signed-off-by: Ryan Nett <[email protected]> * Remove doInit(), update javadocs Signed-off-by: Ryan Nett <[email protected]> * small fixes Signed-off-by: Ryan Nett <[email protected]>
1 parent e4a11b3 commit 0fc54c8

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ public final class Ops {
347347

348348
public final SignalOps signal;
349349

350-
public final QuantizationOps quantization;
351-
352350
public final TrainOps train;
353351

352+
public final QuantizationOps quantization;
353+
354354
private final Scope scope;
355355

356356
private Ops(Scope scope) {
@@ -372,8 +372,8 @@ private Ops(Scope scope) {
372372
math = new MathOps(this);
373373
audio = new AudioOps(this);
374374
signal = new SignalOps(this);
375-
quantization = new QuantizationOps(this);
376375
train = new TrainOps(this);
376+
quantization = new QuantizationOps(this);
377377
}
378378

379379
/**
@@ -2755,11 +2755,10 @@ public Init init() {
27552755
*
27562756
* <p>Registered initializers are then grouped as a single unit of computation by adding
27572757
* and executing an {@link org.tensorflow.op.core.Init#create(Scope) init} operation from a graph
2758-
* session.
2758+
* session. This is a no-op if executed in an eager session.
27592759
*
27602760
* @param scope
27612761
* @param initializer
2762-
* @throws IllegalArgumentException if the execution environment in scope is not a graph
27632762
* @see org.tensorflow.op.core.Init#create(Scope) init
27642763
*/
27652764
public void initAdd(Op initializer) {

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,19 @@ public void run(Op op) {
490490
runner().addTarget(op.op()).run();
491491
}
492492

493+
494+
/**
495+
* Execute the graph's initializers.
496+
*
497+
* <p>This method is equivalent to {@code session.run(Ops.create(session.graph).init())}.
498+
*
499+
*/
500+
public void runInit(){
501+
Runner runner = runner();
502+
graph.initializers().forEach(runner::addTarget);
503+
runner.run();
504+
}
505+
493506
/**
494507
* Saves the actual state of the variables of this session's graph.
495508
*

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/Init.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,19 @@ public static Init create(Scope scope) {
8989
*
9090
* <p>Registered initializers are then grouped as a single unit of computation by adding
9191
* and executing an {@link org.tensorflow.op.core.Init#create(Scope) init} operation from a graph
92-
* session.
92+
* session. This is a no-op if executed in an eager session.
9393
*
9494
* @param scope
9595
* @param initializer
96-
* @throws IllegalArgumentException if the execution environment in scope is not a graph
9796
* @see org.tensorflow.op.core.Init#create(Scope) init
9897
*/
9998
@Endpoint(name = "initAdd")
10099
public static void add(Scope scope, Op initializer) {
101100
ExecutionEnvironment exEnv = scope.env();
102-
if (!(exEnv instanceof Graph)) {
103-
throw new IllegalArgumentException("initAdd is only supported on Graph sessions.");
101+
102+
if (exEnv.isGraph()) {
103+
((Graph) exEnv).addInitializer(initializer);
104104
}
105-
Graph graph = (Graph) exEnv;
106-
graph.addInitializer(initializer);
107105
}
108106

109107
private Init(Operation operation) {

0 commit comments

Comments
 (0)