Add warning for batchsize = 1

Use term cumulative avg instead of counting
This commit is contained in:
kartikdutt18
2020-07-13 09:10:07 +05:30
parent 0ae5fcd7a3
commit 626bd67d64
2 changed files with 14 additions and 2 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ class BatchNorm
*
* @param size The number of input units / channels.
* @param eps The epsilon added to variance to ensure numerical stability.
* @param average Boolean to determine whether counting average is used for
* @param average Boolean to determine whether cumulative average is used for
* updating the parameters or momentum is used.
* @param momentum Parameter used to to update the running mean and variance.
*/
@@ -91,6 +91,14 @@ void BatchNorm<InputDataType, OutputDataType>::Forward(
// We will calculate minibatch norm on each channel / feature map.
if (!deterministic)
{
// Check only during training, batch-size can be one during inference.
if (batchSize == 1 && inputSize == 1)
{
Log::Warn << "Variance for single element isn't defined and" <<
" will be set to 0.0 for training. Use a batch-size" <<
" greater than 1 to fix the warning." << std::endl;
}
// Input corresponds to output from convolution layer.
// Use a cube for simplicity.
arma::cube inputTemp(const_cast<arma::Mat<eT>&>(input).memptr(),
@@ -129,11 +137,15 @@ void BatchNorm<InputDataType, OutputDataType>::Forward(
count += 1;
averageFactor = average ? 1.0 / count : momentum;
double nElements = 0.0;
if (input.n_elem - size != 0)
nElements = 1.0 / (input.n_elem - size + eps);
// Update running mean and running variance.
runningMean = (1 - averageFactor) * runningMean + averageFactor *
mean.t();
runningVariance = (1 - averageFactor) * runningVariance +
input.n_elem * (1.0 / (input.n_elem - size + eps)) *
input.n_elem * nElements *
averageFactor * variance.t();
}
else