Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / XML

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

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
7 Dec 2017CPOL5 min read 21.1K   4   12
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#.

In this post, I 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 of 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.

Image 7

The excitement of this tutorial is using the LSTM recurrent neural network which is nicely suited for these kind of problems. As you probably know, LSTM is a special recurrent neural network which has the 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 modes which are 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 blog post exposes variables of the batch size and iteration number to the user, so the user may define 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 an ordered set of past values and future (desired predicted values) of the sine wave, respectively. The two methods are implemented:

generateWaveDataset()

The generateWaveDataset takes the periodic function, set of independent values (which corresponded to the time for this case) and generates 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.

C#
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
        a1.Add(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)>();
    retVal.Add("features", xxx);
    retVal.Add("label", yyy);
    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:

C#
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.

Image 10

Network Modeling

As mentioned at 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 to 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 in the following picture:

Image 11

The implementation of the LSTM can be summarized 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.

C#
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.

C#
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;

            lst.AddRange(data[i]);
        }
        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 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 a 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 to the following:

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

C#
// 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.

C#
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:

C#
// 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>();
        batchData.Add(feature, xValues);
        batchData.Add(label, yValues);

        //train minibatch 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.

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

C#
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.

Image 12

As can be seen, the blog post extends the original tutorial with some handy tricks during the training process. Also, this demo is a good starting point for development of a better 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.

Filed under: .NET, C#, CNTK, CodeProject
Tagged: .NET, C#, CNTK, Code Project, CodeProject, Deep Neural Network, LSTM, Machine Learning, Neural Network, RNN
Image 13 Image 14

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Bosnia and Herzegovina Bosnia and Herzegovina
Bahrudin Hrnjica holds a Ph.D. degree in Technical Science/Engineering from University in Bihać.
Besides teaching at University, he is in the software industry for more than two decades, focusing on development technologies e.g. .NET, Visual Studio, Desktop/Web/Cloud solutions.

He works on the development and application of different ML algorithms. In the development of ML-oriented solutions and modeling, he has more than 10 years of experience. His field of interest is also the development of predictive models with the ML.NET and Keras, but also actively develop two ML-based .NET open source projects: GPdotNET-genetic programming tool and ANNdotNET - deep learning tool on .NET platform. He works in multidisciplinary teams with the mission of optimizing and selecting the ML algorithms to build ML models.

He is the author of several books, and many online articles, writes a blog at http://bhrnjica.net, regularly holds lectures at local and regional conferences, User groups and Code Camp gatherings, and is also the founder of the Bihac Developer Meetup Group. Microsoft recognizes his work and awarded him with the prestigious Microsoft MVP title for the first time in 2011, which he still holds today.

Comments and Discussions

 
QuestionHow to download full project? Pin
Buratino2k17-Aug-18 1:02
Buratino2k17-Aug-18 1:02 
QuestionQuestion regarding updating the code to handle a different problem Pin
Pr34cher26-Mar-18 3:29
Pr34cher26-Mar-18 3:29 
AnswerRe: Question regarding updating the code to handle a different problem Pin
Bahrudin Hrnjica17-May-18 22:34
professionalBahrudin Hrnjica17-May-18 22:34 
QuestionExecution error inquiry Pin
Member 1210376021-Jan-18 16:01
Member 1210376021-Jan-18 16:01 
AnswerRe: Execution error inquiry Pin
Bahrudin Hrnjica22-Jan-18 10:59
professionalBahrudin Hrnjica22-Jan-18 10:59 
GeneralRe: Execution error inquiry Pin
Member 1210376023-Jan-18 14:14
Member 1210376023-Jan-18 14:14 
GeneralRe: Execution error inquiry Pin
Bahrudin Hrnjica29-Jan-18 6:58
professionalBahrudin Hrnjica29-Jan-18 6:58 
GeneralRe: Execution error inquiry Pin
Member 1210376030-Jan-18 20:19
Member 1210376030-Jan-18 20:19 
GeneralRe: Execution error inquiry Pin
Bahrudin Hrnjica4-Feb-18 7:32
professionalBahrudin Hrnjica4-Feb-18 7:32 
Questionmulti class question Pin
nikosdim17-Jan-18 12:07
nikosdim17-Jan-18 12:07 
AnswerRe: multi class question Pin
Bahrudin Hrnjica17-Jan-18 22:01
professionalBahrudin Hrnjica17-Jan-18 22:01 
QuestionThank you bro! Pin
Member 42255848-Dec-17 15:22
Member 42255848-Dec-17 15:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.