TensorFlowSharp 1.5.0-pre1

.NET Bindings for TensorFlow

This is a prerelease version of TensorFlowSharp.
There is a newer version of this package available.
See the version list below for details.
Install-Package TensorFlowSharp -Version 1.5.0-pre1
dotnet add package TensorFlowSharp --version 1.5.0-pre1
<PackageReference Include="TensorFlowSharp" Version="1.5.0-pre1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TensorFlowSharp --version 1.5.0-pre1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Build Status

TensorFlowSharp are .NET bindings to the TensorFlow library published here:

https://github.com/tensorflow/tensorflow

This surfaces the C API as a strongly-typed .NET API for use from C# and F#.

The API binding is pretty much done, and at this point, I am polishing the
API to make it more pleasant to use from C# and F# and resolving some of the
kinks and TODO-items that I left while I was doing the work.

The current API
documentation
is here.

Using TensorFlowSharp

Installation

The easiest way to get started is to use the NuGet package for
TensorFlowSharp which contains both the .NET API as well as the
native libraries for 64-bit Linux, Mac and Windows using the CPU backend.

You can install using NuGet like this:

nuget install TensorFlowSharp

Or select it from the NuGet packages UI on Visual Studio.

On Visual Studio, make sure that you are targeting .NET 4.6.1 or
later, as this package uses some features of newer .NETs. Otherwise,
the package will not be added. Once you do this, you can just use the
TensorFlowSharp nuget

Alternatively, you can download it directly.

Using TensorFlowSharp

Your best source of information right now are the SampleTest that
exercises various APIs of TensorFlowSharp, or the stand-alone samples
located in "Examples".

This API binding is closer design-wise to the Java and Go bindings
which use explicit TensorFlow graphs and sessions. Your application
will typically create a graph (TFGraph) and setup the operations
there, then create a session from it (TFSession), then use the session
runner to setup inputs and outputs and execute the pipeline.

Something like this:

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 output:
    TFTensor result = output [0];
}

In scenarios where you do not need to setup the graph independently,
the session will create one for you. The following example shows how
to abuse TensorFlow to compute the addition of two numbers:

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);
}

Here is an F# scripting version of the same example, you can use this in F# Interactive:

#r @"packages\TensorFlowSharp.1.4.0\lib\net461\TensorFlowSharp.dll"

open System
open System.IO
open TensorFlow

// set the path to find the native DLL
Environment.SetEnvironmentVariable("Path", 
    Environment.GetEnvironmentVariable("Path") + ";" + __SOURCE_DIRECTORY__ + @"/packages/TensorFlowSharp.1.2.2/native")

module AddTwoNumbers = 
    let session = new TFSession()
    let graph = session.Graph

    let a = graph.Const(new TFTensor(2))
    let b = graph.Const(new TFTensor(3))
    Console.WriteLine("a=2 b=3")

    // Add two constants
    let addingResults = session.GetRunner().Run(graph.Add(a, b))
    let addingResultValue = addingResults.GetValue()
    Console.WriteLine("a+b={0}", addingResultValue)

    // Multiply two constants
    let multiplyResults = session.GetRunner().Run(graph.Mul(a, b))
    let multiplyResultValue = multiplyResults.GetValue()
    Console.WriteLine("a*b={0}", multiplyResultValue)

Working on TensorFlowSharp

If you want to work on extending TensorFlowSharp or contribute to its development
read the CONTRIBUTING.md file.

Possible Contributions

Build More Tests

Would love to have more tests to ensure the proper operation of the framework.

Samples

The binding is pretty much complete, and at this point, I want to improve the
API to be easier and more pleasant to use from both C# and F#. Creating
samples that use Tensorflow is a good way of finding easy wins on the usability
of the API, there are some here:

https://github.com/tensorflow/models

Packaging

Mobile: we need to package the library for consumption on Android and iOS.

Documentation Styling

The API documentation has not been styled, I am using the barebones template
for documentation, and it can use some work.

Issues

I have logged some usability problems and bugs in Issues, feel free to take
on one of those tasks.

Notes on OpDefs

Look at:

./tensorflow/core/ops/ops.pbtxt AvgPool3D and:
./tensorflow/core/ops/nn_ops.cc for the C++ implementation with type definitions

Docs on types:
https://www.tensorflow.org/extend/adding_an_op

Documentation

Much of the online documentation comes from TensorFlow and is licensed under
the terms of Apache 2 License, in particular all the generated documentation
for the various operations that is generated by using the tensorflow reflection
APIs.

Last API update: a4b352bfddd518b540c30e456f3bc0027ba9351f

Build Status

TensorFlowSharp are .NET bindings to the TensorFlow library published here:

https://github.com/tensorflow/tensorflow

This surfaces the C API as a strongly-typed .NET API for use from C# and F#.

The API binding is pretty much done, and at this point, I am polishing the
API to make it more pleasant to use from C# and F# and resolving some of the
kinks and TODO-items that I left while I was doing the work.

The current API
documentation
is here.

Using TensorFlowSharp

Installation

The easiest way to get started is to use the NuGet package for
TensorFlowSharp which contains both the .NET API as well as the
native libraries for 64-bit Linux, Mac and Windows using the CPU backend.

You can install using NuGet like this:

nuget install TensorFlowSharp

Or select it from the NuGet packages UI on Visual Studio.

On Visual Studio, make sure that you are targeting .NET 4.6.1 or
later, as this package uses some features of newer .NETs. Otherwise,
the package will not be added. Once you do this, you can just use the
TensorFlowSharp nuget

Alternatively, you can download it directly.

Using TensorFlowSharp

Your best source of information right now are the SampleTest that
exercises various APIs of TensorFlowSharp, or the stand-alone samples
located in "Examples".

This API binding is closer design-wise to the Java and Go bindings
which use explicit TensorFlow graphs and sessions. Your application
will typically create a graph (TFGraph) and setup the operations
there, then create a session from it (TFSession), then use the session
runner to setup inputs and outputs and execute the pipeline.

Something like this:

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 output:
    TFTensor result = output [0];
}

In scenarios where you do not need to setup the graph independently,
the session will create one for you. The following example shows how
to abuse TensorFlow to compute the addition of two numbers:

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);
}

Here is an F# scripting version of the same example, you can use this in F# Interactive:

#r @"packages\TensorFlowSharp.1.4.0\lib\net461\TensorFlowSharp.dll"

open System
open System.IO
open TensorFlow

// set the path to find the native DLL
Environment.SetEnvironmentVariable("Path", 
    Environment.GetEnvironmentVariable("Path") + ";" + __SOURCE_DIRECTORY__ + @"/packages/TensorFlowSharp.1.2.2/native")

module AddTwoNumbers = 
    let session = new TFSession()
    let graph = session.Graph

    let a = graph.Const(new TFTensor(2))
    let b = graph.Const(new TFTensor(3))
    Console.WriteLine("a=2 b=3")

    // Add two constants
    let addingResults = session.GetRunner().Run(graph.Add(a, b))
    let addingResultValue = addingResults.GetValue()
    Console.WriteLine("a+b={0}", addingResultValue)

    // Multiply two constants
    let multiplyResults = session.GetRunner().Run(graph.Mul(a, b))
    let multiplyResultValue = multiplyResults.GetValue()
    Console.WriteLine("a*b={0}", multiplyResultValue)

Working on TensorFlowSharp

If you want to work on extending TensorFlowSharp or contribute to its development
read the CONTRIBUTING.md file.

Possible Contributions

Build More Tests

Would love to have more tests to ensure the proper operation of the framework.

Samples

The binding is pretty much complete, and at this point, I want to improve the
API to be easier and more pleasant to use from both C# and F#. Creating
samples that use Tensorflow is a good way of finding easy wins on the usability
of the API, there are some here:

https://github.com/tensorflow/models

Packaging

Mobile: we need to package the library for consumption on Android and iOS.

Documentation Styling

The API documentation has not been styled, I am using the barebones template
for documentation, and it can use some work.

Issues

I have logged some usability problems and bugs in Issues, feel free to take
on one of those tasks.

Notes on OpDefs

Look at:

./tensorflow/core/ops/ops.pbtxt AvgPool3D and:
./tensorflow/core/ops/nn_ops.cc for the C++ implementation with type definitions

Docs on types:
https://www.tensorflow.org/extend/adding_an_op

Documentation

Much of the online documentation comes from TensorFlow and is licensed under
the terms of Apache 2 License, in particular all the generated documentation
for the various operations that is generated by using the tensorflow reflection
APIs.

Last API update: a4b352bfddd518b540c30e456f3bc0027ba9351f

Release Notes

Adds support for TensorFlor 1.5

* This brings support for the TensorFlow 1.5 API
* New transpose overload without explicit perm parameter (Cesar Souza)
* New ReduceProd method (Cesar Souza)
* Supports for TensorFlow.Cond (Cesar Souza)

Showing the top 1 GitHub repositories that depend on TensorFlowSharp:

Repository Stars
cesarsouza/keras-sharp
An ongoing effort to port the Keras deep learning library to C#, supporting both TensorFlow and CNTK

Version History

Version Downloads Last updated
1.13.0 10,531 5/1/2019
1.12.0 20,480 12/6/2018
1.11.0 8,610 10/2/2018
1.10.0 3,801 9/7/2018
1.9.0 3,938 8/7/2018
1.9.0-pre1 400 8/2/2018
1.8.0-pre1 2,857 5/25/2018
1.7.0 14,193 4/15/2018
1.7.0-pre1 639 4/3/2018
1.6.0-pre1 913 3/11/2018
1.5.1-pre1 498 3/1/2018
1.5.0 9,584 1/27/2018
1.5.0-pre2 421 1/24/2018
1.5.0-pre1 437 1/14/2018
1.4.0 9,861 11/22/2017
1.4.0-pre1 928 11/5/2017
1.3.1-pre1 449 9/15/2017
1.3.0 2,741 9/15/2017
1.3.0-pre1 1,104 8/26/2017
1.2.2 9,441 6/28/2017
1.2.1 543 6/28/2017
0.96.0 7,639 5/21/2017
0.95.0 474 5/21/2017
0.94.0 513 5/21/2017
0.13.0 105 5/1/2019
Show less