ANNdotNET – the first GUI based CNTK tool

ANNdotNET is windows desktop application written in C# for creating and training ANN models. The application relies on Microsoft Cognitive Toolkit, CNTK, and it is supposed to be GUI tool for CNTK library with extensions in data preprocessing, model evaluation and exporting capabilities. It is hosted at GitHub and can be clone from http://github.com/bhrnjica/anndotnet

Currently, ANNdotNET supports the folowing type of ANN:

• Simple Feed Forward NN
• Deep Feed Forward NN
• Recurrent NN with LSTM

The process of creating, training, evaluating and exporting models is provided from the GUI Application and does not require knowledge for supported programming languages. The ANNdotNET is ideal for engineers which are not familiar with programming languages.

Software Requirements

ANNdotNET is x64 Windows desktop application which is running on .NET Framework 4.7.1. In order to run the application, the following requirements must be met:

– Windows 7, 8 or 10 with x64 architecture
– NET Framework 4.7.1
– CPU/GPU support.

Note: The application automatically detect GPU capability on your machine and use it in training and evaluation, otherwise it will use CPU.

How to run application

In order to run the application there are two possibilities:

Clone the GitHub repository of the application and open it in Visual Studio 2017.

1. Change build architecture into x64, build and run the application.

The following three short videos quickly show how to create, train and evaluate regression, binary and multi class classification models.

• Training regression model. Data set is Concrete Slump Test is downloaded from the UCI ML Repository and loaded into ANNdotNET without any modification, since the data preparation module can prepare it.

2. Training and evaluation binary classifier model. Data represent Titanic data set downloaded from the public repository.

3. Training and evaluation multi class classification models. Data represents Iris data set downloaded from the same page as above.

Announcement of GPdotNET v5 and ANNdotNET v1.0

As you already know GPdotNET v4 tool consists of several modules which include:

• GP module for creating and training models based on genetic programming,
• ANN module for creating and training models based on Feed Forward Neural Networks,
• GA module for model and function optimization using Genetic Algorithm
• LGA module is for  linear programming with GA which includes solving Traveling Salesman based problems, Assignment and Transportation problems.

With the latest release the GPdotNET has changed a lot. First of all, the initial idea about GPdotNET was to provide GP method in the application. And as the project grew lot of new implementations were included in the main project. This year I decided to make two different projects which can be seen as the natural evolution of GPdotNET v4.

The first project remain the same which follows the previous version and it is called GPdotNET v5. The project includes only GP related algorithm implementation which is developed for creating and training supervised ML problems (regression, binary and multi-class classification).

The second project uses several ANN algorithms for creating and training supervised machine learning problems.  The project is called ANNdotNET. It is Windows Forms desktop application very similar with GPdotNET, for creating and training ANN models.

I am very prod to announce that the new version of GPdotNET will be released as two  different open source projects.

1. GPdotNET v5 – which is hosted at the same address as previous. The older version GPdotNET v4 has moved at http://github.com/bhrnjica/gpdotnetv4  – and will be the latest version for non GP and ANN modules in GPdotNET.
2. ANNdotNET v1 – is hosted at separate repository http://github.com/bhrnjica/anndotnet.

Data Preparation Tool for Machine Learning

Regardless of machine learning library you use, the data preparation is the first and one of the most important step in developing predictive models. It is very often case that the data supposed to be used for the training is dirty with lot of unnecessary columns, full of missing values, un-formatted numbers etc. Before training the data must be cleaned and properly defined in order to get good model. This is known as data preparation. The data preparation consist of cleaning the data, defining features and labels, deriving the new features from the existing data, handling missing values, scaling the data etc.  It can be concluded that the total time we spend in ML modelling,the most of it is related to data preparation.

In this blog post I am going to present the simple tool which can significantly reduce the preparation time for ML. The tool simply loads the data in to GUI, and then the user can define all necessary information. Once the data is prepared user can store the data it to files which can be then directly imported into ML algorithm such as CNTK.

The following image shows the ML Data Preparation Tool main window.

From the image above, the data preparation can be achieved in several steps.

1. Load dirty data into ML Prep Tool, by pressing Import Data button
2. Transform the data by providing the flowing:
1. Type – each column can be:
1. Numeric – which holds continuous numeric values,
2. Binary – which indicates two class categorical data,
3. Category – which indicates categorical data with more than two classes,
4. String – which indicate the column will not be part of training and testing data set,
2. Encoding – in case of Binary and Category column type, the encoding must be defined. The flowing encoding is supported:
1. Binary Encoding with (0,1) – first binary values will be 0, and second binary values will be 1.
2. Binary encoding with (-1,1) – first binary values will be -1, and second binary values will be 1.
3. Category Level- which each class treats as numeric value. In case of 3 categories(R,G, B), encoding will be (0,1,2)
4. Category 1:N- implements One-Hot vector with N columns. In case of 3 categories(R,G, B), encoding will be R =  (1,0,0),G =  (0,1,0), B =  (0,0,1).
5. Category 1:N-1(0) – implements dummy coding with N-1 columns. In case of 3 categories(R, G, B), encoding will be R =  (1,0),G =  (0,1), B =  (0,0).
6. Category 1:N-1(-1) – implements dummy coding with N-1 columns. In case of 3 categories(R, G, B), encoding will be R =  (1,0),G =  (0,1), B =  (-1,-1).
3. Variable – defines features and label. Only one label, and at least one features can be defined. Also the column can be defined as Ignore variable, which will skip that column.  The following options are sported:
1. Input – which identifies the column as feature or predictor,
2. Output – which identifies the column as label or model output.
4. Scaling – defines column scaling. Two scaling options are supported:
1. MinMax,
2. Gauss Standardization,
5. Missing Values – defines the replacement for the missing value withing the column. There are several options related to numeric and two options (Random and Mode ) for categorical type.
3. Define the testing data set size by providing information of row numbers or percent.
4. Define export options
5. Press Export Button.

As can be seen this is straightforward workflow of data preparation.

Besides the general export options which can be achieved by selecting different delimiter options, you can export data set in to CNTK format, which is very handy if you play with CNTK.

After data transformations, the user need to check CNTK format int the export options and press Export in order to get CNTK training and testing files, which can be directly used in the code without any modifications.

Some of examples will be provided int he next blog post.

The project is hosted at GitHub, where the source code can be freely downloaded and used at this location: https://github.com/bhrnjica/MLDataPreparationTool .

In case you want only binaries, the release of version v1.0 is published here: https://github.com/bhrnjica/MLDataPreparationTool/releases/tag/v1.0

CNTK 106 Tutorial – Time Series prediction with LSTM using C#

In this post will show how to implement CNTK 106 Tutorial in C#. This tutorial lecture is written in Python and there is no related example in C#. For this reason I decided to translate this very good tutorial into C#. The tutorial can be found at: CNTK 106: Part A – Time series prediction with LSTM (Basics)  and uses sin wave function in order to predict time series data. For this problem the Long Short Term Memory, LSTM, Recurrent Neural Network is used.

Goal

The goal of this tutorial is prediction the simulated data of a continuous function ( sin wave). From $N$ previous values of the $y=sin(t)$ function where $y$ is the observed amplitude signal at time $t$, prediction of  $M$ values of $y$ is going to predict for the corresponding future time points.

The excitement of this tutorial is using the LSTM recurrent neural network which is nicely suited for this kind of problems. As you probably know LSTM is special recurrent neural network which has ability to learn from its experience during the training. More information about this fantastic version of recurrent neural network can be found here.

The blog post is divided into several sub-sections:

1. Simulated data part
2. LSTM Network
3. Model training and evaluation

Since the simulated data set is huge, the original tutorial has two running mode which is described by the variable isFast. In case of fast mode, the variable is set to True, and this mode will be used in this tutorial. Later, the reader may change the value to False in order to see much better training model, but the training time will be much longer. The Demo for this this blog post exposes variables of the batch size and iteration number to the user, so the user may defined those numbers as he/she want.

Data generation

In order to generate simulated sin wave data, we are going to implement several helper methods. Let $N$ and $M$  be a ordered set of past values and future (desired predicted values) of the sine wave, respectively. The two methods are implemented:

1. generateWaveDataset()

The generateWaveDataset takes the periodic function,set of independent values (which is corresponded the time for this case) and generate the wave function, by providing the time steps and time shift. The method is related to the generate_data() python methods from the original tutorial.

```static Dictionary<string, (float[][] train, float[][] valid, float[][] test)> loadWaveDataset(Func<double, double> fun, float[] x0, int timeSteps, int timeShift)
{
////fill data
float[] xsin = new float[x0.Length];//all data
for (int l = 0; l < x0.Length; l++)
xsin[l] = (float)fun(x0[l]);

//split data on training and testing part
var a = new float[xsin.Length - timeShift];
var b = new float[xsin.Length - timeShift];

for (int l = 0; l < xsin.Length; l++)
{
//
if (l < xsin.Length - timeShift) a[l] = xsin[l]; // if (l >= timeShift)
b[l - timeShift] = xsin[l];
}

//make arrays of data
var a1 = new List<float[]>();
var b1 = new List<float[]>();
for (int i = 0; i < a.Length - timeSteps + 1; i++)
{
//features
var row = new float[timeSteps];
for (int j = 0; j < timeSteps; j++)
row[j] = a[i + j];
//create features row
//label row
b1.Add(new float[] { b[i + timeSteps - 1] });
}

//split data into train, validation and test data set
var xxx = splitData(a1.ToArray(), 0.1f, 0.1f);
var yyy = splitData(b1.ToArray(), 0.1f, 0.1f);

var retVal = new Dictionary<string, (float[][] train, float[][] valid, float[][] test)>();
return retVal;
}
```

Once the data is generated, three datasets should be created: train, validate and test dataset, which are generated by splitting the dataset generated by the above method. The following splitData method splits the original sin wave dataset into three datasets,

```static (float[][] train, float[][] valid, float[][] test) splitData(float[][] data, float valSize = 0.1f, float testSize = 0.1f)
{
//calculate
var posTest = (int)(data.Length * (1 - testSize));
var posVal = (int)(posTest * (1 - valSize));

return (data.Skip(0).Take(posVal).ToArray(), data.Skip(posVal).Take(posTest - posVal).ToArray(), data.Skip(posTest).ToArray());
}
```

In order to visualize the data, the Windows Forms project is created. Moreover, the ZedGraph .NET class library is used in order to visualize the data. The following picture shows the generated data.

Network modeling

As mentioned on the beginning of the blog post, we are going to create LSTM recurrent neural network, with 1 LSTM cell for each input. We have N inputs and each input is a value in our continuous function. The N outputs from the LSTM are the input into a dense layer that produces a single output. Between LSTM and dense layer we insert a dropout layer that randomly drops 20% of the values coming from the LSTM to prevent overfitting the model to the training dataset. We want use use the dropout layer during training but when using the model to make predictions we don’t want to drop values.

The description above can be illustrated on the following picture:

The implementation of the LSTM can be sumarize in one method, but the real implementation can be viewed in the demo sample which is attached with this blog post.
The following methods implements LSTM network depicted on the image above. The arguments for the method are already defined.

```public static Function CreateModel(Variable input, int outDim, int LSTMDim, int cellDim, DeviceDescriptor device, string outputName)
{

Func<Variable, Function> pastValueRecurrenceHook = (x) => CNTKLib.PastValue(x);

//creating LSTM cell for each input variable
Function LSTMFunction = LSTMPComponentWithSelfStabilization<float>(
input,
new int[] { LSTMDim },
new int[] { cellDim },
pastValueRecurrenceHook,
pastValueRecurrenceHook,
device).Item1;

//after the LSTM sequence is created return the last cell in order to continue generating the network
Function lastCell = CNTKLib.SequenceLast(LSTMFunction);

//implement drop out for 10%
var dropOut = CNTKLib.Dropout(lastCell,0.2, 1);

//create last dense layer before output
var outputLayer =  FullyConnectedLinearLayer(dropOut, outDim, device, outputName);

return outputLayer;
}
```

Training the network

In order to train the model, the nextBatch() method is implemented that produces batches to feed the training function. Note that because CNTK supports variable sequence length, we must feed the batches as list of sequences. This is a convenience function to generate small batches of data often referred to as minibatch.

```private static IEnumerable<(float[] X, float[] Y)> nextBatch(float[][] X, float[][] Y, int mMSize)
{

float[] asBatch(float[][] data, int start, int count)
{
var lst = new List<float>();
for (int i = start; i < start + count; i++) { if (i >= data.Length)
break;

}
return lst.ToArray();
}

for (int i = 0; i <= X.Length - 1; i += mMSize) { var size = X.Length - i; if (size > 0 && size > mMSize)
size = mMSize;

var x = asBatch(X, i, size);
var y = asBatch(Y, i, size);

yield return (x, y);
}
}
```

Note: Since the this tutorial is implemented as WinForms C# project which can visualize training and testing datasets, as well as it  can show the best found model during the training process, there are lot of other implemented methods which are not mentioned here, but can be found in the demo source code attached in this blog post.

Key Insight

When working with LSTM the user should pay attention on the following:

Since LSTM must work with axes with unknown dimensions, the variables should be defined on different way as we could saw in the previous blog posts. So the input and the output variable are initialized with the following code listing:

```// build the model
var feature = Variable.InputVariable(new int[] { inDim }, DataType.Float, featuresName, null, false /*isSparse*/);
var label = Variable.InputVariable(new int[] { ouDim }, DataType.Float, labelsName, new List<CNTK.Axis>() { CNTK.Axis.DefaultBatchAxis() }, false);
```

As specified in the original tutorial: “Specifying the dynamic axes enables the recurrence engine handle the time sequence data in the expected order. Please take time to understand how to work with both static and dynamic axes in CNTK as described here, the dynamic axes is key point in LSTM.
Now the implementation is continue with the defining learning rate, momentum, the learner and the trainer.

```
var lstmModel = LSTMHelper.CreateModel(feature, ouDim, hiDim, cellDim, device, "timeSeriesOutput");

Function trainingLoss = CNTKLib.SquaredError(lstmModel, label, "squarederrorLoss");
Function prediction = CNTKLib.SquaredError(lstmModel, label, "squarederrorEval");

// prepare for training
TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble(0.0005, 1);
TrainingParameterScheduleDouble momentumTimeConstant = CNTKLib.MomentumAsTimeConstantSchedule(256);

IList<Learner> parameterLearners = new List<Learner>() {
Learner.MomentumSGDLearner(lstmModel.Parameters(), learningRatePerSample, momentumTimeConstant, /*unitGainMomentum = */true)  };

//create trainer
var trainer = Trainer.CreateTrainer(lstmModel, trainingLoss, prediction, parameterLearners);
```

Now the code is ready, and the 10 epochs should return acceptable result:

```
// train the model
for (int i = 1; i <= iteration; i++)
{
//get the next minibatch amount of data
foreach (var miniBatchData in nextBatch(featureSet.train, labelSet.train, batchSize))
{
var xValues = Value.CreateBatch<float>(new NDShape(1, inDim), miniBatchData.X, device);
var yValues = Value.CreateBatch<float>(new NDShape(1, ouDim), miniBatchData.Y, device);

//Combine variables and data in to Dictionary for the training
var batchData = new Dictionary<Variable, Value>();

//train minibarch data
trainer.TrainMinibatch(batchData, device);
}

if (this.InvokeRequired)
{
// Execute the same method, but this time on the GUI thread
this.Invoke(
new Action(() =>
{
//output training process
progressReport(trainer, lstmModel.Clone(), i, device);
}
));
}
else
{
//output training process
progressReport(trainer, lstmModel.Clone(), i, device);

}
}
```

Model Evaluation

Model evaluation is implemented during the training process. In this way we can see the learning process and how the model is getting better and better.

Fore each minibatch the progress method is called which updates the charts for the training and testing data set.

```void progressReport(Trainer trainer, Function model, int iteration, DeviceDescriptor device)
{
textBox3.Text = iteration.ToString();
textBox4.Text = trainer.PreviousMinibatchLossAverage().ToString();
progressBar1.Value = iteration;

reportOnGraphs(trainer, model, iteration, device);
}

private void reportOnGraphs(Trainer trainer, Function model, int i, DeviceDescriptor device)
{
currentModelEvaluation(trainer, model, i, device);
currentModelTest(trainer, model, i, device);
}
```

The following picture shows the training process, where the model evaluation is shown simultaneously, for the training and testing data set.
Also the simulation of the Loss value during the training is simulated as well.

As can be see the blog post extends the original Tutorial with some handy tricks during the training process. Also this demo is good strarting point for development bether tool for LSTM Time Series training. The full source code of this blog post, which shows much more implementation than presented in the blog post can be found here.

How to save CNTK model to file in C#

Final process of training is the model which should be used in the production as safe, reliable and accurate. Usually model training is frustrating and time consuming process, and it is not like we can see as demo to introduce with the library. Once the model is built with right combination of parameter values and network architecture, the process of modelling turn in to interesting and funny task, since it calculates the values just as we expect.

In most of the time, we have to save the current state of the model, and continue with training due to various reasons:

• to change the parameters of the learner
• to switch from one to another machine,
• or to share the state of the model with your team mate,
• to switch from CPU to GPU,
• etc

In all above cases the current state of the model should be saved, and continue the training process from the last stage of the model. Because, the training from the beginning is not a solution, since we already invest time and knowledge to achieve progress of the model building.

The CNTK supports two kind of persisting the model.

• saving the checkpoint of the model for later training.

In the first case, the model is prepare for the evaluation and production but cannot be trained again, because it is freed from all other information but for the evaluation. During the saving process, only one files is generated.

In the second case, beside a model file, another file is generated with the name “modelname.ckp”. The file contains all information needed for the continuation of training.  Once the trainer  checkpoint is persisted we can continue with model training even if we changed the following:

• the training data set with the same dimensions and data types
• the parameters of the learner,
• the learner

What we cannot change in order to continue with training is the the network model. In other words, the model must remain with the same number of layers, input and output dimensions.

Once the model is trained it can be persisted as separated file. As separate file, it can be loaded and evaluated with different dataset, but the number of the features and the label must remain the same as in case when was trained. Use this method when you want to share the model with someone else, or when you want to deploy the model in the production.

The model is saved simply by calling the CNTK method  Save:

```public void SaveTrainedModel(Function model, string fileName)
{
model.Save(fileName);
}
```

The model evaluation requires several steps:

• load the model from the file,
• extract the features and label from the model
• call evaluate method from the model, by passing the batch created from the features, label and the evaluation dataset.

```public Function LoadTrainedModel(string fileName, DeviceDescriptor device)
{
}
```

Once the model is loaded, features and label are extracted from the model on the following way:

```//load the mdoel from file
//extract features and label from the model
Variable feature = ffnn_model.Arguments[0];
Variable label = ffnn_model.Output;
```

The next step is creating the minibatch in order to pass the data to the evaluation.In this case we are going to create only one row for the Iris example of:

```//Example: 5.0f, 3.5f, 1.3f, 0.3f, setosa
float[] xVal = new float[4] { 5.0f, 3.5f, 1.3f, 0.3f };
Value xValues = Value.CreateBatch<float>(new int[] {feature.Shape[0] }, xVal, device);
//Value yValues = - we don't need it, because we are going to calculate it
```

Once we created the variable and values we can map them and pass to the model evaluation, and calculate the result:

```//map the variables and values
var inputDataMap = new Dictionary<Variable, Value>();
var outputDataMap = new Dictionary<Variable, Value>();
//evaluate the model
ffnn_model.Evaluate(inputDataMap, outputDataMap, device);
//extract the result  as one hot vector
var outputData = outputDataMap[label].GetDenseData<float>(label);
```

The evaluation result should be transformed to proper format, and compared with expected result:

```//transforms into class value
var actualLabels = outputData.Select(l => l.IndexOf(l.Max())).ToList();
var flower = actualLabels.FirstOrDefault();
var strFlower = flower == 0 ? "setosa" : flower == 1 ? "versicolor" : "versicolor";
Console.WriteLine(\$"Model Prediction: Input({xVal[0]},{xVal[1]},{xVal[2]},{xVal[3]}), Iris Flower={strFlower}");
Console.WriteLine(\$"Model Expectation: Input({xVal[0]},{xVal[1]},{xVal[2]},{xVal[3]}), Iris Flower= setosa");
```

Training previous saved model

Training previously saved model is very simple, since it requires no special coding. Right after the trainer is created with all necessary stuff (network, learning rate, momentum and other),
you just need to call

``` trainer.RestoreFromCheckpoint(strIrisFilePath);
```

The above method is called, after you successfully saved the model state by calling

```trainer.SaveCheckpoint(strIrisFilePath);
```

The method is usually called at the end of the training process.
Complete source code from this blog post can be found here.

Testing and Validation CNTK models using C#

…continue from the previous post.
Once the model is build and Loss and Validation functions are satisfied our expectation, we need to validate and test the model using the data which was not part of the training data set (unseen data). The model validation is very important because we want to see if our model is trained well,so that can evaluates unseen data approximately same as the training data. Otherwise the model which cannot predict the output is called overfitted model. Overfitting can happen when the model was trained long enough that shows very high performance for the training data set, but for the testing data evaluate bad results.
We will continue with the implementation from the prevision two posts, and implement model validation. After the model is trained, the model and the trainer are passed to the Evaluation method. The evaluation method loads the testing data and calculated the output using passed model. Then it compares calculated (predicted) values with the output from the testing data set and calculated the accuracy. The following source code shows the evaluation implementation.

```private static void EvaluateIrisModel(Function ffnn_model, Trainer trainer, DeviceDescriptor device)
{
var dataFolder = "Data";//files must be on the same folder as program
var trainPath = Path.Combine(dataFolder, "testIris_cntk.txt");
var featureStreamName = "features";
var labelsStreamName = "label";

//extract features and label from the model
var feature = ffnn_model.Arguments[0];
var label = ffnn_model.Output;

//stream configuration to distinct features and labels in the file
var streamConfig = new StreamConfiguration[]
{
new StreamConfiguration(featureStreamName, feature.Shape[0]),
new StreamConfiguration(labelsStreamName, label.Shape[0])
};

// prepare testing data
var testMinibatchSource = MinibatchSource.TextFormatMinibatchSource(
trainPath, streamConfig, MinibatchSource.InfinitelyRepeat, true);
var featureStreamInfo = testMinibatchSource.StreamInfo(featureStreamName);
var labelStreamInfo = testMinibatchSource.StreamInfo(labelsStreamName);

int batchSize = 20;
int miscountTotal = 0, totalCount = 20;
while (true)
{
var minibatchData = testMinibatchSource.GetNextMinibatch((uint)batchSize, device);
if (minibatchData == null || minibatchData.Count == 0)
break;
totalCount += (int)minibatchData[featureStreamInfo].numberOfSamples;

// expected labels are in the mini batch data.
var labelData = minibatchData[labelStreamInfo].data.GetDenseData<float>(label);
var expectedLabels = labelData.Select(l => l.IndexOf(l.Max())).ToList();

var inputDataMap = new Dictionary<Variable, Value>() {
{ feature, minibatchData[featureStreamInfo].data }
};

var outputDataMap = new Dictionary<Variable, Value>() {
{ label, null }
};

ffnn_model.Evaluate(inputDataMap, outputDataMap, device);
var outputData = outputDataMap[label].GetDenseData<float>(label);
var actualLabels = outputData.Select(l => l.IndexOf(l.Max())).ToList();

int misMatches = actualLabels.Zip(expectedLabels, (a, b) => a.Equals(b) ? 0 : 1).Sum();

miscountTotal += misMatches;
Console.WriteLine(\$"Validating Model: Total Samples = {totalCount}, Mis-classify Count = {miscountTotal}");

if (totalCount >= 20)
break;
}
Console.WriteLine(\$"---------------");
Console.WriteLine(\$"------TESTING SUMMARY--------");
float accuracy = (1.0F - miscountTotal / totalCount);
Console.WriteLine(\$"Model Accuracy = {accuracy}");
return;

}
```

The implemented method is called in the previous Training method.

``` EvaluateIrisModel(ffnn_model, trainer, device);

```

As can be seen the model validation has shown that the model predicts the data with high accuracy, which is shown on the following picture.

This was the latest post in series of blog posts about using Feed forward neural networks to train the Iris data using CNTK and C#.

The full source code for all three samples can be found here.

Train Iris data by Batch using CNTK and C#

In the previous post we have seen how to train NN model by using MinibatchSource. Usually we should use it when we have large amount of data. In case of small amount of the data, all data can be loaded in memory, and all can be passed to each iteration in order to train the model. This blog post will implement this kind of feeding the trainer.
We will reused the previous implementation, so the starting point can be previous source code. For data loading we have to define a new method. The Iris data is stored in text format like the following:

```sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa(1 0 0)
7.0,3.2,4.7,1.4,versicolor(0 1 0)
7.6,3.0,6.6,2.1,virginica(0 0 1)
...
```

The output column is encoded to 1-N-1 encoding rule we have seen previously.
The method will read all the data from the file, parse the data and create two float arrays:

• float[] feature, and
• float[] label.

As can be seen both arrays are 1D, which means all data will be inserted in 1D, because the CNTK requires so.  Since the data is in 1D array, we should also provide the dimensionality of the data so te CNTK can resolve what values for each features. The following listing shows the loading Iris data in two 1D array returned as tuple.

```static (float[], float[]) loadIrisDataset(string filePath, int featureDim, int numClasses)
{
var features = new List<float>();
var label = new List<float>();
for (int i = 1; i < rows.Length; i++)
{
var row = rows[i].Split(',');
var input = new float[featureDim];
for (int j = 0; j < featureDim; j++)
{
input[j] = float.Parse(row[j], CultureInfo.InvariantCulture);
}
var output = new float[numClasses];
for (int k = 0; k < numClasses; k++)
{
int oIndex = featureDim + k;
output[k] = float.Parse(row[oIndex], CultureInfo.InvariantCulture);
}

}

return (features.ToArray(), label.ToArray());
}
```

Once the data is loaded we should change very little amount of the previous code in order to implement batching instead of using minibatchSource. At the beginning we provides several variable to define the NN model structure. Then we call the loadIrisDataset, and define xValues and yValues, which we use in order to create feature and label input variables. Then we create dictionary which connect the feature and labels with data values which we will pass to the trainer later.
The next code is the same as in the previous version in order to create NN model, Loss and Evaluation functions, and learning rate.

Then we create loop, for 800 iteration. Once the iteration reaches the maximum value the program outputs the model properties and terminates.
Above said it implemented in the following code.

```public static void TrainIriswithBatch(DeviceDescriptor device)
{
//data file path
var iris_data_file = "Data/iris_with_hot_vector.csv";

//Network definition
int inputDim = 4;
int numOutputClasses = 3;
int numHiddenLayers = 1;
int hidenLayerDim = 6;
int sampleSize = 130;

var dataSet = loadIrisDataset(iris_data_file, inputDim, numOutputClasses);

// build a NN model
//define input and output variable
var xValues = Value.CreateBatch<float>(new NDShape(1, inputDim), dataSet.Item1, device);
var yValues    = Value.CreateBatch<float>(new NDShape(1, numOutputClasses), dataSet.Item2, device);

// build a NN model
//define input and output variable and connecting to the stream configuration
var feature = Variable.InputVariable(new NDShape(1, inputDim), DataType.Float);
var label = Variable.InputVariable(new NDShape(1, numOutputClasses), DataType.Float);

//Combine variables and data in to Dictionary for the training
var dic = new Dictionary<Variable, Value>();

//Build simple Feed Froward Neural Network model
// var ffnn_model = CreateMLPClassifier(device, numOutputClasses, hidenLayerDim, feature, classifierName);
var ffnn_model = createFFNN(feature, numHiddenLayers, hidenLayerDim, numOutputClasses, Activation.Tanh, "IrisNNModel", device);

//Loss and error functions definition
var trainingLoss = CNTKLib.CrossEntropyWithSoftmax(new Variable(ffnn_model), label, "lossFunction");
var classError = CNTKLib.ClassificationError(new Variable(ffnn_model), label, "classificationError");

// set learning rate for the network
var learningRatePerSample = new TrainingParameterScheduleDouble(0.001125, 1);

//define learners for the NN model
var ll = Learner.SGDLearner(ffnn_model.Parameters(), learningRatePerSample);

//define trainer based on ffnn_model, loss and error functions , and SGD learner
var trainer = Trainer.CreateTrainer(ffnn_model, trainingLoss, classError, new Learner[] { ll });

//Preparation for the iterative learning process
//used 800 epochs/iterations. Batch size will be the same as sample size since the data set is small
int epochs = 800;
int i = 0;
while (epochs > -1)
{
trainer.TrainMinibatch(dic, device);

//print progress
printTrainingProgress(trainer, i++, 50);

//
epochs--;
}
//Summary of training
double acc = Math.Round((1.0 - trainer.PreviousMinibatchEvaluationAverage()) * 100, 2);
Console.WriteLine(\$"------TRAINING SUMMARY--------");
Console.WriteLine(\$"The model trained with the accuracy {acc}%");
}
```

If we run the code, the output will be the same as we got from the previous blog post example: