diff --git a/dlib/dnn/core.h b/dlib/dnn/core.h index 03a699239..5d1ac529a 100644 --- a/dlib/dnn/core.h +++ b/dlib/dnn/core.h @@ -112,23 +112,6 @@ namespace dlib } } -// ---------------------------------------------------------------------------------------- - - inline double log1pexp(double x) - { - using std::exp; - using namespace std; // Do this instead of using std::log1p because some compilers - // error out otherwise (E.g. gcc 4.9 in cygwin) - if (x <= -37) - return exp(x); - else if (-37 < x && x <= 18) - return log1p(exp(x)); - else if (18 < x && x <= 33.3) - return x + exp(-x); - else - return x; - } - // ---------------------------------------------------------------------------------------- // Tell us if T is one of the special layer types (i.e. add_layer, repeat, add_tag_layer, or @@ -442,24 +425,6 @@ namespace dlib { return item; } -// ---------------------------------------------------------------------------------------- - - inline void randomize_parameters ( - tensor& params, - unsigned long num_inputs_and_outputs, - dlib::rand& rnd - ) - { - for (auto& val : params) - { - // Draw a random number to initialize the layer according to formula (16) - // from Understanding the difficulty of training deep feedforward neural - // networks by Xavier Glorot and Yoshua Bengio. - val = 2*rnd.get_random_float()-1; - val *= std::sqrt(6.0/(num_inputs_and_outputs)); - } - } - // ---------------------------------------------------------------------------------------- template diff --git a/dlib/dnn/core_abstract.h b/dlib/dnn/core_abstract.h index 86683d00e..11c7b8600 100644 --- a/dlib/dnn/core_abstract.h +++ b/dlib/dnn/core_abstract.h @@ -16,23 +16,6 @@ namespace dlib // ---------------------------------------------------------------------------------------- - void randomize_parameters ( - tensor& params, - unsigned long num_inputs_and_outputs, - dlib::rand& rnd - ); - /*! - ensures - - This function assigns random values into params based on the given random - number generator. In particular, it uses the parameter initialization method - of formula 16 from the paper "Understanding the difficulty of training deep - feedforward neural networks" by Xavier Glorot and Yoshua Bengio. - - It is assumed that the total number of inputs and outputs from the layer is - num_inputs_and_outputs. That is, you should set num_inputs_and_outputs to - the sum of the dimensionalities of the vectors going into and out of the - layer that uses params as its parameters. - !*/ - template < typename... T > @@ -58,15 +41,6 @@ namespace dlib a non-std::tuple object is found. !*/ - double log1pexp( - double x - ); - /*! - ensures - - returns log(1+exp(x)) - (except computes it using a numerically accurate method) - !*/ - // ---------------------------------------------------------------------------------------- template diff --git a/dlib/dnn/layers.h b/dlib/dnn/layers.h index 5af5ab5df..355c0c044 100644 --- a/dlib/dnn/layers.h +++ b/dlib/dnn/layers.h @@ -12,6 +12,7 @@ #include "../string.h" #include "tensor_tools.h" #include "../vectorstream.h" +#include "utilities.h" namespace dlib diff --git a/dlib/dnn/utilities.h b/dlib/dnn/utilities.h index 6bee976e0..116d5bbad 100644 --- a/dlib/dnn/utilities.h +++ b/dlib/dnn/utilities.h @@ -9,6 +9,41 @@ namespace dlib { +// ---------------------------------------------------------------------------------------- + + inline double log1pexp(double x) + { + using std::exp; + using namespace std; // Do this instead of using std::log1p because some compilers + // error out otherwise (E.g. gcc 4.9 in cygwin) + if (x <= -37) + return exp(x); + else if (-37 < x && x <= 18) + return log1p(exp(x)); + else if (18 < x && x <= 33.3) + return x + exp(-x); + else + return x; + } + +// ---------------------------------------------------------------------------------------- + + inline void randomize_parameters ( + tensor& params, + unsigned long num_inputs_and_outputs, + dlib::rand& rnd + ) + { + for (auto& val : params) + { + // Draw a random number to initialize the layer according to formula (16) + // from Understanding the difficulty of training deep feedforward neural + // networks by Xavier Glorot and Yoshua Bengio. + val = 2*rnd.get_random_float()-1; + val *= std::sqrt(6.0/(num_inputs_and_outputs)); + } + } + // ---------------------------------------------------------------------------------------- namespace impl diff --git a/dlib/dnn/utilities_abstract.h b/dlib/dnn/utilities_abstract.h index 8408d09dd..77e8d05df 100644 --- a/dlib/dnn/utilities_abstract.h +++ b/dlib/dnn/utilities_abstract.h @@ -8,6 +8,36 @@ namespace dlib { +// ---------------------------------------------------------------------------------------- + + double log1pexp( + double x + ); + /*! + ensures + - returns log(1+exp(x)) + (except computes it using a numerically accurate method) + !*/ + +// ---------------------------------------------------------------------------------------- + + void randomize_parameters ( + tensor& params, + unsigned long num_inputs_and_outputs, + dlib::rand& rnd + ); + /*! + ensures + - This function assigns random values into params based on the given random + number generator. In particular, it uses the parameter initialization method + of formula 16 from the paper "Understanding the difficulty of training deep + feedforward neural networks" by Xavier Glorot and Yoshua Bengio. + - It is assumed that the total number of inputs and outputs from the layer is + num_inputs_and_outputs. That is, you should set num_inputs_and_outputs to + the sum of the dimensionalities of the vectors going into and out of the + layer that uses params as its parameters. + !*/ + // ---------------------------------------------------------------------------------------- template