-
Notifications
You must be signed in to change notification settings - Fork 214
Initialization imprvements #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/Init.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
2d1f54b
to
304dc00
Compare
I remember that we we've added the Init ops, we discussed about having an specialized endpoint like this to initialize the variables but then ended up deciding that we should have something more generic that allows you to run any single op in a session. try (Graph g = new Graph()) {
Variable<TInt32> x = tf.variable(tf.constant(10));
Variable<TInt32> y = tf.variable(tf.constant(20));
try (Session s = new Session(g)) {
s.run(tf.init());
...
}
} Just double checking, is there any specific reason to go back to a specialized endpoint for variable initialization only? @Craigacp , any comment on this? |
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/Init.java
Outdated
Show resolved
Hide resolved
Two reasons: discoverability that initialization is a thing that needs to be ran (i.e. if you depend on a network that uses init that you didn't write, you may not even know it exists. I got burnt by forgetting to run it a few times), and doing |
Signed-off-by: Ryan Nett <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm fine with adding this back in, the previous discussion about it happened here - #36 (comment)
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
Outdated
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/Init.java
Outdated
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java
Outdated
Show resolved
Hide resolved
All TF 1 networks require the init op to be executed, it's been dropped with TF 2 in Python, but that's because the variables changed and things are implicitly eager whereas we still have Graph & Eager modes which are quite different. Until we paper over that more (and it might not be possible to do so without more improvements in the C API) then TF Java will still always need the init operator. I guess that means we should document that initialization is still required somewhere. |
Again, if we focus on the functional API instead, we can probably call |
Depends on how you store the session in the ConcreteFunction, but yeah probably. Idk about Python TF2, but I'm still using it for variable initialization in my PR for variables, and I don't see a way around it. Python uses |
Signed-off-by: Ryan Nett <[email protected]>
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
Outdated
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/Init.java
Outdated
Show resolved
Hide resolved
Thanks @rnett , I like this simplified version and I personally like to make it easier to initialize the variables. I've left a few minor comments, if you want to take a look before I merge. Still to be discuss, I would like to see at some point the Functional API taking care of initializing the variables once before calling the graph without the need for the user to do it. |
I'll change those. I agree about variable initialization, we could have the session track whether it's initialized, and run it on the first run call if not? We'd probably want to prevent the manual usage of the |
Signed-off-by: Ryan Nett <[email protected]>
* 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]>
* Initial checkin * Initial checkin and sync with master * Initial checkin and sync with master * JavaDoc cleanup * Javadoc fixes * Change LossInterface to LossMetric. Fix JavaDoc, modify one line code block to include braces. * Removed hashmap for variables, they are not needed as the variables only live within a single instance of a Metric. * reformat code * Add tests for assertBroadcastable * Change type to resultType * Added V data type for sampleWeights so that it is not forced to be the same type as the return or internal variables, * change 'type' to 'resultType' * clean up mean and fix assert assertBroadcastable * fix error message * Change sampleWeights to have its own generic type <S extends TNumber> * Add commment about invalid tests expecting IllegalArgumentExceptions * Add this exception instead of the more generic IllegalArgumentException when static shapes cannot boradcast. * change IllegalArgumentException to NotBroadcastableException. change hasValidNonscalarShape to canBroadcastNonscalarShapes change hasValidNonscalarShape to canBroadcastNonscalarShapes * reformat code * Fis=x Javadoc move the dynamic shapes and rank down to the dynamic section so they are created needlessly when static Fix if statement to check for unknown size and unknown dimensions * Fix Reduce to use boradcastWeights, renamed WeightBroadcastTest to AssertBroadcastableTest and added BroadcastWeightsTest * Added comment to count to indicate that it may be weighted. * Added SetsOps and fixed AssertBroadcastable to use SetsOps methods, * Fixed based on various PR comments. * Deleted, no longer needed after change to Variable handling in Metrics. * Nicer error messages for mode-forbidden ops (#169) * start fobbiden ops checks Signed-off-by: Ryan Nett <[email protected]> * fix style Signed-off-by: Ryan Nett <[email protected]> * move checks to builder method Signed-off-by: Ryan Nett <[email protected]> * 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]> * Clairify tensorOf lifetime requirements (#190) * Clairify tensorOf lifetime requirements Signed-off-by: Ryan Nett <[email protected]> * Do codegen Signed-off-by: Ryan Nett <[email protected]> * Remove extra generics from op generation (#193) * Successfully remove extra type params, but it broke javadoc generation Signed-off-by: Ryan Nett <[email protected]> * Generate covariant types Signed-off-by: Ryan Nett <[email protected]> * Do generation Signed-off-by: Ryan Nett <[email protected]> * Update help text. Signed-off-by: Ryan Nett <[email protected]> * Fixes Signed-off-by: Ryan Nett <[email protected]> * Add Java 11 support - Initial Phase (#185) * Add profile for JDK11 and Automatic-Module-Name to jars * add maven.compiler.release=11 * Update manual ops for new codegen (#196) Signed-off-by: Ryan Nett <[email protected]> * Fix Losses to use CHANNELS_FIRST/LAST for CategoricalCrossentropy * Fix SetOps to properly convert sparse tensor to dense tensor using tf.sparse.sparseToDense with the output of tf.sparse.denseToDenseSetOperation * Initial checkin * Initial checkin and sync with master * Initial checkin and sync with master * JavaDoc cleanup * Javadoc fixes * Change LossInterface to LossMetric. Fix JavaDoc, modify one line code block to include braces. * Removed hashmap for variables, they are not needed as the variables only live within a single instance of a Metric. * reformat code * Add tests for assertBroadcastable * Change type to resultType * Added V data type for sampleWeights so that it is not forced to be the same type as the return or internal variables, * change 'type' to 'resultType' * clean up mean and fix assert assertBroadcastable * fix error message * Change sampleWeights to have its own generic type <S extends TNumber> * Add commment about invalid tests expecting IllegalArgumentExceptions * Add this exception instead of the more generic IllegalArgumentException when static shapes cannot boradcast. * change IllegalArgumentException to NotBroadcastableException. change hasValidNonscalarShape to canBroadcastNonscalarShapes change hasValidNonscalarShape to canBroadcastNonscalarShapes * reformat code * Fis=x Javadoc move the dynamic shapes and rank down to the dynamic section so they are created needlessly when static Fix if statement to check for unknown size and unknown dimensions * Fix Reduce to use boradcastWeights, renamed WeightBroadcastTest to AssertBroadcastableTest and added BroadcastWeightsTest * Added comment to count to indicate that it may be weighted. * Added SetsOps and fixed AssertBroadcastable to use SetsOps methods, * Fixed based on various PR comments. * Deleted, no longer needed after change to Variable handling in Metrics. * Fix Losses to use CHANNELS_FIRST/LAST for CategoricalCrossentropy * Fix SetOps to properly convert sparse tensor to dense tensor using tf.sparse.sparseToDense with the output of tf.sparse.denseToDenseSetOperation Co-authored-by: Ryan Nett <[email protected]>
Makes three simple improvements to initialization:
Ops.initAdd
no-op in eager mode, since the op has already been ran.runInit()
method toSession
that runs the graph's initializers.doInitialization()
method toSession.Runner
to add the initializers as targets.There's no changes that conflict w/ the type refactor, so I'll just wait till it's merged and rebase onto master.