Visual Neural Network Designer in ANNdotNET

Brief Introduction to ANNdotNET

ANNdotNET – is an open source project for deep learning on .NET platform (.NET Framework and .NET Core). The project is hosted at with more information at the

The project comes in two versions: GUI and CMD tool. The main purpose of the project is focus on building deep learning models without to be distracted with debugging the source code and installing/updating missing packages and environments. The user should no worry which version of ML Engine the application is using. In other words, the ANNdotNET is ideal in several scenarios:

  1. more focus on network development and training process using classic desktop approach, instead of focusing on coding,
  2. less time spending on debugging source code, more focusing on different configuration and parameter variants,
  3. ideal for engineers/users who are not familiar with supported programming languages,
  4. in case the problem requires coding more advanced custom models, or training process, ANNdotNET CMD provides high level of API for such implementation,
  5. all ml configurations files generated with GUI tool, can be handled with CMD tool and vice versa.

With ANNdotNET GUI Tool the user can prepare data for training, by performing several actions: data cleaning, feature selection, category encoding, missing values handling, and create training and validation dataset prior to start building deep neural network. Once the data is prepared, the user can create Machine Learning Configuration (mlconfig) file in order to start building and training deep neural network. All previous actions user can handle using GUI tool implemented in the application.

For persisting information about data preparation and transformation actions, the application uses annproject file type which consists information about raw dataset, metadata information and information about mlconfig files.

The machine learning configurations are stored in separated files with mlconfig file extension. For more information about files in ANNdotNET the reader may open this link. The following image shows how ANNdotNET handles annproject and corresponded machine learning configurations within the annproject:

As can be seen the annproject can be consisted of arbitrary number of mlconfigs, which is typical scenario when working on ML Project. User can switch between mlconfigs any time except when the application is in training or evaluation mode.

ANNdotNET ML Engine

ANNdotNET introduces the ANNdotNET Machine Learning Engine (MLEngine) which is responsible for training and evaluation models defined in the mlconfig files.The ML Engine relies on Microsoft Cognitive Toolkit, CNTK open source library which is proved to be one of the best open source library for deep learning. Through all application ML Engine exposed all great features of the CNTK e.g. GPU support for training and evaluation, different kind of learners, but also extends CNTK features with more Evaluation functions (RMSE, MSE, Classification Accuracy, Coefficient of Determination, etc.), Extended Mini-batch Sources, Trainer and Evaluaton models.

ML Engine also contains the implementation of neural network layers which supposed to be high level CNTK API very similar as layer implementation in Keras and other python based deep learning APIs. With this implementation the ANNdotNET implements the Visual Neural Network Designer called ANNdotNET NNDesigner which allows the user to design neural network configuration of any size with any type of the layers. In the first release the following layers are implemented:

  • Normalization Layer – takes the numerical features and normalizes its values before getting to the network. More information can be found here.
  • Dense – classic neural network layer with activation function
  • LSTM – LSTM layer with option for peephole and self-stabilization.
  • Embedding – Embedding layer,
  • Drop – drop layer.

More layer types will be added in the future release.

Designing the neural network can be simplify by using pre-defined layer. So on this way we can implement almost any network we usually implement through the source code.

How to use ANNdotNET NNDesigner

Once the MLConfig is created user can open it and start building neural network. NNDesigner is placed in the Network Setting tab page. The following image shows the Network Setting tab page.

NNetwork Designer contains combo box with supported NN layers, and two action buttons for adding and removing layers in/from the network. Adding and removing layers is simple as adding and removing items in/from the list box. In order to add a layer, select the item from the combo box, and press Add button. In order to remove the layer form the network, click the layer in the listbox and press Remove button, then confirm deletion. In order to successfully create the network, the last layer in the list must be created with the same output dimension as the Output layer shown on the left side of the window, otherwise the warning messages will appear about this information once the training is stared.

Once the layer is added to the list it must be configured. The layer configuration depends of its type . The main parameter for each layer is output dimension and activation function, except the drop and normalization layer. The following text explains parameters for all supported layers:

Normalization layer – does not require any parameter. The following image shows the normalization item in the NNDesigner. You can insert only one normalization layer, and it is positioned at the first place.

Drop layer – requires percentage drop value which is integer value. The following image shows how drop layer looks in the NNDesigner. There is no any constrains for this layer.

Embedding layer – requires only output dimension to be configured. There is no any constrains for the layer. The following image shows how it looks in the NNDesigner:

Dense layer – requires output dimension and activation function to be configured. There is no any constrains for the layer.

LSTM layer – requires: output and cell dimension, activation function, and two Boolean parameters to enable peephole and self-stabilization variant in the layer. The following image shows how LSTM item looks in the NNDesigner.

The LSTM layer has some constrains which is already implemented in the code. In case two LSTM layers are added in the network, the network becomes the Stacked LSTM which should be treated differently. Also all LSTM layers are inserted as stack, and they cannot be inserted on different places in the list. The implementation of the Stacked LSTM layer will be shown later.

Different network configurations

In this section, various network configuration will be listed, in order to show how easy is to use NNDesigner to create very complex neural network configurations. Network examples are implemented in pre-calculated examples which come with default ANNdotNET installation package.

Feed Forward network

This example shows how to implement Feed Forward network, with one hidden and one output layer which is the last layer in the NNDesinger. The example is part of the ANNdotNET installation package.

Feed Forward with Normalization layer

This example shows feed forward network with normalization layer as the first layer. The example of this configuration can be found in the installation package of the ANNdotNET.

Feed Forward Network with Embedding layers

In this example embedding layers are used in order to reduce the dimensions of the input layer. Network is configured with 3 embedding layers, one hidden and output layer. The example is part of the ANNdotNET installation package.

Deep Neural Network

This example shows deep neural network with three kind of layers: Embedding, Drop and Dense layers. The project is part of the ANNdotNET installation package.

LSTM Deep Neural Network

This example shows how to configure LSTM based network. The network consist of normalization, embedding, drop, dense and LSTM layers. The project is part of the ANNdotNET installation package.

Stacked LSTM Neural Network

This is example of Stacked LSTM network, consist of multiple LSTM layers connected into stack. The example is part of the installation package.

The complete list of examples can be seen at the ANNdotNET Start Page. In order to open the example, the user just need to click the link. Hope this project will be useful for many ml scenarios.

Linear Regression with CNTK and C#

CNTK is Microsoft’s deep learning tool for training very large and complex neural network models. However, you can use CNTK for various other purposes. In some of the previous posts we have seen how to use CNTK to perform matrix multiplication, in order to calculate descriptive statistics parameters on data set.
In this blog post we are going to implement simple linear regression model, LR. The model contains only one neuron. The model also contains bias parameters, so in total the linear regression has only two parameters: w and b.
The image below shows LR model:

The reason why we use the CNTK to solve such a simple task is very straightforward. Learning on simple models like this one, we can see how the CNTK library works, and see some of not-so-trivial actions in CNTK.
The model shown above can be easily extend to logistic regression model, by adding activation function. Besides the linear regression which represent the neural network configuration without activation function, the Logistic Regression is the simplest neural network configuration which includes activation function.

The following image shows logistic regression model:
In case you want to see more info about how to create Logistic Regression with CNTK, you can see this official demo example.
Now that we made some introduction to the neural network models, we can start by defining the data set. Assume we have simple data set which represent the simple linear function y=2x+1. The generated data set is shown in the following table:

We already know that the linear regression parameters for presented data set are: b_0=1 and b_1=2, so we want to engage the CNTK library in order to get those values, or at least parameter values which are very close to them.

All task about how the develop LR model by using CNTK can be described in several steps:

Step 1: Create C# Console application in Visual Studio, change the current architecture to x64, and add the latest “CNTK.GPU “ NuGet package in the solution. The following image shows those action performed in Visual Studio.

Step 2: Start writing code by adding two variables: X – feature, and label Y. Once the variables are defined, start with defining the training data set by creating batch. The following code snippet shows how to create variables and batch, as well as how to start writing CNTK based C# code.

First we need to add some using statements, and define the device where computation will be happen. Usually, we can defined CPU or GPU in case the machine contains NVIDIA compatible graphics card. So the demo starts with the following cod snippet:

using System;
using System.Linq;
using System.Collections.Generic;
using CNTK;
namespace LR_CNTK_Demo
    class Program
        static void Main(string[] args)
             //Step 1: Create some Demo helpers
             Console.Title = "Linear Regression with CNTK!";
             Console.WriteLine("#### Linear Regression with CNTK! ####");
            //define device
            var device = DeviceDescriptor.UseDefaultDevice();

Now define two variables, and data set presented in the previous table:

//Step 2: define values, and variables
Variable x = Variable.InputVariable(new int[] { 1 }, DataType.Float, "input");
Variable y = Variable.InputVariable(new int[] { 1 }, DataType.Float, "output");

//Step 2: define training data set from table above
var xValues = Value.CreateBatch(new NDShape(1, 1), new float[] { 1f, 2f, 3f, 4f, 5f }, device);
var yValues = Value.CreateBatch(new NDShape(1, 1), new float[] { 3f, 5f, 7f, 9f, 11f }, device);

Step 3: Create linear regression network model, by passing input variable and device for computation. As we already discussed, the model consists of one neuron and one bias parameter. The following method implements LR network model:

private static Function createLRModel(Variable x, DeviceDescriptor device)
    //initializer for parameters
    var initV = CNTKLib.GlorotUniformInitializer(1.0, 1, 0, 1);

    var b = new Parameter(new NDShape(1,1), DataType.Float, initV, device, "b"); ;

    var W = new Parameter(new NDShape(2, 1), DataType.Float, initV, device, "w");

    //matrix product
    var Wx = CNTKLib.Times(W, x, "wx");

    var l = CNTKLib.Plus(b, Wx, "wx_b");

    return l;

First, we create initializer, which will initialize startup values of network parameters. Then we defined bias and weight parameters, and join them in form of linear model “wx+b”, and returned as Function type. The createModel function is called in the main method. Once the model is created, we can exam it, and prove there are only two parameters in the model. The following code create the Linear Regression model, and print model parameters:

//Step 3: create linear regression model
var lr = createLRModel(x, device);
//Network model contains only two parameters b and w, so we query
//the model in order to get parameter values
var paramValues = lr.Inputs.Where(z => z.IsParameter).ToList();
var totalParameters = paramValues.Sum(c => c.Shape.TotalSize);
Console.WriteLine($"LRM has {totalParameters} params, {paramValues[0].Name} and {paramValues[1].Name}.");

In the previous code, we have seen how to extract parameters from the model. Once we have parameters, we can change its values, or just print those values for the further analysis.

Step 4: Create Trainer, which will be used to train network parameters w and b. The following code snippet shows implementation of Trainer method.

public Trainer createTrainer(Function network, Variable target)
    //learning rate
    var lrate = 0.082;
    var lr = new TrainingParameterScheduleDouble(lrate);
//network parameters
    var zParams = new ParameterVector(network.Parameters().ToList());

    //create loss and eval
    Function loss = CNTKLib.SquaredError(network, target);
    Function eval = CNTKLib.SquaredError(network, target);

    var llr = new List();
    var msgd = Learner.SGDLearner(network.Parameters(), lr);

    var trainer = Trainer.CreateTrainer(network, loss, eval, llr);
    return trainer;

First we defined learning rate the main neural network parameter. Then we create Loss and Evaluation functions. With those parameters we can create SGD learner. Once the SGD learner object is instantiated, the trainer is created by calling CreateTrainer static CNTK method, and passed it further as function return. The method createTrainer is called in the main method:

//Step 4: create trainer
var trainer = createTrainer(lr, y);

Step 5: Training process: Once the variables, data set, network model and trainer are defined, the training process can be started.

//Ştep 5: training
for (int i = 1; i <= 200; i++)
var d = new Dictionary();
d.Add(x, xValues);
d.Add(y, yValues);
trainer.TrainMinibatch(d, true, device);
var loss = trainer.PreviousMinibatchLossAverage();
var eval = trainer.PreviousMinibatchEvaluationAverage();
if (i % 20 == 0)
    Console.WriteLine($"It={i}, Loss={loss}, Eval={eval}");

    //print weights
    var b0_name = paramValues[0].Name;
    var b0 = new Value(paramValues[0].GetValue()).GetDenseData(paramValues[0]);
    var b1_name = paramValues[1].Name;
    var b1 = new Value(paramValues[1].GetValue()).GetDenseData(paramValues[1]);
    Console.WriteLine($" ");
    Console.WriteLine($"Training process finished with the following regression parameters:");
    Console.WriteLine($"b={b0[0][0]}, w={b1[0][0]}");
    Console.WriteLine($" ");

As can be seen, in just 200 iterations, regression parameters got the values we almost expected b_0=0.995, and w=2.005. Since the training process is different than classic regression parameter determination, we cannot get exact values. In order to estimate regression parameters, the neural network uses iteration methods called Stochastic Gradient Decadent, SGD. On the other hand, classic regression uses regression analysis procedures by minimizing the least square error, and solve system equations where unknowns are b and w.
Once we implement all code above, we can start LR demo by pressing F5. Similar output window should be shown:

Hope this blog post can provide enough information to start with CNTK C# and Machine Learning. Source code for this blog post can be downloaded here.

Using CNTK and C# to train Mario to drive Kart


In this blog post I am going to explain one of possible way how to implement Deep Learning to play video game. For this purpose I used the following:

  1. N64 Nintendo emulator which can be found here,
  2. Mario Kart 64 ROM, which can be found on internet as well,
  3. CNTK – Microsoft Cognitive Toolkit
  4. .NET Framework and C#

The idea behind this machine learning project is to capture images together with action, while you play Mario Kart game. Then captured images are transformed into features of training data set, and action keys into label hot vectors respectively.  Since we need to capture images, the emulator should be positioned at fixed location and resized to fixed size during playing the game, testing algorithm to play game, and play the game. The flowing image shows N64 emulator graphics configuration settings.


Also the N64 emulator is positioned to Top-Left corned of screen, so it is easier to capture the images.

Data collection for training data set

During image captures, game is played as you would play normally. Also no special agent, nor platform is required.

In .NET and C#, it is implemented image capture from the specific position of screen, as well as it is recorded which keys are pressed during game play. In order to record keys press, the code found here is modified and used.

The flowing image shows the position of N64 emulator with Mario Kart game (1), the window which shows the captured and transformed images (2), and .NET Console  application with the implementation (3).


The data is generated on the following way:

  • each image is captured, resized to 100×74 pixels and gray scaled prior to be transformed and persisted to data set training file.
  • before image is persisted the hotkey of action key press is recorded and connected to image.

So the training data is persisted into CNTK format which consist of:

  1. |label – which represent 5 component hot vector, indicate: Forward, Break, Forward-Left, Forward-Right and None (1 0 0 0 0)
  2. |features consist of 100×74 numbers which represent pixels of the images.

The following data sample shows how training data set are persisted in the txt file:

|label 1 0 0 0 0 |features 202 202 202 202 202 202 204 189 234 209 199...
|label 0 1 0 0 0 |features 201 201 201 201 201 201 201 201 203 18...
|label 0 0 1 0 0 |features 199 199 199 199 199 199 199 199 199 19...
|label 0 0 0 1 1 |features 199 199 199 199 199 199 199 199 199 19...

Since my training data is more than 300 000 MB of size, I provided just few BM sized file, but you can generate file as big as you wish with just playing the game, and running the flowing code from Program.cs file:

await GenerateData.Start();

Model training to play the game

Once we generate the data, we can move to the next step: training RCNN model to play the game. For training model the CNTK is used. Since we play a game which the previous sequence will determined the next sequence in the game, the LSTM RNN is used. More information about CNTK and LSTM can be found in previous posts.

In my case I have collected nearly 15000 images during several round of playing the same level and route. Also for more accurate model much more images should be collected, nearly 100 000. The model is trained in one hour, with 500000 iterations. The source code about whole project can be found on GitHub page. ( )

By running the following code, the training process is started with provided training data:


Playing the game with CNTK model

Once we trained the model, we move to the next step: playing a game.

The emulator should be positioned on the same position and with the same size as we had while generating training data.Once the model is trained and created, the playing game can be achieve by running:

var dev = DeviceDescriptor.UseDefaultDevice();
MarioKartPlay.LoadModel("../../../../training/mario_kart_modelv1", dev);

How it looks like on my case, you can see on this you tube video:

Using CNTK with Visual Studio 2017 and Python

In the next few steps will show how to install CNTK and python environment in Visual Studio 2017.

  1. First download the latest CNTK version from the official GitHub page, or just click on the following link:

The release page will show the latest bits. Click on the CPU only package, accept the license and download the zip file.

  1. Once that you have zip file on your PC, create the folder C:/local on disk and unzip the package in to it.
  2. The next step performs the installation of the library as well as installation of the Python related distribution anaconda 4.1.1.
  3. Open C:\local\cntk\Scripts\install\windows path and run install.bat file. You will need administrative rights in order to successfully install all required components.
  4. The following image shows the installation process:

  1. As can be seen first you have to run batch file (step 2), then press 1 and ENTER in order to continue with the installation process and press ‘y‘, to perform downloading required components.
  2. The installation process takes several minutes to complete. The first component to be installed is Anaconda 4.1.1 which is needed in order to setup  CNTK.

  1. Once the anaconda is installed, the process of CNTK installation starts and passes very quickly since we already download all CNTK bits.

  1. Now that we have CNTK installed, the last installation step is installation of the Visual Studio Tool for Python.
  2. Run the Visual Studio 2017 Installer and after the installed is show, just select the python components similar picture shows below:

  1. Once the installation is completed run Visual Studio 2017.
  2. From the Visual Studio 2017 Tool menu select Python and then select Python Environment:

  1. From the Python Environment window select Anaconda 4.1.1 and update symbols DB, by pressing the button pointed on the image below:

  1. Once we have environment updated, Press “Make this the default environment for the new projects” option in order to apply the environment for the future Python CNTK based projects.
  2. Also the path for Python and Python scripts should be registered in Global Environment OS.

  1. Once the previous steps are performed successfully, we can start writing CNTK aware python code in Visual Studio 2017.
  2. OPen VS 2017 and Anaconda 4.1.1 environment and type.

   import cntk

print(“CNTK verion:”, cntk__version__)

  1. Similar output should be appear
  2. print(“CNTK version:”, cntk.__version__)