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 <davis685@gmail.com>
This commit is contained in:
Cydral 2024-08-29 13:40:10 +02:00 committed by GitHub
parent 98d26dfaf5
commit 27a0135220
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 0 deletions

View File

@ -2134,6 +2134,24 @@ namespace dlib
template <typename SUBNET>
using dropout = add_layer<dropout_, SUBNET>;
// ----------------------------------------------------------------------------------------
template <int DROP_RATE_PERCENT>
class dropout_rate_ : public dropout_
{
public:
explicit dropout_rate_() : dropout_(static_cast<float>(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 <int DROP_RATE, typename SUBNET>
using dropout_rate = add_layer<dropout_rate_<DROP_RATE>, SUBNET>;
template <typename SUBNET>
using dropout_10 = add_layer<dropout_rate_<10>, SUBNET>;
// ----------------------------------------------------------------------------------------
class multiply_

View File

@ -1433,6 +1433,41 @@ namespace dlib
template <typename SUBNET>
using dropout = add_layer<dropout_, SUBNET>;
// ----------------------------------------------------------------------------------------
template <int DROP_RATE_PERCENT>
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 <int DROP_RATE, typename SUBNET>
using dropout_rate = add_layer<dropout_rate_<DROP_RATE>, SUBNET>;
template <typename SUBNET>
using dropout_10 = add_layer<dropout_rate_<10>, SUBNET>;
// ----------------------------------------------------------------------------------------
class multiply_