Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into linux-gcc
Browse files Browse the repository at this point in the history
  • Loading branch information
chenguoguo committed Jul 18, 2015
2 parents 967c6ba + 329d3bc commit 82c7d8c
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion MachineLearning/CNTK/SGD.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class SGD : ComputationNetworkHelper<ElemType>
{
ConfigArray learningRatesPerMBStr = configSGD("learningRatesPerMB", "");
m_needToNormalizeLRByParallUtterance = false;
m_needToNormalizeMomentumByParallUtterance = false;
floatargvector learningRatesPerMB = learningRatesPerMBStr;

ConfigArray learningRatesPerSampleStr = configSGD("learningRatesPerSample", "");
Expand Down Expand Up @@ -437,6 +438,8 @@ class SGD : ComputationNetworkHelper<ElemType>
}
m_momentumPerSample[i] = (float)pow(momentumPerMB[i], 1.0 / m_mbSize[i]);
}

m_needToNormalizeMomentumByParallUtterance = true;
}
else
{
Expand Down Expand Up @@ -770,6 +773,15 @@ class SGD : ComputationNetworkHelper<ElemType>
x /= trainSetDataReader->NumberSlicesInEachRecurrentIter();
}
}

// first, we need to normalize the effect of nbruttsineachrecurrentiter for momemtum
if (trainSetDataReader->NumberSlicesInEachRecurrentIter() > 1 && m_needToNormalizeMomentumByParallUtterance)
{
for (auto& x : m_momentumPerSample)
{
x = (float)pow(x, 1.0 / trainSetDataReader->NumberSlicesInEachRecurrentIter());
}
}

bool learnRateInitialized = false;
if (startEpoch > 0)
Expand Down Expand Up @@ -857,6 +869,7 @@ class SGD : ComputationNetworkHelper<ElemType>
INT32 mySamples = (INT32)
#endif
size_t chosenMinibatchSize;
size_t actualMinibatchSize;

// Through the command line or config file the user can set minibatch sizes on a per epoch
// basis for a set number of epochs. For epochs after that point, m_mbSize.size(), either
Expand Down Expand Up @@ -885,9 +898,17 @@ class SGD : ComputationNetworkHelper<ElemType>
// use the explicitly set minibatch size
chosenMinibatchSize = m_mbSize[i];
}

actualMinibatchSize = chosenMinibatchSize;
if (trainSetDataReader->NumberSlicesInEachRecurrentIter() > 1 && m_needToNormalizeMomentumByParallUtterance)
{
actualMinibatchSize = chosenMinibatchSize * trainSetDataReader->NumberSlicesInEachRecurrentIter();
}



fprintf(stderr, "Starting Epoch %d: learning rate per sample = %f momentum = %f \n",
i + 1, learnRatePerSample, MomentumPerMB(m_momentumPerSample[i], chosenMinibatchSize));
i + 1, learnRatePerSample, MomentumPerMB(m_momentumPerSample[i], actualMinibatchSize));

TrainOneEpoch(net,
refNet,
Expand Down Expand Up @@ -2310,6 +2331,7 @@ class SGD : ComputationNetworkHelper<ElemType>

// only true when the user specify LearningRatePerMB and the number of parallel utterances in Reader > 1
bool m_needToNormalizeLRByParallUtterance;
bool m_needToNormalizeMomentumByParallUtterance;

intargvector m_mbSize;

Expand Down

0 comments on commit 82c7d8c

Please sign in to comment.