From 27a0135220935b9118add4d807f075f1738a9100 Mon Sep 17 00:00:00 2001 From: Cydral <53169060+Cydral@users.noreply.github.com> Date: Thu, 29 Aug 2024 13:40:10 +0200 Subject: [PATCH] Add customizable dropout layer with compile-time rate specification (#3000) * Add customizable dropout layer with compile-time rate specification * Update to the name of the new dropout rate customisation class * Fix: Replace float template parameter with int for C++17 compatibility * Update dlib/dnn/layers_abstract.h --------- Co-authored-by: Davis E. King --- dlib/dnn/layers.h | 18 ++++++++++++++++++ dlib/dnn/layers_abstract.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/dlib/dnn/layers.h b/dlib/dnn/layers.h index f92749d7b..77ff918a6 100644 --- a/dlib/dnn/layers.h +++ b/dlib/dnn/layers.h @@ -2134,6 +2134,24 @@ namespace dlib template using dropout = add_layer; +// ---------------------------------------------------------------------------------------- + + template + class dropout_rate_ : public dropout_ + { + public: + explicit dropout_rate_() : dropout_(static_cast(DROP_RATE_PERCENT) / 100.0f) + { + static_assert(DROP_RATE_PERCENT >= 0 && DROP_RATE_PERCENT <= 100, + "DROP_RATE_PERCENT must be between 0 and 100, inclusive."); + } + }; + + template + using dropout_rate = add_layer, SUBNET>; + template + using dropout_10 = add_layer, SUBNET>; + // ---------------------------------------------------------------------------------------- class multiply_ diff --git a/dlib/dnn/layers_abstract.h b/dlib/dnn/layers_abstract.h index 1ddecead0..7a29ab134 100644 --- a/dlib/dnn/layers_abstract.h +++ b/dlib/dnn/layers_abstract.h @@ -1433,6 +1433,41 @@ namespace dlib template using dropout = add_layer; +// ---------------------------------------------------------------------------------------- + + template + class dropout_rate_ : public dropout_ + { + /*! + WHAT THIS OBJECT REPRESENTS + This object represents a customizable dropout layer that inherits from + the dropout_ class. It allows specifying the dropout rate at compile-time, + which is particularly useful for deep networks with many layers where it + might be cumbersome to explicitly modify the dropout rate for each layer + individually. + + The main advantage of this layer is that it offers the possibility to specify + the dropout rate at the moment of network construction, providing more + flexibility and clarity in the network architecture definition. + + TEMPLATE PARAMETERS + - DROP_RATE_PERCENT: A int value between 0 and 100 that specifies the dropout rate. + This value is set at compile-time and cannot be changed during runtime. + !*/ + + public: + explicit dropout_rate_(); + /*! + ensures + - Constructs a dropout layer with a dropout rate of DROP_RATE. + - Calls the base class constructor dropout_(DROP_RATE). + !*/ + }; + + template + using dropout_rate = add_layer, SUBNET>; + template + using dropout_10 = add_layer, SUBNET>; // ---------------------------------------------------------------------------------------- class multiply_