-
Notifications
You must be signed in to change notification settings - Fork 214
Add fetchVariable method to Session to get value of resource variable #261
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
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
CI errors are from Javadoc, tests are passing. |
I don't think splitting In addition to the API issue, why does this require creating an eager session and constant op to read a value? What are the alternatives? |
Users can tell if their operand is a variable by checking the data type, maybe we should add a Fetching a variable is almost always an error anyways, since it's not usually an output, especially if you don't realize what you're fetching is a variable (i.e. #260). This is intended for the special use cases where you want to get a variable, in which case you'll know ahead of time it's a variable. To read a value from a resource tensor, you need to use the |
I think fetching variables is entirely reasonable as you can fetch everything else in the graph by name. Having a set of things that you can't fetch and the only way to find out is by trying it and catching a runtime exception is bad. I'd expect TF python to add |
Agreed, but I don't think it's possible, at least for now. The reason I did this in a separate method is because we can't fetch variables normally. There's no Resource TType, so tensor creation fails. Even if we did add one, it doesn't fit with the existing TType setup as the only method we would want to provide is
I think it does, but it's possible for people to use the raw op rather than |
Why not have it check for the read op and then silently redirect the fetch to that read op. That will give us back a non-resource tensor right? Otherwise unpicking the resource and exporting it into a regular tensor seems fine too. I'm wary of spinning up and throwing away eager sessions to access a variable. There's a bunch of stuff that happens in TFE_NewContext. |
It looks like it is possible to get the variable dtype from the attribute, so I can do everything using graph based ops, and automatically. However, eventually, once we have support for resource tensors, fetching a resource tensor will be a legitimate operation. So there still needs to be some way to differentiate between fetch w/ conversion and fetch without. Maybe make fetch auto-convert for now, and once we have resource tensor support add |
Signed-off-by: Ryan Nett <[email protected]>
Signed-off-by: Ryan Nett <[email protected]>
Ok, it automatically wraps variables in a read in the graph in |
@rnett, did you try simply using the BTW this is mostly out of curiosity, I'm also fine with the usage of |
Signed-off-by: Ryan Nett <[email protected]>
I didn't try that, but I assume the 1.x client was using the old variables, not resource variables. I'm not sure how to get a tensor from that buffer, either. |
Probably using that endpoint, should be easy enough to convert the But anyway, if Python does it with the |
👍 They are already added to the graph during the variable's creation in Python, and I plan on doing something similar for our version, so it should be fine. |
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.
A couple of small things about logging/messages, and I think a test loading a python graph is important to have.
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
Outdated
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/SessionTest.java
Show resolved
Hide resolved
Signed-off-by: Ryan Nett <[email protected]>
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.
LGTM
Small PR to add a method to Session to allow for the fetching of the values of resource variables. Fetches the resource tensor then uses an eager session to read the value.