TensorFlowSharp

Now that we've talked about and shown you Tensors, let's look at how we would typically use the TensorFlowSharp API itself.

Your application will typically create a graph (TFGraph), set up the operations there, then create a session from it (TFSession). This session will then use the session runner to set up inputs and outputs and execute the pipeline. Let's look at a quick example of how that might flow:

using(var graph = new TFGraph ())
{
graph.Import (File.ReadAllBytes ("MySavedModel"));
var session = new TFSession (graph);
var runner = session.GetRunner ();
runner.AddInput (graph ["input"] [0], tensor);
runner.Fetch (graph ["output"] [0]);
var output = runner.Run ();

Fetch the results from the output:

TFTensor result = output [0];
}

In scenarios where you do not need to set up the graph independently, the session will create one automatically for you. The following example shows how to use TensorFlow to compute the sum of two numbers. We will have the session automatically create the graph for us:

using (var session = new TFSession())
{
var graph = session.Graph;
var a = graph.Const(2);
var b = graph.Const(3);
Console.WriteLine("a=2 b=3");

Add two constants:

var addingResults = session.GetRunner().Run(graph.Add(a, b));
var addingResultValue = addingResults.GetValue();
Console.WriteLine("a+b={0}", addingResultValue);

Multiply two constants:

var multiplyResults = session.GetRunner().Run(graph.Mul(a, b));
var multiplyResultValue = multiplyResults.GetValue();
Console.WriteLine("a*b={0}", multiplyResultValue);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset