Categories &

Functions List

Class Definition: TFModel

tensorflow: TFModel

A trained TensorFlow model loaded from a SavedModel directory, for running inference.

 
 model = TFModel ("path/to/saved_model");
 y = model.predict (x);

The operations feeding and reading the model are found by inspecting the Graph, following the names tf.saved_model.save gives them: the inputs are the placeholders named 'serving_default_name' and the output is the operation named 'StatefulPartitionedCall'. Both are reported by the 'InputNames' and 'OutputNames' properties and can be given explicitly to the constructor when a model does not follow those names.

Signature names are not read from the SavedModel itself, which would mean decoding the MetaGraphDef, so a model exported some other way may need its operation names supplied. Inspect them with model.Session.Graph.operationNames ().

Source Code: TFModel

The TFModel class contains the following properties:

The TFModel class offers the following public methods:

TFModel: obj = TFModel (dirname)
TFModel: obj = TFModel (dirname, name, value, …)

The following optional Name/Value pairs are accepted.

NameValue
'Tags'The tags identifying the MetaGraphDef to load, a character vector or a cellstr vector. The default is {'serve'}.
'InputNames'The operations to feed, a character vector or a cellstr vector. The default is every placeholder named 'serving_default_name', sorted by name.
'OutputNames'The operations to read, a character vector or a cellstr vector. The default is every output of the operation named 'StatefulPartitionedCall'.

TFModel loads a model that was trained and exported elsewhere, from the directory holding its SavedModel. The package ships a small one for its own tests, which computes y = w .* x + b for w = [2, 3, 4] and b = [1, 1, 1].

 model = TFModel (__tf_test_model__ ());
 class (model)
ans = TFModel
 model.predict (single ([1, 1, 1]))
ans =

   3   4   5

The operations the model is fed through and read from are found by inspecting its graph, and reported so they can be checked. A model exported by some other means may not follow those names, in which case they are given to the constructor with InputNames and OutputNames.

 model = TFModel (__tf_test_model__ ());
 model.InputNames
ans =
{
  [1,1] = serving_default_x
}
 model.OutputNames
ans =
{
  [1,1] = StatefulPartitionedCall:0
}

The tags select which graph to load from the SavedModel, and default to serve, the one exported for inference.

 model = TFModel (__tf_test_model__ (), "Tags", "serve");
 model.Tags
ans =
{
  [1,1] = serve
}
TFModel: y = predict (obj, x)
TFModel: y = predict (obj, x1, …, xN)

As many inputs must be given as the model has 'InputNames', in that order. y is an Octave array when the model has a single output, and a cell array of them otherwise.

predict runs the model on an input and returns its output. This model expects rows of three values and scales each column by [2, 3, 4] before adding one.

 model = TFModel (__tf_test_model__ ());
 x = single ([1, 2, 3])
x =

   1   2   3
 y = model.predict (x)
y =

    3    7   13

Each row is an independent observation, so several are predicted at once by stacking them.

 model = TFModel (__tf_test_model__ ());
 x = single ([1, 2, 3; 4, 5, 6; 0, 0, 0])
x =

   1   2   3
   4   5   6
   0   0   0
 y = model.predict (x)
y =

    3    7   13
    9   16   25
    1    1    1

The input may also be given as a TF_Tensor, which is useful when the same values are predicted more than once, since the conversion from an Octave array happens only when the tensor is built.

 model = TFModel (__tf_test_model__ ());
 t = TF_Tensor (single ([1, 1, 1]));
 model.predict (t)
ans =

   3   4   5
 model.predict (t)
ans =

   3   4   5
TFModel: delete (obj)