Skip to content

Commit 27a0135

Browse files
Cydraldavisking
andauthored
Add customizable dropout layer with compile-time rate specification (davisking#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>
1 parent 98d26df commit 27a0135

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

dlib/dnn/layers.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,24 @@ namespace dlib
21342134
template <typename SUBNET>
21352135
using dropout = add_layer<dropout_, SUBNET>;
21362136

2137+
// ----------------------------------------------------------------------------------------
2138+
2139+
template <int DROP_RATE_PERCENT>
2140+
class dropout_rate_ : public dropout_
2141+
{
2142+
public:
2143+
explicit dropout_rate_() : dropout_(static_cast<float>(DROP_RATE_PERCENT) / 100.0f)
2144+
{
2145+
static_assert(DROP_RATE_PERCENT >= 0 && DROP_RATE_PERCENT <= 100,
2146+
"DROP_RATE_PERCENT must be between 0 and 100, inclusive.");
2147+
}
2148+
};
2149+
2150+
template <int DROP_RATE, typename SUBNET>
2151+
using dropout_rate = add_layer<dropout_rate_<DROP_RATE>, SUBNET>;
2152+
template <typename SUBNET>
2153+
using dropout_10 = add_layer<dropout_rate_<10>, SUBNET>;
2154+
21372155
// ----------------------------------------------------------------------------------------
21382156

21392157
class multiply_

dlib/dnn/layers_abstract.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,41 @@ namespace dlib
14331433
template <typename SUBNET>
14341434
using dropout = add_layer<dropout_, SUBNET>;
14351435

1436+
// ----------------------------------------------------------------------------------------
1437+
1438+
template <int DROP_RATE_PERCENT>
1439+
class dropout_rate_ : public dropout_
1440+
{
1441+
/*!
1442+
WHAT THIS OBJECT REPRESENTS
1443+
This object represents a customizable dropout layer that inherits from
1444+
the dropout_ class. It allows specifying the dropout rate at compile-time,
1445+
which is particularly useful for deep networks with many layers where it
1446+
might be cumbersome to explicitly modify the dropout rate for each layer
1447+
individually.
1448+
1449+
The main advantage of this layer is that it offers the possibility to specify
1450+
the dropout rate at the moment of network construction, providing more
1451+
flexibility and clarity in the network architecture definition.
1452+
1453+
TEMPLATE PARAMETERS
1454+
- DROP_RATE_PERCENT: A int value between 0 and 100 that specifies the dropout rate.
1455+
This value is set at compile-time and cannot be changed during runtime.
1456+
!*/
1457+
1458+
public:
1459+
explicit dropout_rate_();
1460+
/*!
1461+
ensures
1462+
- Constructs a dropout layer with a dropout rate of DROP_RATE.
1463+
- Calls the base class constructor dropout_(DROP_RATE).
1464+
!*/
1465+
};
1466+
1467+
template <int DROP_RATE, typename SUBNET>
1468+
using dropout_rate = add_layer<dropout_rate_<DROP_RATE>, SUBNET>;
1469+
template <typename SUBNET>
1470+
using dropout_10 = add_layer<dropout_rate_<10>, SUBNET>;
14361471
// ----------------------------------------------------------------------------------------
14371472

14381473
class multiply_

0 commit comments

Comments
 (0)