// Copyright (C) 2016 Davis E. King (davis@dlib.net) // License: Boost Software License See LICENSE.txt for the full license. #ifndef DLIB_DNn_UTILITIES_H_ #define DLIB_DNn_UTILITIES_H_ #include "core.h" #include "utilities_abstract.h" #include "../geometry.h" #include namespace dlib { // ---------------------------------------------------------------------------------------- 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 { class visitor_net_to_xml { public: visitor_net_to_xml(std::ostream& out_) : out(out_) {} template void operator()(size_t idx, const input_layer_type& l) { out << "\n"; to_xml(l,out); out << "\n"; } template void operator()(size_t idx, const add_loss_layer& l) { out << "\n"; to_xml(l.loss_details(),out); out << "\n"; } template void operator()(size_t idx, const add_layer& l) { out << "\n"; to_xml(l.layer_details(),out); out << "\n"; } template void operator()(size_t idx, const add_tag_layer& /*l*/) { out << "\n"; } template class T, typename U> void operator()(size_t idx, const add_skip_layer& /*l*/) { out << "\n"; } private: std::ostream& out; }; } template void net_to_xml ( const net_type& net, std::ostream& out ) { auto old_precision = out.precision(9); out << "\n"; visit_layers(net, impl::visitor_net_to_xml(out)); out << "\n"; // restore the original stream precision. out.precision(old_precision); } template void net_to_xml ( const net_type& net, const std::string& filename ) { std::ofstream fout(filename); net_to_xml(net, fout); } // ---------------------------------------------------------------------------------------- namespace impl { class visitor_net_map_input_to_output { public: visitor_net_map_input_to_output(dpoint& p_) : p(p_) {} dpoint& p; template void operator()(const input_layer_type& ) { } template void operator()(const add_loss_layer& net) { (*this)(net.subnet()); } template void operator()(const add_layer& net) { (*this)(net.subnet()); p = net.layer_details().map_input_to_output(p); } template void operator()(const dimpl::subnet_wrapper,B>& net) { (*this)(net.subnet()); p = net.layer_details().map_input_to_output(p); } template void operator()(const add_tag_layer& net) { // tag layers are an identity transform, so do nothing (*this)(net.subnet()); } template void operator()(const dimpl::subnet_wrapper,is_first>& net) { // tag layers are an identity transform, so do nothing (*this)(net.subnet()); } template class TAG_TYPE, typename U> void operator()(const add_skip_layer& net) { (*this)(layer(net)); } template class TAG_TYPE, typename SUBNET> void operator()(const dimpl::subnet_wrapper,is_first>& net) { // skip layers are an identity transform, so do nothing (*this)(layer(net)); } }; class visitor_net_map_output_to_input { public: visitor_net_map_output_to_input(dpoint& p_) : p(p_) {} dpoint& p; template void operator()(const input_layer_type& ) { } template void operator()(const add_loss_layer& net) { (*this)(net.subnet()); } template void operator()(const add_layer& net) { p = net.layer_details().map_output_to_input(p); (*this)(net.subnet()); } template void operator()(const dimpl::subnet_wrapper,B>& net) { p = net.layer_details().map_output_to_input(p); (*this)(net.subnet()); } template void operator()(const add_tag_layer& net) { // tag layers are an identity transform, so do nothing (*this)(net.subnet()); } template void operator()(const dimpl::subnet_wrapper,is_first>& net) { // tag layers are an identity transform, so do nothing (*this)(net.subnet()); } template class TAG_TYPE, typename U> void operator()(const add_skip_layer& net) { (*this)(layer(net)); } template class TAG_TYPE, typename SUBNET> void operator()(const dimpl::subnet_wrapper,is_first>& net) { // skip layers are an identity transform, so do nothing (*this)(layer(net)); } }; } template inline dpoint input_tensor_to_output_tensor( const net_type& net, dpoint p ) { impl::visitor_net_map_input_to_output temp(p); temp(net); return p; } template inline dpoint output_tensor_to_input_tensor( const net_type& net, dpoint p ) { impl::visitor_net_map_output_to_input temp(p); temp(net); return p; } // ---------------------------------------------------------------------------------------- template size_t count_parameters( const net_type& net ) { size_t num_parameters = 0; visit_layer_parameters(net, [&](const tensor& t) { num_parameters += t.size(); }); return num_parameters; } // ---------------------------------------------------------------------------------------- namespace impl { class visitor_learning_rate_multiplier { public: visitor_learning_rate_multiplier(double new_learning_rate_multiplier_) : new_learning_rate_multiplier(new_learning_rate_multiplier_) {} template void operator()(layer& l) const { set_learning_rate_multiplier(l, new_learning_rate_multiplier); } private: double new_learning_rate_multiplier; }; } template void set_all_learning_rate_multipliers( net_type& net, double learning_rate_multiplier ) { DLIB_CASSERT(learning_rate_multiplier >= 0); impl::visitor_learning_rate_multiplier temp(learning_rate_multiplier); visit_computational_layers(net, temp); } template void set_learning_rate_multipliers_range( net_type& net, double learning_rate_multiplier ) { static_assert(begin <= end, "Invalid range"); static_assert(end <= net_type::num_layers, "Invalid range"); DLIB_CASSERT(learning_rate_multiplier >= 0); impl::visitor_learning_rate_multiplier temp(learning_rate_multiplier); visit_computational_layers_range(net, temp); } // ---------------------------------------------------------------------------------------- } #endif // DLIB_DNn_UTILITIES_H_