Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 226
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e08eb06
Add fetchVariable method to Session to get value of resource variable
rnett f8f9a2f
Format
rnett 951a2d9
More Formatting
rnett 6d9317f
Rework, automatically wrap variables in read when fetched
rnett e5d8512
Forgot to format
rnett fb5c319
Remove obsolete method
rnett b40fb0c
Small fixes
rnett a6496ce
Python model loading + variable fetching test
rnett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
77 changes: 61 additions & 16 deletions
77 tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,9 +16,7 @@ | ||
| package org.tensorflow; | ||
| import static org.tensorflow.Graph.resolveOutputs; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_CloseSession; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteSession; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_NewSession; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_OperationGetAttrType; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_SessionRun; | ||
| import static org.tensorflow.internal.c_api.global.tensorflow.TF_SetConfig; | ||
| @@ -38,8 +36,12 @@ | ||
| import org.tensorflow.internal.c_api.TF_SessionOptions; | ||
| import org.tensorflow.internal.c_api.TF_Status; | ||
| import org.tensorflow.internal.c_api.TF_Tensor; | ||
| import org.tensorflow.internal.types.registry.TensorTypeRegistry; | ||
| import org.tensorflow.op.Op; | ||
| import org.tensorflow.op.Ops; | ||
| import org.tensorflow.op.core.ReadVariableOp; | ||
| import org.tensorflow.proto.framework.ConfigProto; | ||
| import org.tensorflow.proto.framework.DataType; | ||
| import org.tensorflow.proto.framework.RunMetadata; | ||
| import org.tensorflow.proto.framework.RunOptions; | ||
| import org.tensorflow.proto.util.SaverDef; | ||
| @@ -192,6 +194,11 @@ public Runner feed(String operation, int index, Tensor t) { | ||
| * @return this session runner | ||
| */ | ||
| public Runner feed(Operand<?> operand, Tensor t) { | ||
| if (operand.env() != graph) { | ||
| throw new IllegalStateException("Can't feed value for operand " + operand + ", it is from " + | ||
| (operand.env().isEager() ? "an eager session" : "a different graph") + "."); | ||
| } | ||
| inputs.add(operand.asOutput()); | ||
| inputTensors.add(t); | ||
| return this; | ||
| @@ -200,6 +207,8 @@ public Runner feed(Operand<?> operand, Tensor t) { | ||
| /** | ||
| * Make {@link #run()} return the output of {@code operation}. | ||
| * | ||
| * If the output is a resource variable, will fetch the value. | ||
| * | ||
| * @param operation Is either the string name of the operation, in which case this method is a shorthand for {@code | ||
| * fetch(operation, 0)}, or it is a string of the form | ||
| * <tt>operation_name:output_index</tt> , in which case this method acts like {@code | ||
| @@ -215,6 +224,8 @@ public Runner fetch(String operation) { | ||
| /** | ||
| * Make {@link #run()} return the {@code index}-th output of {@code operation}. | ||
| * | ||
| * If the output is a resource variable, will fetch the value. | ||
| * | ||
| * <p>Operations in a {@link Graph} can have multiple outputs, {@code index} identifies which | ||
| * one to return. | ||
| * | ||
| @@ -225,24 +236,61 @@ public Runner fetch(String operation) { | ||
| */ | ||
| public Runner fetch(String operation, int index) { | ||
| Operation op = graph.operationOrThrow(operation); | ||
| outputs.add(op.output(index)); | ||
| return this; | ||
| return fetch(op.output(index)); | ||
| } | ||
| /** | ||
| * Makes {@link #run()} return the Tensor referred to by {@code output}. | ||
| * | ||
| * If {@code output} is a resource variable, will fetch the value. | ||
| * | ||
| * @param output the node to fetch the tensor from | ||
| * @return this session runner | ||
| */ | ||
| public Runner fetch(Output<?> output) { | ||
| outputs.add(output); | ||
| if (output.env() != graph) { | ||
| throw new IllegalStateException("Can't fetch output " + output + ", it is from " + | ||
| (output.env().isEager() ? "an eager session" : "a different graph") + "."); | ||
| } | ||
| if (output.dataType() == DataType.DT_RESOURCE) { | ||
| int[] rawDt = new int[1]; | ||
| GraphOperation graphOp = (GraphOperation) output.op(); | ||
| try (PointerScope scope = new PointerScope()) { | ||
| TF_Status status = TF_Status.newStatus(); | ||
| TF_OperationGetAttrType(graphOp.getUnsafeNativeHandle(), "dtype", rawDt, status); | ||
| status.throwExceptionIfNotOK(); | ||
| } | ||
| DataType valueDt = DataType.forNumber(rawDt[0]); | ||
| Operand<?> read = null; | ||
| for (GraphOperation op : graphOp.consumers()) { | ||
| if (op.dtype(0) == valueDt && op.type().equals(ReadVariableOp.OP_NAME)) { | ||
| read = op.output(0); | ||
| break; | ||
| } | ||
| } | ||
| if (read == null) { | ||
| read = Ops.create(graph).withSubScope("session_reads").withName(output.op().name() + "_read") | ||
Craigacp marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .readVariableOp(output, TensorTypeRegistry.find(valueDt).type()); | ||
| } | ||
| outputs.add(read.asOutput()); | ||
| } else { | ||
| outputs.add(output); | ||
| } | ||
| return this; | ||
| } | ||
| /** | ||
| * Makes {@link #run()} return the Tensor referred to by the output of {@code operand}. | ||
| * | ||
| * If {@code operand} is a resource variable, will fetch the value. | ||
| * | ||
| * @param operand the node to fetch the tensor from, as an operand | ||
| * @return this session runner | ||
| */ | ||
| @@ -258,9 +306,7 @@ public Runner fetch(Operand<?> operand) { | ||
| * @throws IllegalArgumentException if no operation exists with the provided name | ||
| */ | ||
| public Runner addTarget(String operation) { | ||
| GraphOperation op = graph.operationOrThrow(operation); | ||
| targets.add(op); | ||
| return this; | ||
| return addTarget(graph.operationOrThrow(operation)); | ||
| } | ||
| /** | ||
| @@ -269,13 +315,12 @@ public Runner addTarget(String operation) { | ||
| * @param operation the operation to execute | ||
| * @return this session runner | ||
| * @throws IllegalArgumentException if the operation is not a {@link GraphOperation} | ||
| * @throws IllegalStateException if the operation is not from the session's graph. | ||
| */ | ||
| public Runner addTarget(Operation operation) { | ||
| if (!(operation instanceof GraphOperation)) { | ||
| throw new IllegalArgumentException( | ||
| "Operation of type " | ||
| + operation.getClass().getName() | ||
| + " is not supported in graph sessions"); | ||
| if (operation.env() != graph) { | ||
| throw new IllegalStateException("Can't target operation " + operation + ", it is from " + | ||
| (operation.env().isEager() ? "an eager session" : "a different graph") + "."); | ||
| } | ||
| targets.add((GraphOperation) operation); | ||
| return this; | ||
| @@ -594,12 +639,12 @@ private static void delete(TF_Session handle) { | ||
| * | ||
| * @param handle to the C API TF_Session object (Session.nativeHandle) | ||
| * @param runOptions A RunOptions protocol buffer, or null | ||
| * @param inputOpHandles (see inputOpIndices) | ||
| * @param inputOpIndices (see inputTensorHandles) | ||
| * @param inputTensorHandles together with inputOpHandles and inputOpIndices specifies the values that are being "fed" | ||
| * (do not need to be computed) during graph execution. inputTensorHandles[i] (which corresponds to a | ||
| * Tensor.nativeHandle) is considered to be the inputOpIndices[i]-th output of the Operation inputOpHandles[i]. Thus, | ||
| * it is required that inputOpHandles.length == inputOpIndices.length == inputTensorHandles.length. | ||
| * @param inputOpHandles (see inputOpIndices) | ||
| * @param inputOpIndices (see inputTensorHandles) | ||
| * @param outputOpHandles (see outputOpIndices) | ||
| * @param outputOpIndices together with outputOpHandles identifies the set of values that should be computed. The | ||
| * outputOpIndices[i]-th output of the Operation outputOpHandles[i], It is required that outputOpHandles.length == | ||
16 changes: 12 additions & 4 deletions
16 tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/SavedModelBundleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.