Commit 32125dea authored by Davis King's avatar Davis King

Optimized batch normalization code

parent 273a21cf
This diff is collapsed.
...@@ -43,7 +43,7 @@ namespace dlib ...@@ -43,7 +43,7 @@ namespace dlib
void batch_normalize ( void batch_normalize (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -52,7 +52,7 @@ namespace dlib ...@@ -52,7 +52,7 @@ namespace dlib
void batch_normalize_gradient ( void batch_normalize_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
...@@ -63,7 +63,7 @@ namespace dlib ...@@ -63,7 +63,7 @@ namespace dlib
void batch_normalize_conv ( void batch_normalize_conv (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -72,7 +72,7 @@ namespace dlib ...@@ -72,7 +72,7 @@ namespace dlib
void batch_normalize_conv_gradient ( void batch_normalize_conv_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
......
...@@ -74,7 +74,7 @@ namespace dlib ...@@ -74,7 +74,7 @@ namespace dlib
void batch_normalize ( void batch_normalize (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -90,19 +90,19 @@ namespace dlib ...@@ -90,19 +90,19 @@ namespace dlib
ensures ensures
- have_same_dimensions(#dest, src) == true - have_same_dimensions(#dest, src) == true
- #means.num_samples() == 1 - #means.num_samples() == 1
- #vars.num_samples() == 1 - #invstds.num_samples() == 1
- means.nr() == vars.nr() == src.nr() - means.nr() == invstds.nr() == src.nr()
- means.nc() == vars.nc() == src.nc() - means.nc() == invstds.nc() == src.nc()
- means.k() == vars.k() == src.k() - means.k() == invstds.k() == src.k()
- #src == the batch normalized version of src. - #src == the batch normalized version of src.
- #means == the mean values of the contents of src. - #means == the mean values of the contents of src.
- #vars == the variance values of the contents of src. - #invstds == 1/(the standard deviation values of the contents of src).
!*/ !*/
void batch_normalize_gradient ( void batch_normalize_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
...@@ -111,8 +111,8 @@ namespace dlib ...@@ -111,8 +111,8 @@ namespace dlib
); );
/*! /*!
requires requires
- vars and means should be the output of a call to - invstds and means should be the output of a call to
batch_normalize(dest,means,vars,src,gamma,beta) batch_normalize(dest,means,invstds,src,gamma,beta)
- have_same_dimensions(gradient_input, src) == true - have_same_dimensions(gradient_input, src) == true
- have_same_dimensions(src, src_grad) == true - have_same_dimensions(src, src_grad) == true
- src.num_samples() > 1 - src.num_samples() > 1
...@@ -123,10 +123,10 @@ namespace dlib ...@@ -123,10 +123,10 @@ namespace dlib
- gamma.nc() == src.nc() - gamma.nc() == src.nc()
- gamma.k() == src.k() - gamma.k() == src.k()
- have_same_dimensions(means, gamma) == true - have_same_dimensions(means, gamma) == true
- have_same_dimensions(vars, gamma) == true - have_same_dimensions(invstds, gamma) == true
ensures ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of - Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize(dest,means,vars,src,gamma,beta)) batch_normalize(dest,means,invstds,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src_grad. - Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad. - Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad. - Adds the gradient of f() with respect to beta to #beta_grad.
...@@ -135,7 +135,7 @@ namespace dlib ...@@ -135,7 +135,7 @@ namespace dlib
void batch_normalize_conv ( void batch_normalize_conv (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -149,17 +149,17 @@ namespace dlib ...@@ -149,17 +149,17 @@ namespace dlib
ensures ensures
- have_same_dimensions(#dest, src) == true - have_same_dimensions(#dest, src) == true
- #means.num_samples()==means.nr()==means.nc() == 1 - #means.num_samples()==means.nr()==means.nc() == 1
- #vars.num_samples() ==vars.nr() ==vars.nc() == 1 - #invstds.num_samples() ==invstds.nr() ==invstds.nc() == 1
- means.k() == vars.k() == src.k() - means.k() == invstds.k() == src.k()
- #src == the batch normalized version of src. - #src == the batch normalized version of src.
- #means == the mean values of the contents of src. - #means == the mean values of the contents of src.
- #vars == the variance values of the contents of src. - #invstds == 1/(the standard deviation values of the contents of src).
!*/ !*/
void batch_normalize_conv_gradient ( void batch_normalize_conv_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
...@@ -168,8 +168,8 @@ namespace dlib ...@@ -168,8 +168,8 @@ namespace dlib
); );
/*! /*!
requires requires
- vars and means should be the output of a call to - invstds and means should be the output of a call to
batch_normalize_conv(dest,means,vars,src,gamma,beta) batch_normalize_conv(dest,means,invstds,src,gamma,beta)
- have_same_dimensions(gradient_input, src) == true - have_same_dimensions(gradient_input, src) == true
- have_same_dimensions(src, src_grad) == true - have_same_dimensions(src, src_grad) == true
- src.num_samples() > 1 - src.num_samples() > 1
...@@ -178,10 +178,10 @@ namespace dlib ...@@ -178,10 +178,10 @@ namespace dlib
- have_same_dimensions(gamma, beta_grad) == true - have_same_dimensions(gamma, beta_grad) == true
- gamma.k() == src.k() - gamma.k() == src.k()
- have_same_dimensions(means, gamma) == true - have_same_dimensions(means, gamma) == true
- have_same_dimensions(vars, gamma) == true - have_same_dimensions(invstds, gamma) == true
ensures ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of - Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize_conv(dest,means,vars,src,gamma,beta)) batch_normalize_conv(dest,means,invstds,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src_grad. - Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad. - Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad. - Adds the gradient of f() with respect to beta to #beta_grad.
......
...@@ -150,7 +150,7 @@ namespace dlib { namespace tt ...@@ -150,7 +150,7 @@ namespace dlib { namespace tt
void batch_normalize ( void batch_normalize (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -166,13 +166,13 @@ namespace dlib { namespace tt ...@@ -166,13 +166,13 @@ namespace dlib { namespace tt
ensures ensures
- have_same_dimensions(#dest, src) == true - have_same_dimensions(#dest, src) == true
- #means.num_samples() == 1 - #means.num_samples() == 1
- #vars.num_samples() == 1 - #invstds.num_samples() == 1
- means.nr() == vars.nr() == src.nr() - means.nr() == invstds.nr() == src.nr()
- means.nc() == vars.nc() == src.nc() - means.nc() == invstds.nc() == src.nc()
- means.k() == vars.k() == src.k() - means.k() == invstds.k() == src.k()
- #src == the batch normalized version of src. - #src == the batch normalized version of src.
- #means == the mean values of the contents of src. - #means == the mean values of the contents of src.
- #vars == the variance values of the contents of src. - #invstds == 1/(the standard deviation values of the contents of src).
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -180,7 +180,7 @@ namespace dlib { namespace tt ...@@ -180,7 +180,7 @@ namespace dlib { namespace tt
void batch_normalize_gradient ( void batch_normalize_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
...@@ -189,8 +189,8 @@ namespace dlib { namespace tt ...@@ -189,8 +189,8 @@ namespace dlib { namespace tt
); );
/*! /*!
requires requires
- vars and means should be the output of a call to - invstds and means should be the output of a call to
batch_normalize(dest,means,vars,src,gamma,beta) batch_normalize(dest,means,invstds,src,gamma,beta)
- have_same_dimensions(gradient_input, src) == true - have_same_dimensions(gradient_input, src) == true
- have_same_dimensions(src, src_grad) == true - have_same_dimensions(src, src_grad) == true
- src.num_samples() > 1 - src.num_samples() > 1
...@@ -201,10 +201,10 @@ namespace dlib { namespace tt ...@@ -201,10 +201,10 @@ namespace dlib { namespace tt
- gamma.nc() == src.nc() - gamma.nc() == src.nc()
- gamma.k() == src.k() - gamma.k() == src.k()
- have_same_dimensions(means, gamma) == true - have_same_dimensions(means, gamma) == true
- have_same_dimensions(vars, gamma) == true - have_same_dimensions(invstds, gamma) == true
ensures ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of - Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize(dest,means,vars,src,gamma,beta)) batch_normalize(dest,means,invstds,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src_grad. - Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad. - Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad. - Adds the gradient of f() with respect to beta to #beta_grad.
...@@ -213,7 +213,7 @@ namespace dlib { namespace tt ...@@ -213,7 +213,7 @@ namespace dlib { namespace tt
void batch_normalize_conv ( void batch_normalize_conv (
resizable_tensor& dest, resizable_tensor& dest,
resizable_tensor& means, resizable_tensor& means,
resizable_tensor& vars, resizable_tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
const tensor& beta const tensor& beta
...@@ -227,17 +227,17 @@ namespace dlib { namespace tt ...@@ -227,17 +227,17 @@ namespace dlib { namespace tt
ensures ensures
- have_same_dimensions(#dest, src) == true - have_same_dimensions(#dest, src) == true
- #means.num_samples()==means.nr()==means.nc() == 1 - #means.num_samples()==means.nr()==means.nc() == 1
- #vars.num_samples() ==vars.nr() ==vars.nc() == 1 - #invstds.num_samples() ==invstds.nr() ==invstds.nc() == 1
- means.k() == vars.k() == src.k() - means.k() == invstds.k() == src.k()
- #src == the batch normalized version of src. - #src == the batch normalized version of src.
- #means == the mean values of the contents of src. - #means == the mean values of the contents of src.
- #vars == the variance values of the contents of src. - #invstds == 1/(the standard deviation values of the contents of src).
!*/ !*/
void batch_normalize_conv_gradient ( void batch_normalize_conv_gradient (
const tensor& gradient_input, const tensor& gradient_input,
const tensor& means, const tensor& means,
const tensor& vars, const tensor& invstds,
const tensor& src, const tensor& src,
const tensor& gamma, const tensor& gamma,
tensor& src_grad, tensor& src_grad,
...@@ -246,8 +246,8 @@ namespace dlib { namespace tt ...@@ -246,8 +246,8 @@ namespace dlib { namespace tt
); );
/*! /*!
requires requires
- vars and means should be the output of a call to - invstds and means should be the output of a call to
batch_normalize_conv(dest,means,vars,src,gamma,beta) batch_normalize_conv(dest,means,invstds,src,gamma,beta)
- have_same_dimensions(gradient_input, src) == true - have_same_dimensions(gradient_input, src) == true
- have_same_dimensions(src, src_grad) == true - have_same_dimensions(src, src_grad) == true
- src.num_samples() > 1 - src.num_samples() > 1
...@@ -256,10 +256,10 @@ namespace dlib { namespace tt ...@@ -256,10 +256,10 @@ namespace dlib { namespace tt
- have_same_dimensions(gamma, beta_grad) == true - have_same_dimensions(gamma, beta_grad) == true
- gamma.k() == src.k() - gamma.k() == src.k()
- have_same_dimensions(means, gamma) == true - have_same_dimensions(means, gamma) == true
- have_same_dimensions(vars, gamma) == true - have_same_dimensions(invstds, gamma) == true
ensures ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of - Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize_conv(dest,means,vars,src,gamma,beta)) batch_normalize_conv(dest,means,invstds,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src_grad. - Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad. - Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad. - Adds the gradient of f() with respect to beta to #beta_grad.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment