Categories &

Functions List

Class Definition: TF_Session

tensorflow: TF_Session

A TensorFlow Session, which executes the operations of a Graph.

A TF_Session owns the Session it wraps, closes and releases it when the object is destroyed, and keeps a reference to its TF_Graph so that the Graph outlives it whatever order the variables are cleared in.

Operations are named by strings of the form 'name' or 'name:index', where index is the zero based output index of the operation and defaults to 0.

Source Code: TF_Session

The TF_Session class contains the following properties:

The TF_Session class offers the following public methods:

TF_Session: obj = TF_Session (graph)

TF_Session: out = run (obj, inputs, values, outputs)

inputs names the operations the values are fed to and outputs names the operations whose values are wanted, both either a character vector or a cellstr vector, each name optionally carrying an output index as 'name:index'. values holds the values fed to inputs, either an Octave array, a TF_Tensor, or a cell array of either, and must have as many elements as inputs.

out is an Octave array when a single output is requested, and a cell array of them otherwise.

run executes a graph, naming the operations to feed and those to read. It is what TFModel.predict uses, and is the way to reach a model whose operations are not named as TFModel expects.

 sess = TF_Session.fromSavedModel (__tf_test_model__ ());
 x = single ([1, 2, 3]);
 y = sess.run ("serving_default_x", x, "StatefulPartitionedCall")
y =

    3    7   13

An operation may have several outputs, and the one wanted is named after a colon. Without it the first output, index 0, is read.

 sess = TF_Session.fromSavedModel (__tf_test_model__ ());
 x = single ([1, 1, 1]);
 sess.run ("serving_default_x:0", x, "StatefulPartitionedCall:0")
ans =

   3   4   5

Asking for more than one output returns a cell array holding them in the order they were named.

 sess = TF_Session.fromSavedModel (__tf_test_model__ ());
 x = single ([2, 2, 2]);
 y = sess.run ("serving_default_x", x, ...
               {"StatefulPartitionedCall", "StatefulPartitionedCall"})
y =
{
  [1,1] =

     5   7   9

  [1,2] =

     5   7   9

}
TF_Session: s = devices (obj)

devices reports what the session can execute on. A build of libtensorflow without GPU support finds the CPU alone; a GPU build also lists each visible GPU, which TensorFlow then places operations on by itself.

 sess = TF_Session.fromSavedModel (__tf_test_model__ ());
 d = sess.devices ();
 for i = 1:numel (d)
   printf ("%s  (%s, %d bytes)\n", d(i).Name, d(i).Type, d(i).MemoryBytes);
 endfor
/job:localhost/replica:0/task:0/device:CPU:0  (CPU, 268435456 bytes)
TF_Session: delete (obj)

This is called automatically when no variable refers to the object any more, and calling it a second time does nothing.

TF_Session: obj = TF_Session.fromSavedModel (dirname)
TF_Session: obj = TF_Session.fromSavedModel (dirname, tags)

tags identifies the MetaGraphDef to load, either a character vector or a cellstr vector, and defaults to {'serve'}.