ConcreteFunction

public class ConcreteFunction

A graph that can be invoked as a single function, with an input and output signature.

A function can also invoke a tf.function defined in a SavedModelBundle.

ConcreteFunction myFunction = savedModelBundle.function("myFunctionSignatureName");
 Map<String, Tensor> outputTensorMap = myFunction.call(inputTensorMap);
 

Public Methods

Tensor
call(Tensor tensor)
Invokes a function with a single input and output.
Map<String, Tensor>
call(Map<String, Tensor> arguments)
Invokes a function.
void
close()
static ConcreteFunction
create(Signature signature, Session session)
Create a function from a signature and a valid graph session.
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.
Graph
graph()
Returns the graph of this function
void
save(String exportDir)
Export this function as a saved model.
Session
session()
Returns the session used to execute the graph when calling this function

In general, a user does not need to handle directly the session of a function and rely on call(Map) to execute the graph instead.

Signature
signature()
Returns the signature of this function
String

Inherited Methods

Public Methods

public Tensor call (Tensor tensor)

Invokes a function with a single input and output.

Caller is responsible for closing all Tensors.

Parameters
tensor input tensor
Returns
  • output tensor
Throws
IllegalArgumentException if there are multiple input or output parameters defined in the function

public Map<String, Tensor> call (Map<String, Tensor> arguments)

Invokes a function.

Caller is responsible for closing all Tensors.

Parameters
arguments list of tensors to pass in input to the function, mapped by their signature name
Returns
  • output tensors resulting from the execution of the function, mapped by their signature name
Throws
IllegalArgumentException

public void close ()

public static ConcreteFunction create (Signature signature, Session session)

Create a function from a signature and a valid graph 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
 }
 }

Parameters
signature signature of the function to create
session a valid session to an initialized graph
Returns
  • a new function

public static ConcreteFunction create (Function<Ops, Signature> functionBuilder)

Creates a function by building a new graph.

The functionBuilder must initialize the function graph from the provided ERROR(/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());
     }
   }
 }
 }

Parameters
functionBuilder function builder
Returns
  • the new function

public static ConcreteFunction create (Signature signature, Graph graph)

Create a function from a signature and an existing 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
 }
 }

Parameters
signature signature of the function to create
graph a valid and initialized graph
Returns
  • a new function

public Graph graph ()

Returns the graph of this function

public void save (String exportDir)

Export this function as a saved model.

This method is convenient shortcut equivalent to SavedModel.exporter(exportDir).withFunction(this).export()

Parameters
exportDir directory where to export the saved model
Throws
IOException if saved model or variable state cannot be written on disk

public Session session ()

Returns the session used to execute the graph when calling this function

In general, a user does not need to handle directly the session of a function and rely on call(Map) to execute the graph instead. But in some cases, direct access to the session might be necessary, as it allows more running options.

Returns
  • the function session

public Signature signature ()

Returns the signature of this function

public String toString ()