Popularity
7.9
Growing
Activity
0.0
-
1,796
120
338

Description

SciSharp's philosophy allows a large number of machine learning code written in Python to be quickly migrated to .NET, enabling .NET developers to use cutting edge machine learning models and access a vast number of Tensorflow resources which would not be possible without this project.

In comparison to other projects, like for instance TensorFlowSharp which only provide Tensorflow's low-level C++ API and can only run models that were built using Python, Tensorflow.NET also implements Tensorflow's high level API where all the magic happens. This computation graph building layer is still under active development. Once it is completely implemented you can build new Machine Learning models in C# or F#.

Programming language: C#
License: Apache License 2.0
Latest version: v0.30

TensorFlow.NET alternatives and similar packages

Based on the "Machine Learning and Data Science" category.
Alternatively, view TensorFlow.NET alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of TensorFlow.NET or a related project?

Add another 'Machine Learning and Data Science' Package

README

[logo](docs/assets/tf.net.logo.png)

TensorFlow.NET (TF.NET) provides a .NET Standard binding for TensorFlow. It aims to implement the complete Tensorflow API in C# which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. TensorFlow.NET has built-in Keras high-level interface and is released as an independent package TensorFlow.Keras.

Join the chat at https://gitter.im/publiclab/publiclab Tensorflow.NET NuGet Documentation Status Badge Binder

master branch is based on tensorflow 2.3 now, v0.15-tensorflow1.15 is from tensorflow1.15.

[tensors_flowing](docs/assets/tensors_flowing.gif)

Why TensorFlow in C# and F# ?

SciSharp STACK's mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Since the APIs are kept as similar as possible you can immediately adapt any existing Tensorflow code in C# or F# with a zero learning curve. Take a look at a comparison picture and see how comfortably a Tensorflow/Python script translates into a C# program with TensorFlow.NET.

[pythn vs csharp](docs/assets/syntax-comparision.png)

SciSharp's philosophy allows a large number of machine learning code written in Python to be quickly migrated to .NET, enabling .NET developers to use cutting edge machine learning models and access a vast number of Tensorflow resources which would not be possible without this project.

In comparison to other projects, like for instance TensorFlowSharp which only provide Tensorflow's low-level C++ API and can only run models that were built using Python, Tensorflow.NET also implements Tensorflow's high level API where all the magic happens. This computation graph building layer is still under active development. Once it is completely implemented you can build new Machine Learning models in C# or F#.

How to use

TensorFlow tf native1.14 tf native 1.15 tf native 2.3
tf.net 0.30, tf.keras 0.1 x
tf.net 0.20 x x
tf.net 0.15 x x
tf.net 0.14 x

Install TF.NET and TensorFlow binary through NuGet.

### install tensorflow C#/F# binding
PM> Install-Package TensorFlow.NET
### install keras for tensorflow
PM> Install-Package TensorFlow.Keras

### Install tensorflow binary
### For CPU version
PM> Install-Package SciSharp.TensorFlow.Redist

### For GPU version (CUDA and cuDNN are required)
PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU

Import TF.NET and Keras API in your project.

using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

Linear Regression in Eager mode:

// Parameters        
int training_steps = 1000;
float learning_rate = 0.01f;
int display_step = 100;

// We can set a fixed init value in order to demo
var W = tf.Variable(-0.06f, name: "weight");
var b = tf.Variable(-0.73f, name: "bias");
var optimizer = tf.optimizers.SGD(learning_rate);

// Run training for the given number of steps.
foreach (var step in range(1, training_steps + 1))
{
    // Run the optimization to update W and b values.
    // Wrap computation inside a GradientTape for automatic differentiation.
    using var g = tf.GradientTape();
    // Linear regression (Wx + b).
    var pred = W * X + b;
    // Mean square error.
    var loss = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples);
    // should stop recording
    // Compute gradients.
    var gradients = g.gradient(loss, (W, b));

    // Update W and b following gradients.
    optimizer.apply_gradients(zip(gradients, (W, b)));

    if (step % display_step == 0)
    {
        pred = W * X + b;
        loss = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples);
        print($"step: {step}, loss: {loss.numpy()}, W: {W.numpy()}, b: {b.numpy()}");
    }
}

Run this example in Jupyter Notebook.

Toy version of ResNet in Keras functional API:

// input layer
var inputs = keras.Input(shape: (32, 32, 3), name: "img");

// convolutional layer
var x = layers.Conv2D(32, 3, activation: "relu").Apply(inputs);
x = layers.Conv2D(64, 3, activation: "relu").Apply(x);
var block_1_output = layers.MaxPooling2D(3).Apply(x);

x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_1_output);
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
var block_2_output = layers.add(x, block_1_output);

x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_2_output);
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
var block_3_output = layers.add(x, block_2_output);

x = layers.Conv2D(64, 3, activation: "relu").Apply(block_3_output);
x = layers.GlobalAveragePooling2D().Apply(x);
x = layers.Dense(256, activation: "relu").Apply(x);
x = layers.Dropout(0.5f).Apply(x);

// output layer
var outputs = layers.Dense(10).Apply(x);

// build keras model
model = keras.Model(inputs, outputs, name: "toy_resnet");
model.summary();

// compile keras model in tensorflow static graph
model.compile(optimizer: keras.optimizers.RMSprop(1e-3f),
    loss: keras.losses.CategoricalCrossentropy(from_logits: true),
    metrics: new[] { "acc" });

// prepare dataset
var ((x_train, y_train), (x_test, y_test)) = keras.datasets.cifar10.load_data();

// training
model.fit(x_train[new Slice(0, 1000)], y_train[new Slice(0, 1000)], 
          batch_size: 64, 
          epochs: 10, 
          validation_split: 0.2f);

Read the docs & book The Definitive Guide to Tensorflow.NET.

There are many examples reside at TensorFlow.NET Examples.

Troubleshooting of running example or installation, please refer [here](tensorflowlib/README.md).

Contribute:

Feel like contributing to one of the hottest projects in the Machine Learning field? Want to know how Tensorflow magically creates the computational graph? We appreciate every contribution however small. There are tasks for novices to experts alike, if everyone tackles only a small task the sum of contributions will be huge.

You can:

  • Let everyone know about this project
  • Port Tensorflow unit tests from Python to C# or F#
  • Port missing Tensorflow code from Python to C# or F#
  • Port Tensorflow examples to C# or F# and raise issues if you come accross missing parts of the API
  • Debug one of the unit tests that is marked as Ignored to get it to work
  • Debug one of the not yet working examples and get it to work

How to debug unit tests:

The best way to find out why a unit test is failing is to single step it in C# or F# and its corresponding Python at the same time to see where the flow of execution digresses or where variables exhibit different values. Good Python IDEs like PyCharm let you single step into the tensorflow library code.

Git Knowhow for Contributors

Add SciSharp/TensorFlow.NET as upstream to your local repo ...

git remote add upstream [email protected]:SciSharp/TensorFlow.NET.git

Please make sure you keep your fork up to date by regularly pulling from upstream.

git pull upstream master

Contact

Feel free to star or raise issue on Github.

Follow us on Medium.

Join our chat on Gitter.

Scan QR code to join Tencent TIM group:

[SciSharp STACK](docs/TIM.jpg)

WeChat Sponsor 微信打赏:

[SciSharp STACK](docs/assets/WeChatCollection.jpg)

TensorFlow.NET is a part of SciSharp STACK