Designing neural networks in C# using TensorFlow.NET


Image by Colin Behrens from Pixabay

As you can know or see according to my other posts, I enjoy exploring and learning new things about Machine Learning and Artificial Intelligence. One aspect that I am investigating is creating ML algorithms in the .NET environment. So far I have used ML.NET, but today I want to focus on another one. I will introduce you to how you can design a neural network in C# using TensorFlow.NET.


TensorFlow.NET is a library that provides .NET Standard binding for TensorFlow. Thanks to that, .NET developers have possibilities to design, train and deploy Machine Learning algorithms, among others, Neural Networks. This library enables us to take advantage of a variety of machine learning models and access a huge amount of resources that TensorFlow offers.

Feedforward Neural Network

Before the examples with the code, I would like to write some theory about the type of neural networks, the implementation of which I will present. FNN, also called multilayer perceptrons (MLP), are one of the pillars of deep learning models. The goal of a feedforward network is to approximate some function f. For instance, we have a classifier and y = f(x) maps an input  x  to a category y . Feedforward Neural Network specify a mapping y = f(x;\alpha) and learns the value of the parameters \alpha . Exactly these that result in the best approximation of a function. As you can see below, MLP perceptron consists of one input layer, at least one hidden layer and an output layer. In a feedforward network, information always goes one direction, it never moves backwards.

Feedforward Neural Network

Let’s code

After creating a console application project and downloading the following libraries from NuGet Packages:

you can proceed to implementation and model creation. In the beginning, you should create a class corresponding to your neural network. It should contain fields of model, training and test sets. The Model class come from TensorFlow.Keras.Engine and NDArray is just a part of NumSharp that is the equivalent of the NumPy library known in the Python world.

The next step is to create test and training sets. For this purpose, you can use MNIST datasets from Keras. MNIST is a huge database of digits that is used for training various image processing algorithms.

Now is the time for building a model and set up the options for your neural network. Here, among other things, you specify layers and their activation functions, an optimizer, a loss function and metrics. Explaining all these things is material for another post, but I recommend checking this article. The general concept of neural networks is easily explained there. So the implementation of our neural network is as follows:

In this example, we have a shape set up to 784, an input layer with the dimensionality of the output space equal to 64 and an output layer with 10 units. The activation function is ReLU (Rectified Linear Unit), the loss function is Sparse Categorical Crossentropy and the algorithm Adam is used as an optimizer. The quality of learning will be checked with accuracy.

After completing the previous steps, you can now go to training and testing the model:

The value of batch size, which denotes the subset size of your training sample, you can set to 8, then epochs to 2. So now you can finally create an instance of the Fnn class and execute the code!

Results and summary

After starting the application, the training phase should begin. In the console, you should see something similar:

After some time, depending on how large the dataset you use, it should perform the testing phase:

As you can see, the accuracy was over 90%. Generally, the results seem very good, but our analysis is not complex and enough, of course. However, in this post, I wanted to focus on showing you how you can design a neural network. Until a few years ago, ML was associated only with programming languages such as Python or R. Thanks to libraries such as the one discussed here, C# is also starting to play a significant role. I hope it will continue in this direction! By the way, check my other posts about Machine Learning.

Scroll to top