How to setup learning rate per iteration in CTNK using C#


So far we have seen how to train and validate models in CNTK using C#. Also there many more details which should be revealed in order to better understand the CNTK library. One of the important feature not only in the CNTK but also in every DNN (deep neural networks) is the learning rate.

In ANN the learning rate is the number by which the derivative is multiply before it is subtracted by the weight. If the weight is decreased to much the loss function will be increased and the network will diverge. On the other hand if the weight is decreased to little the loss function will be changed little and the diverge progress will be to slow. So selecting the right value of the parameter is important. During the training process, the learning rate is usually defined as constant value. In CNTK the learning rate is defined as follow:

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

From the code above the learning rate is assign to 0.2 value per sample. This means whole training process will be done with the learning rate of 0.2.
The CNTK support dynamic changing of the learning rate.
Assume we want to setup different the learning rates so that from the fist to the 100 iterations the learning rate would be 0.2. From the 100 to 500 iterations we want the learning rate would be 0.1. Moreover, after the 500 iterations are completed and to he end of the iteration process, we want to setup the learning rate to 0.05.

Above said can be expressed:

lr1=0.2 , from 1 to 100 iterations

lr2= 0.1 from 100 to 500 iterations

lr3= 0.05 from 500 to the end of the searching process.

In case we want to setup the learning rate dynamically we need to use the PairSizeTDouble class in order to defined the learning rate. So for the above requirements the flowing code should be implemented:

PairSizeTDouble p1 = new PairSizeTDouble(2, 0.2);
PairSizeTDouble p2 = new PairSizeTDouble(10, 0.1);
PairSizeTDouble p3 = new PairSizeTDouble(1, 0.05);

var vp = new VectorPairSizeTDouble() { p1, p2, p3 };
var learningRatePerSample = new CNTK.TrainingParameterScheduleDouble(vp, 50);

First we need to defined PairSizeTDouble object for every learning rate value, with the integer number which will be multiply.
Once we define the rates, make a array of rate values by creating the VectorPairSizeTDouble object. Then the array is passed as the first argument in the TrainingParameterScheduleDouble method. The second argument of the method is multiplication number. So in the first rate value, the 2 is multiple with 50 which is 100, and denotes the iteration number. Similar multiplication are done in the other rate values.

Advertisements

About Bahrudin Hrnjica

PhD in Mechanical Engineering, Microsoft MVP for Visual Studio and Development Technologies. Likes .NET, Math,Data Science, Evolutionary Algorithms, Machine Learning, Blogging.

Posted on 22/11/2017, in .NET, C#, CNTK, CodeProject and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s