public final class ConcreteFunction extends Object implements AutoCloseable, TensorFunction
A function can also invoke a tf.function defined in a SavedModelBundle
.
ConcreteFunction myFunction = savedModelBundle.function("myFunctionSignatureName");
Map<String, Tensor> outputTensorMap = myFunction.call(inputTensorMap);
Modifier and Type | Method and Description |
---|---|
Map<String,Tensor> |
call(Map<String,Tensor> arguments)
Invokes a function using the default eager session.
|
Map<String,Operand<?>> |
call(Ops tf,
Map<String,Operand<?>> arguments)
Calls the function in an execution environment, adding its graph as a function if it isn't
already present.
|
Operand<?> |
call(Ops tf,
Operand<?> argument)
Calls the function in an execution environment, adding its graph as a function if it isn't
already present.
|
Map<String,Operand<?>> |
call(Scope scope,
Map<String,Operand<?>> arguments)
Calls the function in an execution environment, adding its graph as a function if it isn't
already present.
|
Operand<?> |
call(Scope scope,
Operand<?> argument)
Calls the function in an execution environment, adding its graph as a function if it isn't
already present.
|
void |
close() |
static ConcreteFunction |
create(Function<Ops,Signature> functionBuilder)
Creates a function by building a new graph.
|
static ConcreteFunction |
create(Signature signature,
Graph graph)
Create a function from a signature and an existing graph.
|
static ConcreteFunction |
create(Signature signature,
Session session)
Create a function from a signature and a valid graph session.
|
String |
getDefinedName()
Get the name of the function definition.
|
FunctionDef |
getFunctionDef()
Get the
FunctionDef proto. |
boolean |
isStateful()
Get whether the function is stateful.
|
Signature |
signature()
Returns the signature of this function
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
call, validateDescription
public static ConcreteFunction create(Function<Ops,Signature> functionBuilder)
The functionBuilder
must initialize the function graph from the provided Ops
instance and return a valid signature that will be used to feed the input tensors and fetch the
output tensors on execution.
The function will be the owner of the new graph and its resulting session. Therefore, the function must be enclosed properly with a try-with-resources block to guarantee that all native resources will be freed once the function is discarded. For example:
public class MyModel {
public static Signature addTwo(Ops tf) {
Placeholder<TFloat32> input = tf.placeholder(TFloat32.class);
Add<TFloat32> output = tf.math.add(input, tf.constant(2.0f));
return Signature.builder("addTwo").input("x", input).output("y", output).build();
}
public static void main(String args[]) {
try (ConcreteFunction function = ConcreteFunction.create(MyModel::addTwo);
TFloat32 x = TFloat32.scalarOf(2.0f)) {
assertEquals(4.0f, ((TFloat32)function.call(x)).getFloat());
}
}
}
functionBuilder
- function builderpublic static ConcreteFunction create(Signature signature, Graph graph)
The function will keep the ownership of the session used to run the graph but not the graph itself, meaning that the lifetime of the latter can extend beyond the scope of the function. For example:
try (Graph g = new Graph()) {
Placeholder<TFloat32> input = tf.placeholder(TFloat32.class);
Add<TFloat32> output = tf.math.add(input, tf.constant(2.0f));
Signature signature = Signature.builder().input("x", input).output("y", output).build();
try (ConcreteFunction f = ConcreteFunction.create(signature, g);
TFloat32 x = TFloat32.scalarOf(2.0f)) {
assertEquals(4.0f, ((TFloat32)function.call(x)).getFloat());
}
// Graph g is still valid at this point
}
signature
- signature of the function to creategraph
- a valid and initialized graphpublic static ConcreteFunction create(Signature signature, Session session)
The function will not own the session nor its graph, meaning that their lifetime can extend beyond the scope of the function. Therefore the function does not need to be closed after its usage. For example:
try (Graph g = new Graph()) {
Placeholder<TFloat32> input = tf.placeholder(TFloat32.class);
Add<TFloat32> output = tf.math.add(input, tf.constant(2.0f));
Signature signature = Signature.builder().input("x", input).output("y", output).build();
try (Session s = new Session(g)) {
// Auto-closing the function just as an example but this is not required since it has
// no effect
try (ConcreteFunction f = ConcreteFunction.create(signature, s);
TFloat32 t = TFloat32.scalarOf(2.0f)) {
assertEquals(4.0f, ((TFloat32)function.call(x)).getFloat());
}
// Session s is still valid at this point
}
// Graph g is still valid at this point
}
signature
- signature of the function to createsession
- a valid session to an initialized graphpublic Signature signature()
signature
in interface TensorFunction
public String getDefinedName()
public FunctionDef getFunctionDef()
FunctionDef
proto.public boolean isStateful()
public void close()
close
in interface AutoCloseable
public Map<String,Operand<?>> call(Scope scope, Map<String,Operand<?>> arguments)
Signature
.scope
- the scope to call the function inarguments
- the arguments to the callpublic Operand<?> call(Scope scope, Operand<?> argument)
scope
- the scope to call the function inargument
- the argument to the callpublic Map<String,Tensor> call(Map<String,Tensor> arguments)
TensorFunction
Caller is responsible for closing all Tensors.
call
in interface TensorFunction
arguments
- list of tensors to pass in input to the function, mapped by their signature
namepublic Map<String,Operand<?>> call(Ops tf, Map<String,Operand<?>> arguments)
Signature
.tf
- the scope to call the function inarguments
- the arguments to the callpublic Operand<?> call(Ops tf, Operand<?> argument)
tf
- the scope to call the function inargument
- the argument to the callCopyright © 2015–2022. All rights reserved.